面向对象3

一、组合

  1. 定义:将一个对象作为另一个对象的属性

  2. 作用:减少代码冗余

    class People:
        def __init__(self, name, age, sex):
            self.name = name
            self.age = age
            self.sex = sex
    
        # 添加一门课程方法
        def add_course(self, course_obj):
            self.course_list.append(course_obj)
    
        # 打印当前对象中所有课程的课程信息
        def tell_all_course(self):
            # 拿到当前对象的课程列表,列表中存放的是一个个的课程对象
            for course_obj in self.course_list:
    
                # 每一个课程对象调用查看课程信息的方法
                course_obj.tell_course_info()
    
    class Teacher(People):
        def __init__(self, name, age, sex):
            super().__init__(name, age, sex)
            # 用来存放课程对象
            self.course_list = []
    
    
    class Student(People):
        def __init__(self, name, age, sex):
            super().__init__(name, age, sex)
            self.course_list = []
    
    # 课程类
    class Course:
        def __init__(self, course_name, course_period, course_price):
            # 课程名称周期价格
            self.course_name = course_name
            self.course_period = course_period
            self.course_price = course_price
    
        def tell_course_info(self):
            print(f'''
            课程名称: {self.course_name}
            课程周期: {self.course_period}
            课程价钱: {self.course_price}
            ''')
    
    
    tea1 = Teacher('tank', 17, 'male')
    stu1 = Student('张全蛋', 20, 'female')
    python_obj = Course('python', 6, 2.0)
    linux_obj = Course('linux', 6, 1.0)
    
    # 添加python与linux课程
    tea1.add_course(python_obj)
    tea1.add_course(linux_obj)
    # tea1.course_list[0].tell_course_info()
    tea1.tell_all_course()

二、封装

  1. 定义:把一堆属性与方法封装到一个对象中,并提供接口访问。

  2. 作用

    • 保护隐私

    • 隔离复杂度

  3. 第一层封装

    类和对象的属性与方法都在其各自的名称空间中,用‘.’方法访问

 

  4. 第二层封装

    以双下划线的方式隐藏属性,将‘x’自动转换成‘_类名x’的形式

  5. 在继承中父类如果不想让子类覆盖自己的方法,可以将方法定义为私有的

    • 正常情况

class A:
    def fa(self):
        print('from A')

    def test(self):
        self.fa()


class B(A):
    def fa(self):
        print('from B')


b = B()
b.test()

输出
from(B)
    • 把fa定义成私有的

 

class A:
    def __fa(self):  # 在定义时就变形为_A__fa
        print('from A')

    def test(self):
        self.__fa()  # 只会与自己所在的类为准,即调用_A__fa


class B(A):
    def __fa(self):
        print('from B')


b = B()
b.test()

输出
from(A)

  6. 通过__方法名 () 的方式,将内部具体实现细节封装起来,只给外部报漏一个简单的接口(函数)

      class PC: 
         def __init__(self,price,color,kind):
             self.color=color
             self.price=price
             self.kind=kind
         def open(self):
             print('接通电源')
             self.__check_device()
             print('载入内核')
             print('初始化内核')
             self.__start_service()
             print('启动GUI')
             self.__login()
     
         def __check_device(self):
             print('检测硬件1')
             print('检测硬件2')
             print('检测硬件3')
             print('检测硬件4')
         def __start_service(self):
             print('启动服务1')
             print('启动服务2')
             print('启动服务3')
         def __login(self):
             print('login..')
     
     pc=PC(6000,'white','lenvo')
     pc.open()
     
     
     输出
      接通电源
     检测硬件1
     检测硬件2
     检测硬件3
     检测硬件4
     载入内核
     初始化内核
     启动服务1
     启动服务2
     启动服务3
     启动GUI
 login..

 

三、类的property特性

  1. 作用

    将被装饰的方法伪装成一个数据属性,在使用时可以不加括号直接使用

  2. 作为装饰器使用

    class Goods:
        
        @property
        def price(self):
            print('@property')
        
        @price.setter
        def price(self, value):
            print('@price.setter')
        
        @price.deleter
        def price(self):
            print('@price.deleter')
        
        
    obj = Goods()
    
    obj.price 
    obj.price = 123
    del obj.price
    
    输出
    @property
    @price.setter
    @price.deleter

  2. 作为类属性使用

    class Goods(object):
        def __init__(self):
            # 原价
            self.original_price = 100
            # 折扣
            self.discount = 0.8
    
        def get_price(self):
            # 实际价格 = 原价 * 折扣
            new_price = self.original_price * self.discount
            return new_price
    
        def set_price(self, value):
            self.original_price = value
    
        def del_price(self):
            del self.original_price
    
        price = property(get_price, set_price, del_price, '价格属性描述...')
    
    
    obj = Goods()
    print(obj.price)  # 获取商品价格
    
    obj.price = 200
    print(obj.price)
    
    del obj.price
    
    
    输出
    80.0
    160.0

  3. property + 类的封装

    class People:
        def __init__(self, name):
            self.__name = name
    
        @property  # 查看obj.name
        def name(self):
            return '<名字是:%s>' % self.__name
    
    
    peo1 = People('byx')
    print(people.name)
    
    
    输出
    <名字是:byx>

 

原文地址:https://www.cnblogs.com/binyuanxiang/p/11711207.html