Python面向对象

class Cat:
    """
    猫科动物类
    """
    # 类的属性
    tag = 'Cat base'

    def __init__(self, name):
        # 实例化后的属性
        self.name = name
        pass

    def eat(self):
        """
        吃
        :return:
        """
        pass


class Tiger(Cat):
    pass


print(dir(Cat))
print('+' * 50)
help(Cat)
print('+' * 50)
print(Cat.__module__)
print('+' * 50)
print(Cat.__class__)
print(Cat.__bases__)
print(Tiger.__bases__)

class Cat(object):
    """
    猫科动物
    """
    tag = '我是家猫'

    def __init__(self, name, age, sex=None):
        self.name = name
        self.__age = age
        self.sex = sex

    def set_age(self, age):
        """
        改变猫的年龄
        :param age:int 年龄
        :return: 更改后的年龄
        """
        self.__age = age
        # return self.__age

    def show_info(self):
        """
        显示猫的信息
        :return:
        """
        rest = '我叫:{0},今年{1}岁'.format(self.name, self.__age)
        print('我的性别:{0}'.format(self.sex))
        print(rest)

        return rest

    def eat(self):
        """吃"""
        print('猫喜欢吃鱼')

    def catch(self):
        """猫捉老鼠"""
        print("猫捉老鼠")


print(dir(Cat))
print('*' * 80)

if __name__ == '__main__':
    # 实例化你家的小黑
    cat_black = Cat('小黑', '2', '公的')
    cat_black.eat()
    cat_black.show_info()
    print('-------------------------')
    print(cat_black.name)
    #print(cat_black.age)私有
    cat_black.name = '嘿嘿' #可以直接改变
    cat_black.__age = 6 #无法操作私有变量
    cat_black.show_info()

    print('-' * 80)
    cat_black.set_age(7)
    cat_black.show_info()

    print(Cat.tag)
    print(cat_black.tag)

    # 实例化我家的小白
    cat_white = Cat('小白', 3 , '母的')
    cat_white.show_info()
    print(cat_white.tag)
    #类的实例判断
    print(isinstance(cat_white,Cat))
    print(isinstance(cat_black,Cat))
"""
猫科动物的细化
           猫科动物BaseCat
 tiger        panda           petcat
                       DuanCat      HuaCat
"""


class ProtectedMixin():
    """受保护的类"""

    def protected(self):
        print('我是受省级保护的')


class CountryProtectedMixin():
    def protected(self):
        print('我是受国家级别保护的')


class BaseCat(object):
    """
    猫科动物的基础类
    """
    tag = '猫科动物'

    def __init__(self, name):
        self.name = name  # 猫都有名称

    def eat(self):
        """猫吃东西"""
        print('猫都要吃东西')

    def eat2(self):
        print("我是爷爷")


class Tiger(BaseCat, CountryProtectedMixin):
    """
    老虎类 也是猫科动物
    """

    def eat(self):
        super(Tiger, self).eat()
        print("我还喜欢吃大猪肉")


class Panda(BaseCat, ProtectedMixin):
    """
    熊猫类 也是猫科动物
    """
    # def protected(self):
    #     print("我是受国家保护的")
    pass


class PetCat(BaseCat):
    """
    家猫类
    """

    def eat(self):
        # 调用父类的方法
        super(PetCat, self).eat()
        print('我还喜欢吃猫粮')


class HuaCat(PetCat):
    """
    中华田园猫
    """

    def eat(self):
        # 调用父类的方法
        super(HuaCat, self).eat()
        print('我还喜欢吃零食')


class DuanCat(PetCat):
    """
    英国短毛
    """
    pass
    # def eat(self):

    # super(DuanCat,self).eat()
    ##print('我啥都吃')


if __name__ == '__main__':
    # 实例化中华田园猫
    # cat = HuaCat('小黄')
    # cat.eat()
    # print('*' * 50)
    # # 实例化英国短毛猫
    # cat_d = DuanCat('小辉')
    # cat_d.eat()
    # cat_d.eat2()
    #
    # # 子类的判断
    # print(issubclass(DuanCat, BaseCat))
    # print(issubclass(DuanCat, PetCat))
    # print(issubclass(DuanCat, Tiger))

    panda = Panda('卧龙小熊猫')
    panda.eat()
    panda.protected()

    tiger = Tiger('华南虎')
    tiger.protected()
    print('---------')
    # 验证子类信息
    print(issubclass(Panda, ProtectedMixin))
    print(issubclass(Panda, BaseCat))

@property:将类的方法当做属性来使用

class PetCat(object):
    """家猫类"""

    def __init__(self, name, age):
        """
        构造方法
        :param name:猫的名称
        :param age: 猫的年龄
        """
        self.name = name
        # 私有属性,不能给你们操作
        self.__age = age

    @property
    def age(self):
        return self.__age

    @age.setter
    def age(self, value):
        if not isinstance(value, int):
            print('年龄只能是整数')
            return 0
        if value < 0 or value > 100:
            print('年龄只能位于0-100之间')
            return 0
        self.__age = value

    # 描述符
    @property
    def show_info(self):
        """显示猫的信息"""
        return '我叫:{0},今年{1}岁'.format(self.name, self.age)

    def __str__(self):
        return '我的对象:{0}'.format(self.name)


if __name__ == '__main__':
    cat_black = PetCat('小黑', 2)
    rest = cat_black.show_info
    print(rest)
    print(cat_black)
    # 改变猫的age
    cat_black.age = 'hello'
    rest = cat_black.show_info
    print(rest)
# >>> print('{} {}'.format('hello','world'))  # 不带字段
# hello world
# >>> print('{0} {1}'.format('hello','world'))  # 带数字编号
# hello world
# >>> print('{0} {1} {0}'.format('hello','world'))  # 打乱顺序
# hello world hello
# >>> print('{1} {1} {0}'.format('hello','world'))
# world world hello
# >>> print('{a} {tom} {a}'.format(tom='hello',a='world'))  # 带关键字
# world hello world

slots:

  • 为指定的类设置一个静态属性列表
  • 为属性很少的类节约内存空间
class PetCat(object):
    """家猫类"""
    __slots__ = ('name', 'age')

    def __init__(self, name, age):
        """
        构造方法
        :param name:猫的名称
        :param age: 猫的年龄
        """
        self.name = name
        # 私有属性,不能给你们操作
        self.age = age

    # 描述符
    @property
    def show_info(self):
        """显示猫的信息"""
        return '我叫:{0},今年{1}岁'.format(self.name, self.age)

    def __str__(self):
        return '我的对象:{0}'.format(self.name)


class HuaCat(PetCat):
    """中华田园猫"""
    __slots__ = ('color', )
    pass


def eat():
    print('我喜欢吃鱼')


if __name__ == '__main__':
    # cat_black = PetCat('小黑', 2)
    # rest = cat_black.show_info
    # print(rest)
    # # 给实例添加新的属性
    # cat_black.clor = '白色'
    # print(cat_black.clor)
    # # 给实例添加新的方法(函数)
    # cat_black.eat = eat
    # cat_black.eat()

    # 使用slots后不允许给给实例添加新的属性
    # cat_black.clor = '白色'
    # print(cat_black.clor)
    # 使用slots后给实例添加新的方法(函数)
    # cat_black.eat = eat
    # cat_black.eat()
    cat_white = HuaCat('小白', 3)
    rest = cat_white.show_info
    print(rest)
    cat_white.color = '白色'
    print(cat_white.color)
    cat_white.name = '旺旺'
    print(cat_white.show_info)

类的静态方法和实例方法
@staticmethod 表示静态方法
@classmethod 表示类方法

class Cat(object):
    tag = '猫科动物'

    def __init__(self, name):
        self.name = name

    @staticmethod
    def breath():
        """呼吸"""
        print('猫都需要呼吸')

    @classmethod
    def show_info(cls, name):
        """显示猫的信息"""
        # print('类的属性:{0},实例的属性:{1}'.format(cls.tag, cls.name))
        return cls(name)  # cat = Cat(name) #return cat

    def show_info2(self):
        """显示猫的信息"""
        print('类的属性:{0},实例的属性:{1}'.format(self.tag, self.name))


if __name__ == '__main__':
    # 通过类进行调用
    Cat.breath()
    cat = Cat("小包")
    # 通过类的实例进行调用
    cat.breath()
    cat.show_info2()

    # 调用classMethod
    cat2 = Cat.show_info('小黄')
    cat2.show_info2()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值