美文网首页
day15_python_类和对象

day15_python_类和对象

作者: mmlong_07 | 来源:发表于2018-09-10 08:57 被阅读0次

    属性的私有化

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

    1.私有化
    a.类中的属性和方法都可以通过在属性名和方法名前加两个下划线,
    来让属性和方法变成私有的。

    b.私有的属性和方法只能在当前的类中使用

    2.私有化原理
    在前面有两个下划线的属性名和方法前添加了'类名'来阻止外部通过直接访问属性名来使用属性

    class Dog:
    # 字段
    number = 100
    __count = 200
    
    def __init__(self):
        #对象的属性
    
        self.color = '黄色'
        self.age = '18'
        self.name ='大黄'
        self.__sex = '公狗'
    对象方法
    
        def __eat(self):
            print('%s啃骨头~'% self.name)
    def eat(self):
        # 在类中可以使用私有的属性和方法
        dog1.__eat()
        print('%s在吃屎~'%self.name)
    类方法
    
        @classmethod
        def shout(cls):
            print('count:',cls.__count,Dog.__count)
            print('汪汪汪~~~')
    静态方法
    
        @staticmethod
        def function():
            print('看家!!')
    python的类中默认的属性和方法都是公开的
    
    dog1 = Dog()
    print(dog1.name)
    print(dog1.number)
    print(dog1.name,dog1.color,dog1.age)
    
    dog1.eat()
    dog1.shout()
    dog1.function()
    在类的外面不能直接使用私有的属性
    print(Dog.__count) #AttributeError: type object 'Dog' has no attribute '__count'
    print(dog1.__sex) #AttributeError: 'Dog' object has no attribute '__sex'
    dog1.__eat()
    Dog.__shout()
    
    print(dog1.dict)
    

    属性的getter和setter

    1.保护类型的属性:
    a.就是声明对象属性的时候在属性名前加一个下划线来代表这个属性是受保护的属性
    那么以后访问这个属性的时候就不要直接访问,要通过getter来获取这个属性的值,setter来给这个属性赋值
    b.如果一个属性已经声明称保护类型的属性,那么我们就需要给这个属性添加getter,也可以添加setter

    2.添加getter
    添加getter其实就是声明一个参数有一个返回值的函数。作用是获取属性的值
    a.格式:
    @property
    def 去掉下划线的属性名(self):
    函数体
    将属性相关的值返回
    b.使用场景
    场景一:如果想要获取对象的某个属性的值之前,想要再干点别的事情(做额外的处理)。就可以给这个属性添加getter
    场景二:想要拿到某个属性被使用的时刻

    3.添加setter
    添加setter就是声明一个有一个参数但是没有返回值的函数。作用是给属性赋值
    a.格式
    b.使用场景
    在给属性值前想要再干点别的事情,就给属性添加setter

    class Car:
        def __init__(self):
            self.color = '黄色'
            self.type = '自行车'
            # _prince 属性是保护类型
            self._price = 2000
    
        给_price属性添加getter
        @property
        def price(self):
            print('在使用_price属性')
            return self._price/1000
        #想要给一个属性添加setter,必须先给这个属性添加getter
        @price.setter
        def price(self,price):
            if isinstance(price,int) or isinstance(price,float):
                self._price = price
            else:
                self._price
    
    car1 = Car()
    print(car1.color)
    

    添加完getter后就通过getter 去获取属性的值
    price就是属性_price的getter
    print(car1.price,'k') #实质是在调用getter对应的方法

    通过setter 给_price属性赋值。实质是在调用setter对应的方法

    car1.price = 3000
    print(car1.price)
    car1.price = 'abc'
    print(car1.price)
    

    练习:声明一个员工类,其中有一个属性是是否已婚(bool),获取值之前根据存在的值返回’已婚‘/'未婚'

    class Staff:
    
        def __init__(self,name,salary):
            self.name = name
            self.salary = salary
            self._is_marry = False
    
        @property
        def is_marry(self):
            if self._is_marry:
                return'已婚'
            return '未婚'
        @is_marry.setter
        def is_marry(self,marry):
            self._is_marry = marry
    
    staff1 = Staff('张三',10000)
    print(staff1.is_marry)
    
    staff1.is_marry = True
    print(staff1.is_marry)
    

    继承

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

    1.继承的语法
    class 子类(父类)
    类的内容

    注意:如果生明类的时候,没有写继承,那么这个类会自动继承python的基类,object;相当于class 类名(object):
    python中所有类都是直接或者间接的继承自object

    2.能继承哪些东西
    a.所有的属性和方法都能继承
    b.slots的值不会继承,但是会影响子类对象的dict属性。不能获取到父类继承下来的属性

    class Person(object):
        """人类"""
        # 字段
        number = 61
    
     __slots__ = ('name','age')
    对象属性
        def __init__(self,name1 = '张三',age1 = 18):
            self.name = name1
            self.age = age1
            self.__height =160
    
     对象方法
        def show_message(self):
            print('姓名:%s 年龄:%d'%(self.name,self.age))
        类方法
        @classmethod
        def show_number(cls):
            print('人类的数量:%d'% cls.number)
    
        静态方法
        @classmethod
        def complaint(cls):
            print('人类殴打小动物')
    class Student(Person):
            """学生类"""
            pass
    # 创建Person类的对象
    p = Person()
    
    # p.sex = 'boy'
    
    #print(p.__dict__)
    
    #创建Student类的对象
    stu1 = Student()
    
    print(Student.number)
    stu1.name = '李四'
    print(stu1.name)
    stu1.show_message()
    Student.show_number()
    Student.complaint()
    
    stu1.sex = 'giel'
    print(stu1.__dict__)
    

    方法的重写:

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

    1.添加方法和类的字段
    直接在子类中声明相应的方法和字段

    2.添加对象属性
    对象的属性是通过继承父类的init方法而继承下来
    如果想要在保留父类的对象的同时添加自己的对象属性,需要在子类的init方法中通过super()去调用父类的init方法

    3.方法的重写
    在子类中重新实现父类的方法,就是重写
    方式一: 直接覆盖父类的实现
    方式二: 保留父类的功能再添加其他功能

    4.类中方法的调用过程(重点)
    先在当前这个中的去找,没有去父类中找,找不到再去父类的父类中找,依次类推,如在基类中都没有找到才崩溃。
    在第一次找到的位置,去调用

    注意:使用super的时候必须是通过super()来代替父类或者是父类对象

    class Animal:
        """动物类"""
        number = 100
        def __init__(self):
            self.age = 0
            self.sex = '雌'
    
        def shout(self):
            print('嗷嗷叫')
    
        def eat(self):
            print('吃东西')
    
    
    class Cat(Animal):
        """猫类"""
        def __init__(self):
            # 调用父类的init方法
            super().__init__()
            self.name = '小花'
    
        foot = '鱼'
    
        def shout(self):
            print(super())
            print('喵喵~')
    
    
    class Dog(Animal):
        """狗类"""
        def shout(self):
            # 通过super()调用父类的方法,保留父类的功能
            super().shout()
            print('汪汪!!')
        # pass
    
    
    cat1 = Cat()
    print(cat1.age)
    print(cat1.name)
    cat1.shout()
    
    dog1 = Dog()
    dog1.shout()
    

    init方法的重写:

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

    class Person:
        def __init__(self, name1, age1, sex1='boy'):
            self.name = name1
            self.age = age1
            self.sex = sex1
    
    
    class Staff(Person):
        def __init__(self, name):
            super().__init__(name, 18)
            self.salary = 0
    
    
    if __name__ == '__main__':
        p1 = Person('张三', 20, 'boy')
        p2 = Person('夏明', 30)
    
        s1 = Staff('小红')
    

    .运算符的重载:

    如果希望类的对象支持相应的运算符操作(例如:+, -, *, /, >, <等),就必须实现相应的魔法方法。
    这个过程就叫运算符的重载

    +: add()

    : gt
    ....
    一般情况需要对>或者<进行重载,重载后可以通过sort方法直接对对象的列表进行排序

    class Student:
        def __init__(self, name='', age=0, score=0):
            self.name = name
            self.age = age
            self.score = score
    
        # self:+前面的对象
        # other: +后面的对象
        def __add__(self, other):
            return self.score + other.score
    

    重载 > 符号
    注意:重载>和<可以只重载一个,另外一个对应的功能自动取反

        def __gt__(self, other):
            return self.age > other.age
    

    重写魔法方法str,用来定制对象的打印样式

        def __str__(self):
            # return '<%s.%s object at 0x%x>' % (self.__module__, self.__class__.__name__, id(self))
            # return 'Student: %s %d %d' % (self.name, self.age, self.score)
            return str(self.__dict__)[1:-1]
    
    class Schoolchild(Student):
        def __add__(self, other):
            return self.age + other.age
    
    
    if __name__ == '__main__':
    
        stu1 = Student('小明', 18, 90)
        stu2 = Student('老王', 29, 84)
        stu3 = Student('小花', 20, 78)
    
        print(stu1)
    
        all_students = [stu1, stu2, stu3]
        all_students.sort(reverse=True)
        for stu in all_students:
            print(stu.name, stu.age, stu.score)
    
        print(stu1 > stu2)
        print(stu1 < stu2)
        print(stu1 + stu2)
        print(stu3 > stu1)
    
        # 父类重载了运算符,子类也能用
        c1 = Schoolchild('小明明', 8, 70)
        c2 = Schoolchild('小花花', 10, 67)
        print(c1+c2)
    

    相关文章

      网友评论

          本文标题:day15_python_类和对象

          本文链接:https://www.haomeiwen.com/subject/swdkgftx.html