Python3 learning object-oriented

@Object-Oriented
Start blogging, start taking notes

One, the creation of the class

1, the definition of the class

class People:
	country = 'China' 		#类变量
	def __init__(self,name,sex,age,height ):#构造函数
		self.name = name	#实例变量(从C++角度,类似于数据成员)
		self.sex = sex
		self.age = age
		self.__height = height #私有属性
	
	def printInfo(self):		#方法(从C++角度,类似于成员函数)
		print("%s is a %s,from %s"%(self.name,self.sex,self.country))

	def __myPrivacy(self):#私有方法
		print("This is my privacy!")

2,self

The parameter self points to the object itself and represents an instance of the class. It is indispensable. You can name this parameter casually. It is customarily self.

3. Class variables

Class variables are shared by all instances, directly access the available class name. Class variables.
Instance variable, exclusive to this instance.

Class variable access

When accessing a class variable, first access the instance variable. If the instance variable does not exist, access the class variable. If the instance variable exists, the variables accessed by the following method 1 and method 2 are all instance variables.
Method 1: The access method of the class variable in the class is self.country;
Method 2: The access method outside the class is p1.country;
Method 3: The direct access method is People.country.

#若country的实例变量不存在
p1 = People("Zhangsan","girl",22,160)
p1.printInfo()
print("from:",p1.country)
print("country:",People.country)
Zhangsan is a girl,from China
from: China
country: China
#若实例变量存在
class People:
	country = 'China'   #类变量
	 def __init__(self,name,sex,age):#构造函数
		  self.name = name #实例变量
		  self.sex = sex
		  self.age = age
		  self.country = 'Ch'
		 
	 def printInfo(self):  #方法
 		print('%s is a %s,from %s'%(self.name,self.sex,self.country))

p1 = People("Zhangsan","girl",22)
p1.printInfo()
print("from:",p1.country)
print("country:",People.country)
Zhangsan is a girl,from Ch
from: Ch
country: China

Modification of class variables

When modifying a class variable, you can use People.country to directly assign a value. When an instance modifies a class variable, p1.country ='A', which means that the instance variable is added in this instance space without modifying the value of the class variable. If the instance needs to modify the class variable, p1.__class__.country ='A'.

Second, the inheritance of the class

class A0:
	def __init__(self):
		print("init A0")
	
	def printName(self):
		print("class name:A0")

class A1:
	def __init__(self):
		print("init A1")
		
	def printName(self):
 		print("class name:A1")

		
class B(A0):    #单继承
	def __init__(self):
		super().__init__()
		print("init B")
		
	#def printName(self):
		#print("class name:B")

class C(A0,A1):    #多重继承
	def __init__(self):
		A0.__init__(self)
		A1.__init__(self)
		print("init C")

	def printName(self):
 		 print("class name:C")

Multiple inheritance

1. The constructor __init__()

The constructor is used for instance initialization. In inheritance, the constructor of the subclass must call the constructor of its parent to ensure basic initialization. There are two methods:
1. Single inheritance can be called as follows, the following takes the constructor in class B as an example.
super().__init__() (super(B,self).__init__() in the old class)
A0.__init__(self) (calling the unrelated superclass constructor)
2. In multiple inheritance, the following is called, taking class C as example.
super(C,self).__init__()
super(A0,self).__init__()

A0.__init__(self)
A1.__init__(self)
If you need to initialize the parameters of the parent class, they are all in the init of the called parent class.

执行
c = C()
结果
init A0
init A1
init C

2. Method call

If multiple parent classes implement the same method in different ways, and the subclass does not implement this method and does not specify the method of the parent class, then from left to right find whether the parent class contains a method, and the method of the previous class is Override the method of the class behind.

#运行
c = C()
c.printName()
b = B()
b.printName()

#结果
init A0
init A1
init C
class name:C
init A0
init B
class name:A0

3. Some other methods

1. To determine whether a class is a subclass of another class, you can use the built-in method issubclass.

issubclass(childrenClass,basesClass),返回True或者False。

2. To access the base class of a class, you can access its special attribute __bases__.

childrenClass.__bases__

3. To determine whether an object is an instance of a specific class, you can use isinstance.

p = People()
isinstance(p,People)
返回True或者False

You can also use isinstance(a,int) to view the data type.
4. Get which class the object belongs to, the available attribute __class__.

p.__class__。

Guess you like

Origin blog.csdn.net/Lhhxxdhr/article/details/106551065