1 import copy 2 class Dog: 3 def Eat(self): 4 print("Dog Eating...") 5 class Son: 6 def __init__(self): 7 self.height=100 8 def Eat(self): 9 print("Son Eating...") 10 class Father: 11 def LetSonEat(self,m): 12 if(isinstance(m,Son)): 13 m.Eat() 14 m.height=120 15 else: 16 print("You are not my son") 17 son=Son() 18 dog=Dog() 19 son2=copy.copy(son) 20 father=Father() 21 father.LetSonEat(son) 22 father.LetSonEat(dog) 23 print(son.height) 24 print(son2.height)
这是一个父类对象对子类对象的调用演示。“Son”对象数据可以采用引用及复制模式(copy)
原文地址:https://www.cnblogs.com/Exesoft-Mike/p/9247828.html
时间: 2024-10-08 02:42:46