2018-09-07-day15-类和对象

2018-09-07  本文已影响0人  rzlong

内置类属性

内置类属性就是魔法属性

魔法属性: 属性名前后都有两个下划线

魔法方法: 方法的前后都有两个下划线


import datetime
class Person:
    """这里是类的说明文档"""
    def __init__(self,name,age):
        self.name = name
        self.age = age

    @classmethod
    def work(cls):
        print('人类,瓦斯塔拉,我们艾欧尼亚人')
    @staticmethod
    def destroy():
        print('无形之刃最为致命')

# 1. __name__ 属性 -- 得到类的名字(字符串)
    name = Person.__name__
    print(name,type(name))

result:
Person <class 'str'>
# 2.__doc__ 属性 --得到类的说明文档
    doc = Person.__doc__
    print(doc,type(doc))

result:
这里是类的说明文档 <class 'str'>
# 3.__class__ 属性 --获取对象对应的类,返回的是一个类型
    class2 = lv.__class__
    lm = class2('Yasuo',21)
    print(class2,type(class2),lm.name,lm)

result:
<class '__main__.Person'> <class 'type'> Yasuo <__main__.Person object at 0x008B6F70>
 # 4.__dict__属性 -- 将对象和类的属性及其对应的值转换成键值对存到字典中
    dict2 = Person.__dict__
    print(dict2)
    dict3 = lv.__dict__
    print(type(dict3),dict3)

result:
{'__module__': '__main__', '__doc__': '这里是类的说明文档', '__init__': <function Person.__init__ at 0x026B5738>, 'work': <classmethod object at 0x007EC450>, 'destroy': <staticmethod object at 0x02686E30>, '__dict__': <attribute '__dict__' of 'Person' objects>, '__weakref__': <attribute '__weakref__' of 'Person' objects>}
<class 'dict'> {'name': 'Zed', 'age': 20}
 # 5.__module__属性 -- 获取类所在模块对应的名字
    print(Person.__module__)
    print(datetime.datetime.__module__)

result:
__main__
datetime
 # 6.__bases__属性  --获取父类的名字
    print(Person.__bases__)

result:
(<class 'object'>,)

slots属性

class Person:
    __slots__ = ('name','face') #根据__slots__中存的值来限制对象的属性,不含的属性不能声明使用或添加
    def __init__(self,name,face):
        self.name = name
        self.face = face


if __name__ == '__main__':
    # 1.一旦在类中给__slots__属性赋值,那么在这个类的对象就不能使用__dict__属性
    print(Person.__dict__)
    lv = Person('KKK','face')
    print(lv.__dict__)#此句会报错

属性私有化

python中并没有真正的私有化
python中默认的属性和方法都是公开的

class Dog:
    variety = '秋田'
    __count = '私有count'
    def __init__(self):
        self.color = 'yellow'
        self.age = 3
        self.name = '大黄狗'

    def eat(self):
        print('%s都吃屎' % self.name)

    @classmethod
    def shout(cls):
        print(cls.__count)
        # cls.__eat()
        print('汪汪汪汪~')
        
    @staticmethod
    def func():
        print('咬人,可疼可疼了')
        
    @staticmethod
    def __eat(cls):
        print('下划线下划线吃便')

if __name__ == '__main__':
    big_yellow = Dog()
    Dog.shout() #在类的里面可以使用类的私有属性
    # print(Dog.__count)#在类的外面不能直接是私有的属性
    # big_yellow.__eat()
    print(Dog._Dog__count)
    Dog._Dog__eat(Dog)
result:
私有count
汪汪汪汪~
私有count
下划线下划线吃便

保护类型的getattr和setattr

class Car:
    def __init__(self):
        self.color = 'yellow'
        self.type = 'bicycle'
        # 保护类型
        self._price = 20000

    # 给 _price 属性添加getter
    @property
    def price(self):
        return str(self._price/1000)+'K'

    #想要给一个属性添加setter,需要先有getter
    @price.setter
    def price(self,price):
        if isinstance(price,int) or isinstance(price,float):
            self._price = price
        else:
            self._price = 0

if __name__ == '__main__':
    car =Car()
    #添加完getter后就通过getter去获取属性的值
    # price就是属性_price的getter
    print(car.price)

    #通过setter给属性赋值
    car.price = 'abc'
    print(car.price)

result:
20.0K
0.0K

继承

python中类可以继承,并且支持多继承
程序中的继承: 就是让子类直接拥有父类的属性和方法(继承后父类中的内容不会因为被继承而减少)

class 类名(父类):
    类的内容

注意: 如果声明类没有写继承的类,那么它会自动继承基类object.
python中所有的类都是直接或间接继承object
a.所有属性和方法都能继承
b.__slots__的值不会继承,但是会影响子类对象__dict__属性。(只显示子类的属性)
class Person(object):
    number2 = 10
    __private_msg = 500
    __slots__ = ('name','face')
    def __init__(self,name='光明',face='nice'):
        self.name = name
        self.face = face

    def show_msg(self):
        print('姓名: %s 容貌: %s' % (self.name,self.face))

    @classmethod
    def show_number(cls):
        print('show_number:%d' % cls.number2)

    @staticmethod
    def show_msg2():
        print('No message')

class Student(Person):
    pass
if __name__ == '__main__':
    # Person类
    p = Person()
    # print(p.__dict__)
    # Student类
    stu = Student()
    print(Student.number2,stu.name,stu.face)
    stu.show_msg()
    Student.show_number()
    Student.show_msg2()
    print(Student._Person__private_msg)
    stu.age = 20
    print(stu.__dict__)

result:
10 光明 nice
姓名: 光明 容貌: nice
show_number:10
No message
500
{'age': 20}

方法的重写

子类继承父类,拥有父类的属性和方法以后,还可以再添加自己的属性和方法

class Animal:

    def __init__(self):
        self.age = 0
        self.sex = '雌'
    def shout(self):
        print('嗷嗷嗷~')
class Cat(Animal):
    food = 'fish'
    def __init__(self):
        super().__init__()
        self.name = '阿花'

    def shout(self):
        #通过super()调用父类的方法,保留父类的功能
        super(Cat, self).shout()
        print('喵喵喵~')

if __name__ == '__main__':
    ani = Animal()
    ani.shout()
    cat = Cat()
    print(cat.__dict__)
    cat.shout()

result:
嗷嗷嗷~
{'age': 0, 'sex': '雌', 'name': '阿花'}
嗷嗷嗷~
喵喵喵~

init方法的重写

#写一个person类,拥有属性name,age,sex。要求创建Person对象的时候必须给name和age赋值,sex可赋值也可以不赋值。
#再写一个staff类,继承自person类,要求保留person中所有的属性,并添加新的属性salary。
#要求创建staff类的对象的时候,只能给name赋值且必须赋值。

class Person:
    def __init__(self,name,age,sex='mole'):
        self.name = name
        self.age = age
        self.sex = sex
class Staff(Person):
    def __init__(self,name):
        super(Staff, self).__init__(name,10)
        self.salary = ''
        self.name = name


if __name__ == '__main__':
    staff = Staff('王安石')

运算符的重载

class Student:
    def __init__(self,name,age,sex):
        self.name = name
        self.age = age
        self.sex = sex

    # self,other分别为相加的两个对象
    def __add__(self, other):
        list2 = []
        list2.append(self.__dict__)
        list2.append(other.__dict__)
        return list2
    #重载 > 方法
    def __gt__(self, other):
        return self.age > other.age
    #重写魔法方法__str__,用来定制对象的打印形式
    def __str__(self):
        return '%s %d %s' % (self.name,self.age,self.sex)
if __name__ == '__main__':
    stu1 = Student('周一',10,'男')
    stu2 = Student('周二',50,'女')
    stu3 = Student('周三',25,'人妖')
    list2 = [stu1,stu2,stu3]
    list2.sort()
    for n in list2:
        print(n)
    print(stu1+stu2)
    print(stu1.__add__(stu2))

result:
周一 10 男
周三 25 人妖
周二 50 女
[{'name': '周一', 'age': 10, 'sex': '男'}, {'name': '周二', 'age': 50, 'sex': '女'}]
[{'name': '周一', 'age': 10, 'sex': '男'}, {'name': '周二', 'age': 50, 'sex': '女'}]

上一篇 下一篇

猜你喜欢

热点阅读