第八章第4讲:

1.封装(回顾)

class Person:
    # 定义基本属性
    name = ""
    age = 0
    __weight = 0 # 私有变量
    # 定义构造方法,初始化
    def __init__(self,n,a,w):
        self.name = n
        self.age = a
        self.__weight = w
    def speak(self):
        return "%s 说:我今年%d岁,%d斤。" % (self.name,self.age,self.__weight)
p = Person("Ann",24,100)
print(p.speak())
print(p.name)
print(p.age)
print(p.__weight) # 私有变量是无法直接通过对象查看的

 2.继承:从基类或父类继承相关的属性或方法

# 继承
class Animal:
    def run(self):
        return "Animal is running."
class Cat(Animal):
    pass
class Dog(Animal):
    pass
fcat = Cat()
fdog = Dog()
print(fcat.run())
print(fdog.run())

继承案例:

class Person:
    name = ""
    age = 0
    # 私有变量
    __weight = 0
    # 构造函数,勇于实例化时要初始化
    def __init__(self,n,a,w):
        self.name = n
        self.age = a
        self.__weight = w
    def speak(self):
        print("%s说:今年%d岁。" % (self.name,self.age))
# Student类继承Person类
class Student(Person):
    # Student类增加了新属性grade
    grade = ""
    def __init__(self,n,a,w,g):
        Person.__init__(self,n,a,w)
        self.grade = g
    def speak(self):
        print("%s说:今年%d岁,在读%s年级。" % (self.name,self.age,self.grade))
s = Student("Ann",10,30,3)
print(s.speak())

结果:
Ann说:今年10岁,在读3年级。
None

多继承:如果多个父类中有同样的方法,从左往右检索类,看是否包含所调用的方法,首先检索到的被调用

案例1:

# 多继承
class Animal:
    def run(self):
        return "Animal is running."
class Cat():
    def call(self):
        return "喵喵喵"
class BosiCat(Animal,Cat):
    # 代码桩
    pass
foo = BosiCat()
print(foo.run())
print(foo.call())

结果:
Animal is running.
喵喵喵

案例2:

class Person:
    name = ""
    age = 0
    # 私有变量
    __weight = 0
    # 构造函数,勇于实例化时要初始化
    def __init__(self,n,a,w):
        self.name = n
        self.age = a
        self.__weight = w
    def speak(self):
        print("%s说:今年%d岁。" % (self.name,self.age))
# Student类继承Person类
class Student(Person):
    # Student类增加了新属性grade
    grade = ""
    def __init__(self,n,a,w,g):
        Person.__init__(self,n,a,w)
        self.grade = g
    def speak(self):
        print("%s说:今年%d岁,在读%s年级。" % (self.name,self.age,self.grade))

class Speaker():
    topic = ''
    name = ''
    def __init__(self,t,n):
        self.topic = t
        self.name = n
    def speak(self):
        print("我叫%s,我是个演说家,演讲的主题是%s。" % (self.name,self.topic))
class Sample(Speaker,Student):
    a = ''
    def __init__(self,n,a,w,g,t):
        Student.__init__(self,n,a,w,g)
        Speaker.__init__(self,t,n)
test = Sample("Ann",24,50,7,"Python")
print(test.speak())

结果:
我叫Ann,我是个演说家,演讲的主题是Python。
None

3.多态:不同类(对象)有同样的方法,但是方法的行为不同

扫描二维码关注公众号,回复: 6853699 查看本文章
# 多态
class Animal:
    def run(self):
        return "Animal is running."
class Cat(Animal):
    def run(self):
        return "Cat is running."
class Dog(Animal):
    def run(self):
        return "Dog is running."
class Dog1(Animal):
    pass

fcat = Cat()
fdog = Dog()
fdog1 = Dog1()
print(fcat.run())
print(fdog.run())
print(fdog1.run())

结果:
Cat is running.
Dog is running.
Animal is running.

案例:

# 多态
class Animal:
    def run(self):
        print("Animal is running.")
class Cat(Animal):
    def run(self):
        print("Cat is running.")
class Dog(Animal):
    def run(self):
        print("Dog is running.")
class Dog1(Animal):
    pass

# lst = [Animal(),Cat(),Dog()]
# for item in lst:
#     print(item.run())
def run_two(animal):
    animal.run()
    animal.run()
#不必对run_two做任何修改,任何依赖animal作为参数的函数或方法都可以不加修改的正常运行,这就是多态
print(run_two(Animal()))
print(run_two(Cat()))
print(run_two(Dog()))

猜你喜欢

转载自www.cnblogs.com/ling07/p/11235566.html