当前位置:网站首页>Python object-oriented
Python object-oriented
2022-08-08 22:37:00 【On3_Thing】
Class Definition
Similar to C++, Python's object-oriented mechanism:
Class (Class): used to describe a collection of objects with the same properties and methods.It defines the properties and methods common to every object in the collection.Objects are instances of classes.Method: A function defined in a class.Class Variables: Class variables are common throughout the instantiated object.Class variables are defined inside the class and outside the function body.Class variables are generally not used as instance variables.Data members: Class variables or instance variables are used to handle data related to the class and its instance objects.Method overriding: If the method inherited from the parent class cannot meet the needs of the subclass, it can be rewritten. This process is called method override, also known as method overriding.Local variables: Variables defined in a method that only apply to the class of the current instance.Instance variables: In the declaration of a class, attributes are represented by variables, such variables are called instance variables, and an instance variable is a variable modified with self.Inheritance: A derived class inherits the fields and methods of a base class.Inheritance also allows an object of a derived class to be treated as a base class object.For example, there is such a design: an object of type Dog is derived from the Animal class, which simulates the "is-a" relationship (for example, Dog is an Animal).Instantiate: Create an instance of a class, a concrete object of the class.Object: An instance of a data structure defined by a class.Objects include two data members (class variables and instance variables) and methods.
init(constructor)
def init(self):
self.data = []
__init__ can also have parameters
Inside a class, use the def keyword to define a method. Unlike general function definitions, a class method must contain the parameter self, which must be the first parameter. Self represents an instance of the class>.
Double underscore "__" defines private properties (class methods and class properties), which cannot be directly accessed outside the class
Inheritance
Python also supports class inheritance, such as:
class people is a class
If a class named student wants to inherit its internal properties, it can be expressed like this:
class student(people)
Inheritance is also divided into single inheritance and multiple inheritance. The above example is single inheritance
Multiple inheritance is like class modename(base1,base2,base3…)
Pay attention to the order of parent classes in parentheses. If the parent class has the same method name, it is not specified when the child class is used, and the default is from left to leftright.
Method overriding
When the method in the parent class cannot satisfy the subclass, method overriding can be performed.
The super() function is a method used to call the parent class (super class).
class Parent: # Define the parent class
def myMethod(self):
print ('call the parent class method')
class Child(Parent): # define a subclass
def myMethod(self):
print ('call subclass method')
c = Child() # Subclass instance
c.myMethod() # Subclass calls override method
super(Child,c).myMethod() #Calling superclass with subclass object has beenOverriding method
Code reproduced here for rookie tutorial
Class-specific methods
__init__ : constructor, called when the object is generated__del__ : destructor, used when the object is released__repr__ : print, convert__setitem__ : assign value by index__getitem__: get value by index__len__: get the length__cmp__: comparison operation__call__: function call__add__: addition operation__sub__: Subtraction__mul__: Multiplication__truediv__: division operation__mod__: remainder operation__pow__: exponentiation
Operator overloading
Python also supports class overloading.
Reprinted in the rookie tutorial
class Vector:
def init(self, a, b):
self.a = a
self.b = b
def str(self):
return ‘Vector (%d, %d)’ % (self.a, self.b)
def add(self,other):
return Vector(self.a + other.a, self.b + other.b)
v1 = Vector(2,10)
v2 = Vector(5,-2)
print (v1 + v2)
The result is Vector(7,8)
边栏推荐
猜你喜欢
随机推荐
快手“三号人物”的出海漂流
用飞书招聘医生,美中宜和“字节化”?
为什么用了大牌工具后报表开发依然头疼
Adb pull 手机文件到电脑上
Unity 双生ScrollView滑动冲突问题
iptables防火墙内容全解
Analysis of AutoAssign Papers
ALIPAY WEB log in rsa encryption analysis record
scala排序,排序,排序,sortBy sortWith
CTFSHOW_WEB入门web213
防火墙初接触
2020-03-16
按键精灵 删除文件 命令
蚂蚁森林 离线爬虫自动收能量,养小鸡,等各种操作
三国战绩物品序号.txt
ArcPy要素批量转dwg
Button Wizard for ts API usage
SVN Update和Commit执行文件
wsgw登录抓包记录
ALIPAY WEB 登陆rsa加密 分析记录