 博客信息
		博客信息
	封装动物类、猫类、狗类
面向对象:
    1、封装(类的封装)
    2、继承(父类 子类)
        父类(超类,根类,基类)
        子类(派生类)
        继承的特性:
            子类可以继承父类中所有的非私有的属性及方法
         继承的语法:
            class 类(父类):
                pass
    3、多态
不使用单继承描述动物、猫、狗
class Animal():
    def __init__(self,color,age,name,breed,gender):
        self.color = color
        self.age = age
        self.name = name
        self.breed = breed
        self.gender = gender
    def eat(self):
        print("吃")
    def sleep(self):
        print("睡")
class Cat():
    def __init__(self,color,age,name,breed,gender):
        self.color = color
        self.age = age
        self.name = name
        self.breed = breed
        self.gender = gender
    def eat(self):
        print("吃")
    def sleep(self):
        print("睡")
class Dog():
    def __init__(self,color,age,name,breed,gender):
        self.color = color
        self.age = age
        self.name = name
        self.breed = breed
        self.gender = gender
    def eat(self):
        print("吃")
    def sleep(self):
        print("睡")
使用继承关系完成Animal、Cat、Dog三个类
class Animal(object):
    def __init__(self,color,age,name,breed,gender):
        self.color = color
        self.age = age
        self.name = name
        self.breed = breed
        self.gender = gender
    def eat(self):
        print("吃")
    def sleep(self):
        print("睡")
class Cat(Animal):
    pass
class Dog(Animal):
    pass
    def __str__(self):
        return "就不告诉你"
#创建cat对象,验证是否可以继承到父类中的属性以及方法
small_hua_cat = Cat('green',1,'绿巨人','波斯','雌')
print("年龄:%d"%small_hua_cat.age)
small_hua_cat.sleep()
small_hua_cat.eat()
small_hua_dog = Dog('Black',2,'大黄','京巴','雄')
print(small_hua_dog.age)
small_hua_dog.sleep()
small_hua_dog.eat()
print(small_hua_dog)
# print(small_hua_dog.__str__())
# print(small_hua_cat.__str__())
# Python是弱语言,所以哪怕该对象没有这个方法showInfo,编译期也不会报错
# small_hua_cat.showInfo()
idea对比两个文件的差异性



继承:
class Dog(Animal):
    pass
继承语法:
    class  子类(父类1,父类2....):
        pass
多继承:
    一个子类可以同时存在多个父类
    狼狗    狗 狼
    骡子    马 驴
    多继承的前提:方法有什么变化
    如果C(A,B)
        意味C类可以共享A类以及B类中所有的非私有方法
        如果A类与B类中存在同名方法,子类继承的方法取决于父类的顺序(取前边的)
多继承的前提:属性有什么变化
    如果C(A,B)
C类只能继承A类中的非私有属性
能打印类中方法的调用顺序的方式:
类.mro()
多继承中方法的的执行顺序测试如下:
class B():
    def testA(self):
        print("我是B类中的testA方法")
    def testB(self):
        print("我是B中的testB方法")
class A():
    def testA(self):
        print("我是A中的testA方法")
class C(B,A):
    def testA(self):
        print("我是C中testA方法")
    def __testC(self):
        print("我是C中私有的testC方法")
class D(C):
    pass
# c = C()
# c.testA()
# c.testB()
d = D()
d.testA()
#打印方法调用时的查找顺序
str1=D.mro()
# <class 'list'>
print(type(str1))
print("方法的调用顺序:%s"%str1)
#子类对象无法调用父类中的私有方法
# d.__testC()
多继承中属性的处理:
class A():
    def __init__(self,a,aa):
        self.a = a
        self.aa = aa
class B():
    def __init__(self,b):
        self.b = b
class C(A,B):
    def __init__(self,a,b,c):
        super().__init__(a,b)
        self.c = c
#
# c = C(1,2)
# print(c.a)
# print(c.aa)
# print(c.b)
c = C(1,2,3)
# print(c.a)
print(c.a)
print(c.aa)
print(c.c)
print(C.mro())
print(C.__mro__)
方法重写:
    1.什么叫方法重写?如何重写?
    前提: 继承关系,父类中存在某个方法,子类中将该方法重写实现一遍
    overrides
    2.重写有什么好处?
        可以为父类中方法补充新的东西
    3.什么情况下需要重写?
        父类中的方法,不能满足需求的时候,可以使用重写
    4.重写的时候注意事项:
        1.方法名
            方法名必须一致
        2.参数
            参数可以不同
        3.返回值
            与返回值无关
换句话说,只要方法名相同就是方法重写
class Animal(object):
    def __init__(self,color):
        self.color = color
    def eat(self):
        print("吃")
    def shout(self):
        print("动物都会叫")
class Dog(Animal):
    def __init__(self,name,age,color):
        # 如果使用super的话,子类中只能使用父类继承的color
        super().__init__(color)
        #父类中有一个color,子类中重写写了一个color
        # self.color = color
        self.name = name
        self.age = age
    def eat(self):
        print("吃除了吃肉还吃骨头")
    def shout(self,a,c):
        #调用父类的方法
        super().shout()
        # super(Dog, self).shout()s
        print("汪汪汪~")
        return 123
dog = Dog('Big Yellow',3,'Yellow')
dog.eat()
dog.shout(1,2)
print(dog.color)
an = Animal('Black')
print(an.color)
print(dog.color)
over......
 
                 按日志类别
                    按日志类别
                 按日志日期
                    按日志日期
                备案号:湘ICP备19000029号
Copyright © 2018-2019 javaxl晓码阁 版权所有