类与类的组合

2019-02-27  本文已影响0人  weijiaping
方式一:定义时的组合,实现一个类调用另一个类的属性,达到代码重用的目的;
方式二:传参时的组合,实现一个类调用另一个类的属性,达到代码重用的目的; 原理与方式一样的!!!
方式三:不同对象的交互,实现一个类的不同对象的属性的互换;(这个方式有点绕哦)
# 方式一:定义时的组合,实现一个类调用另一个类的属性,达到代码重用的目的
class BirthDate:
    def __init__(self, year, month, day):
        self.year = year
        self.month = month
        self.day = day
class Person:
    def __init__(self, name, year, month, day):      # 定义这个类的属性变量必须传进来的参数(类BirthDat的参数)完全对应
        self.name = name
        self.birthdate = BirthDate(year, month, day) # 定义类的实例对象属性变量ot,让其值等于上面定义的BirthDate类,形成类与类的组合;
                                                     # 这里的birthdate相当于[year, month, day],一次传三个参数,也一次传一个参数,传三次

P = Person('Lyon', 2000, 1, 1)
print(f"{P.name}'s birthdate is {P.birthdate.year} year {P.birthdate.month} munth {P.birthdate.day} day!")
# 相当于以下语句
B = BirthDate(2000, 1, 1)
print(f"{P.name}'s berthdate is {B.year} year {B.month} munth {B.day} day!")  # 这里调用了两个对象P和B

# 方式二:传参时的组合,实现一个类调用另一个类的属性,达到代码重用的目的

class BirthDate:
    def __init__(self, year, month, day): # 定义类的实例对象的属性参数,有以下三个参数
        self.year = year
        self.month = month
        self.day = day

class Person:     # 定义类的实例对象的属性参数,有以下二个参数
    def __init__(self, name, birthdate):
        self.name = name
        self.birthdate = birthdate

p = Person('Lyon', BirthDate(2002, 2, 2)) # 在实例化传参时,将BirthDate类及参数BirthDate(2000, 1, 1)传给Person类的实例对象的birthdate参数
print(f"{p.name}'s birthdate is {p.birthdate.year} year {p.birthdate.month} munth {p.birthdate.day} day!")
# 相当于以下语句
B = BirthDate(2002, 2, 2)
print("{}'s birthdate is {} year {} munth {} day!".format(p.name, B.year, B.month, B.day))  # 这里调用了两个对象P和B

# 方式三:不同对象的交互,实现一个类的不同对象的属性的互换(这个方式有点绕哦)

class Person:  # 定义的一个Person类
    def __init__(self, name):  # 定义Person类实例属性
        self.name = name       # Person类的实例对象有一个属性参数name

    def attack(self,per):      # 定义Person类的方法,该方法的实例对象有一个per参数
                               # 注意:这个参数必须是一个带有name属性的类的对象,不然下面的per.name就是错的
        print(f"{self.name} attacked {per.name}!")   # 这里调用了参数per的name属性

lyon = Person("Lyon")          # 实例化,创建lyon对象
kenneth = Person("kenneth")    # 实例化,创建kenneth对象
lyon.attack(kenneth) # 对象交互:调用lyon对象的attach方法,并给这个方法传入一个对象kenneth,
# 执行 print(f"{self.name} attacked {per.name}")时,相当于下面语句!
print(f"{lyon.name} attacked {kenneth.name}!")

#下面我们换一种写实现上面的例子

class Person:     # 定义类的实例对象的属性参数,有以下二个参数
    def __init__(self, name, year, month, day):
        self.name = name
        self.year = year
        self.month = month
        self.day = day

    def BirthDate (self, Bth): # 定义Person类的方法,该方法的实例对象有一个per参数
                               # 注意:这个参数必须是一个带有year/month/day属性的类的对象,不然下面的语句就是错的
                               # 这个方法功能是:将对象的属性与其参数Bth(另一个对象)给对应起来,实现了对象之的数据互换
        print(f"{self.name}'s birthdate is {Bth.year} year {Bth.month} munth {Bth.day} day!")

Lyon1 = Person("Lyon1", 2004, 4, 4)
Lyon2 = Person("Lyon2", 2003, 3, 3)
Lyon1.BirthDate(Lyon2)
Lyon2.BirthDate(Lyon1)

# 有点绕,其实也通过下面的语句实现相同的效果:
print(f"{Lyon1.name}'s birthdate is {Lyon2.year} year {Lyon2.month} munth {Lyon2.day} day!")
print(f"{Lyon2.name}'s birthdate is {Lyon1.year} year {Lyon1.month} munth {Lyon1.day} day!")

上一篇 下一篇

猜你喜欢

热点阅读