zoukankan      html  css  js  c++  java
  • 面向对象编程三大特性之 ----多态和封装

    一、多态

    什么是多态:

    由不同的类实例化得到的对象调用同一个方法,执行的逻辑相同。

    多态体现在最后的执行上

    多态的概念指出了对象如何通过他们共同的属性和动作来操作及访问,而不需考虑他们具体的类

    多态实际上是依附于继承的两种含义的:“改变”和“扩展”本身就意味着必须有机制去自动选用你扩展过的版本,

    故无多态,则两种含义就不可能实现

    所以多态实质上是继承的实现细节:那么让多态与封装、继承这两个概念并列,显然是不太合适的

    class H2o:
        def __init__(self,name,temperature):
            self.name = name
            self.temperature = temperature
        def turn_ice(self):
            if self.temperature < 0:
                print('[%s]温度太低结冰了'%self.name)
            elif self.temperature > 0 and self.temperature < 100:
                print('[%s]液化成水'%self.name)
            elif self.temperature > 100:
                print('[%s]温度太高变成了水蒸气'%self.name)
    
    class Water(H2o):
        pass
    class Ice(H2o):
        pass
    class Steam(H2o):
        pass
    
    w1 = Water('水',25)
    i1 = Ice('冰',-20)
    s1 = Steam('水蒸气',3000)
    
    w1.turn_ice()
    # i1.turn_ice()
    # s1.turn_ice()
    
    def func(obj):
        obj.turn_ice()
    
    func(i1)
    

    二、封装

    ①类就是麻袋,这本身就是一种封装

    ②类中定义私有的,只在类的内部使用,外部无法访问

    单下划线开头的属性不用该在外部被调用(这是种约定,不是强制)

    如果是双下划线开头的属性,会在内部被重命名

    调用时用  ._类名__star 

    ③明确区分内外,内部的实现逻辑,外部无法知晓,并且为封装到内部的逻辑提供一个访问借口给外部使用(这才是真正的封装) 

    class People:
        _star = 'earth'
        __end = 'liufuren'
        def __init__(self,id,name,age,salary):
            self.id = id
            self.name = name
            self.__age = age
            self.salary = salary
    
        def get_id(self):
            print("我是私有方法啊,我找到的id是[%s]"%self.id)
    
    p1 = People(412825,"金灵",'18',10000)
    print(p1._star)
    print(p1._People__end)
    

      

    通过创建接口访问

    class Room:
        def __init__(self,name, owner, width, length, high):
            self.name = name
            self.owner = owner
            self.__width = width
            self.__length = length
            self.__higth = high
    
        def tell_area(self):
            return self.__length * self.__width
    
    r1 = Room('卫生间','刘',50,50,50)
    print(r1.name)
    # print(r1.__width) #报错哦
    print(r1.tell_area())class Room:
        def __init__(self,name, owner, width, length, high):
            self.name = name
            self.owner = owner
            self.__width = width
            self.__length = length
            self.__higth = high
    
        def tell_area(self):
            return self.__length * self.__width
    #访问函数
      def get_width(self):
         print(self.__width)
    r1 = Room('卫生间','刘',50,50,50) print(r1.name) # print(r1.__width) #报错哦 print(r1.tell_area())

      

    其实上述例子中的 length, width ,high根本不用写成私有属性,因为可能需要经常调用,

    变为私有属性后,单独低用只好写接口(访问函数),程序会变的千疮百孔。

    写程序前先思考需要什么,不要为了觉得所谓的高大上就把继承、封装、多态统统写进去,

    反而,得不偿失。

    总结:面向对象的优点

    ① 通过封装明确了内外

    ② 通过继承 + 多态 在语言层面支持了归一化设计

      

    一个奋斗中的产品小白
  • 相关阅读:
    [SCOI2003]严格N元树
    CF280 C. Game on Tree
    [HDU2281]Square Number
    [HDU5391]Zball in Tina Town
    [HDU3988]Harry Potter and the Hide Story
    [HDU5794]A Simple Chess
    [HDU5451]Best Solver
    [HDU1724]Ellipse
    [HDU6304]Chiaki Sequence Revisited
    [HDU6343]Graph Theory Homework
  • 原文地址:https://www.cnblogs.com/dabai123/p/11575044.html
Copyright © 2011-2022 走看看