当前位置:网站首页>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__: exponentiationOperator 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)
边栏推荐
猜你喜欢
随机推荐
论网络安全的重要性
Upload-labs Pass-02(MIME验证)
Matlab to download
蚂蚁森林 离线爬虫自动收能量,养小鸡,等各种操作
抖音开启“818发现好物节”:电商平台造节活动何时休
BSV 上高效的多方公平交易
2020-03-16
记录每天学习的新知识: Room
SVN Update和Commit执行文件
新安装Laravel Framework 6.18.35 php artisan migrate 报错
C drive space management
UGUI性能优化
Cesium快速上手3-Billboard/Label/PointPrimitives图元使用讲解
Unity BMFont自定义字体
面试常问问题之网络整体传输过程
Unity Text值递增或递减效果
影响你各应用间网速的QoS你了解吗?
同花顺的炒股软件买股票安全正规可信吗?
主机测探与端口扫描
九大内置对象,四大作用域

![17 [2D conversion 3D conversion browser private prefix]](/img/d0/2b310e6b6b375d457f339a6a9d71e1.png)







