Python实践-咚咚呛讲师Python进阶教程

2.2请练习定义一个动物类,并创建出两个实例dog, cat,打印实例,再比较两个实例是否相等
class Animal(object):
    pass
dog = Animal()
cat = Animal()
print(cat)
print(dog==cat)
<__main__.Animal object at 0x00000189935BD280>
False
2.3请定义一个动物类,并创建出两个实例dog, cat,分别赋予不同的名字和年龄。
class Animal(object):
    pass
dog = Animal()
cat = Animal()
dog.name = 'xiaohuang'
dog.age = 1
cat.name = 'xiaohua'
cat.age = 2
2.4请定义一个动物类,抽象出名字、年龄两个属性,并实例化两个实例dog, cat。
class Animal(object):
    def __init__(self,name,age):
        self.name = name
        self.age = age
dog = Animal('xiaohuang',3)
cat = Animal('xiaohua',4)
print(dog.name,dog.age)
print(cat.name,cat.age)
xiaohuang 3
xiaohua 4
2.5请给 Animal类添加一个类属性 count,每创建一个实例,count 属性就加 1,这样就可以统计出一共创建了多少个 Animal的实例。
class Animal(object):
    location = 'China'
    count = 0
    def __init__(self,name,age):
        self.name = name
        self.age = age
        Animal.count += 1

dog = Animal('xiaohuang',4)
cat = Animal('xiaohua',3)
pig = Animal('xiaozhu',6)
print(dog.name,dog.age)
print(cat.name,cat.age)
print(pig.name,pig.age)
print(Animal.count)
xiaohuang 4
xiaohua 3
xiaozhu 6
3
2.6请把上节的 Animal类属性 count 改为 __count,再试试能否从实例和类访问该属性。
class Animal(object):

    __count = 0

    def __init__(self,name):

        Animal.__count = Animal.__count + 1

        self.name = name

        print('内部:{}'.format(Animal.__count))

p1 = Animal('Cat')

p2 = Animal('Dog')
内部:1
内部:2
2.7请给Animal类的__init__方法中添加name和age参数,并把age绑定到__age属性上,看看外部是否能访问到。(很显然访问不到,程序报错)
class Animal(object):
    def __init__(self,name,age):
        self.name = name
        self.__age = age

dog = Animal('xiaohuang',12)
print(dog.name,dog.__age)
---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

Input In [72], in <cell line: 7>()
      4         self.__age = age
      6 dog = Animal('xiaohuang',12)
----> 7 print(dog.name,dog.__age)


AttributeError: 'Animal' object has no attribute '__age'
2.8把Animal类的age、name、localtion定义成私有属性,并定义对应的方法修改和获取他们的值。
class Animal(object):
    def __init__(self,name,age,location):
        self.__name = name
        self.__age = age
        self.__location = location
        
    def set_name(self,name):
        self._name = name
        
    def get_name(self):
        return self.__name
        
    def set_age(self,age):
        self._age = age
        
    def get_age(self):
        return self._age
        
    def set_location(self,location):
        self_location = location
        
    def get_location(self):
        return self._location
        
dog = Animal('xiaohuang',2,'GuangDong')
print(dog.get_name())
dog.set_age(5)
print(dog.get_age())
xiaohuang
5
2.9如果将类属性count改为私有属性__count,则外部无法获取__count,但是可以通过一个类方法获取,请编写类方法获得__count值。
class Animal(object):
    __location = 'Asia'
    __count = 0
    def __init__(self,name,age):
        self.name = name
        self.age = age
        Animal.__count += 1
        
    @classmethod
    def set_
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值