程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[python learning 11] classes and objects in depth again

編輯:Python

                      *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,scorestudent  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()

(四)object類

__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 

1,特殊屬性

類對象的__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

2,特殊方法

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)

(1)__new__與__init__演示創建對象的過程

①方框中的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


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved