Kế thừa nhiều cấp

Nguyên tắc Kỹ thuật Phần mềm với Python

Adam Spannbauer

Machine Learning Engineer at Eastman

Tạo lớp Tweet

  Tệp lớp Tweet

Nguyên tắc Kỹ thuật Phần mềm với Python

Kế thừa nhiều cấp

Cây gia hệ kế thừa

Nguyên tắc Kỹ thuật Phần mềm với Python

Kế thừa nhiều cấp

Cây gia hệ kế thừa nhiều cấp

Nguyên tắc Kỹ thuật Phần mềm với Python

Đa kế thừa

Cây gia hệ đa kế thừa

Nguyên tắc Kỹ thuật Phần mềm với Python

Kế thừa nhiều cấp và super

class Parent:
    def __init__(self):
        print("I'm a parent!")


class Child(Parent):
    def __init__(self):
        Parent.__init__()
        print("I'm a child!")

class SuperChild(Child): def __init__(self): super().__init__() print("I'm a super child!")

Tìm hiểu thêm về đa kế thừa và super().

Nguyên tắc Kỹ thuật Phần mềm với Python

Kế thừa nhiều cấp và super

class Parent:
    def __init__(self):
        print("I'm a parent!")

class SuperChild(Parent):
    def __init__(self):
        super().__init__()
        print("I'm a super child!")

class Grandchild(SuperChild): def __init__(self): super().__init__() print("I'm a grandchild!")
grandchild = Grandchild()
I'm a parent!
I'm a super child!
I'm a grandchild!
Nguyên tắc Kỹ thuật Phần mềm với Python

Theo dõi thuộc tính được kế thừa

# Create an instance of SocialMedia
sm = SocialMedia('@DataCamp #DataScience #Python #sklearn')

# What methods does sm have? ¯\_(ツ)_/¯ dir(sm)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', 
'__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', 
'__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', 
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', 
'__str__', '__subclasshook__', '__weakref__', '_count_hashtags', 
'_count_mentions', '_count_words', '_tokenize', 'hashtag_counts', 
'mention_counts', 'text', 'tokens', 'word_counts']
Nguyên tắc Kỹ thuật Phần mềm với Python

Let's Practice

Nguyên tắc Kỹ thuật Phần mềm với Python

Preparing Video For Download...