*7、類的賦值、淺拷貝、深拷貝
self.__age=age: self.__age前面有兩個"_",Describe this propertyCannot be accessed outside the class object
in the initialization method in the subclass super().__init__(name,age) It is to call the initialization method of the parent class to assign a value to the property,score 是 student own properties in the class,使用self進行賦值
class Person:
def __init__(self,name,age):
self.name=name
self.age=age
def info(self):
print(self.name,self.age)
#定義子類
class Student(Person):
def __init__(self,name,age,score):
super().__init__(name,age) #Call the initialization method of the parent class toname,age賦值
self.score=score #scoreis its own property in the class,故用self賦值
class Teacher(Person):
def __init__(self,name,age,teacheryear):
super().__init__(name,age) #Call the initialization method of the parent class toname,age賦值
self.teacheryear=teacheryear #teacheryearis its own property in the class,故用self賦值
stu=Student("張三",18,95)
teacher=Teacher("王梅",32,8)
stu.info()
teacher.info()
python 中也可以實現 多繼承
class A(object):
pass
class B(object):
pass
class C(A,B): #C類同時繼承了A類,B類
pass
方法重寫:①Methods can be rewritten in subclasses
②Overridden methods are available in subclasses super().xx()Call the original method in the parent class (The same as the initialization method in the subclasssuper().__init__(name,age)statement calls the initialization method in the parent class)
③An arrow display appears when overriding a methodoverrides method in object
class Person: #父類
def __init__(self,name,age):
self.name=name
self.age=age
def info(self):
print(self.name,self.age)
#定義子類
class Student(Person):
def __init__(self,name,age,score):
super().__init__(name,age) #Call the initialization method of the parent class toname,age賦值
self.score=score #scoreis its own property in the class,故用self賦值
def info(self):
super().info() #Override in subclassesinfo方法,If you still want to use the method of the parent class to writesuper().info()
print(self.score) #A statement added after an overridden method
class Teacher(Person):
def __init__(self,name,age,teacheryear):
super().__init__(name,age) #Call the initialization method of the parent class toname,age賦值
self.teacheryear=teacheryear #teacheryearis its own property in the class,故用self賦值
stu=Student("張三",18,95)
teacher=Teacher("王梅",32,8)
stu.info()
teacher.info()
__str__()I don't understand the method
After writing the class,通常對 __str__()方法進行重寫
class Student: #Inherit if not specifiedobject類
pass
stu=Student()
print(dir(stu)) #內置函數dir()View all properties of the specified object
class Animal(object):
def eat(self):
print("動物會吃")
class Dog(Animal):
def eat(self):
print("狗吃骨頭")
class Cat(Animal):
def eat(self):
print("貓吃魚")
class Person(object):
def eat(self):
print("people eat anything")
def fun(obj):
obj.eat()
fun(Cat())
fun(Dog())
fun(Animal())
fun(Person()) #不用關心PersonWho's subclass of when,只關心Person是否具有eat方法
Don't care about the above code Person 是誰的子類,只關心Person是否具有eat方法
JAVA是靜態語言,Python是動態語言
以兩個下劃線"__"start and two underscores"__"End property or method,is a special property or method
類對象的__dict__屬性Include all of the classA dictionary of properties and methods,實例對象的__dict__屬性Include all of the instance object屬性的字典
print(x.__class__) #屬性__class__,輸出實例對象x所屬的類
print(C.__bases__) #屬性__bases__,輸出CMultiple parent classes that a class inherits and place the parent classes in a tuple
print(C.__mro__) #屬性__mro__,Output to view the class hierarchy,That is, the subclass parent class and the grandparent class
print(A.__subclasses__) #屬性__subclasses__,查看ASubclass the class and put the subclass in the list
If you want to add some properties of two instance objects,It needs to be overridden in the class__add__()方法(Understand the corresponding content in the table)
If you want to count the length of some properties of the instance object,It needs to be overridden in the class__len__()方法(Understand the corresponding content in the table)
①方框中的9360,7104是輸出的地址;箭頭表示傳參的過程
②由上圖可知先用__new__創建對象,再用__init__初始化對象
③上圖__new__方法中用了super().__new__(cls),使用父類objectThe method of creating an object in a class is passed as a parametercls
淺拷貝:Subobject refers to Instance objects of other classes included in this source object,The source object and the copy object refer to the same one
子對象
深拷貝:The child objects contained in the object are copied,All child objects of the source object and the copy object are not the same