zoukankan      html  css  js  c++  java
  • 面向对象的三大特征

    一、封装:提高程序的安全性

    • 将数据(属性)和行为(方法)包装到类对象中。在方法内部对属性进行操作,在类对象的外部调用方法。这样,无需关心方法内部的具体实现细节,从而隔离了复杂度。
    class Car:
        def __init__(self,brand):
            self.brand=brand
        def start(self):
            print('汽车已经启动')
    
    car=Car('宝马')
    car.start()
    print(car.brand)
    
    运算结果:
    
    汽车已经启动
    宝马
    • 在python中没有专门的修饰符用于属性的私有,如果该属性不希望在类对象外部被引用,可以在前边使用两个“_”
    class Student:
        def __init__(self,name,age):
            self.name=name
            self.__age=age
    
        def show(self):
            print(self.name,self.__age)
    
    stu=Student('全家福',100)
    stu.show()
    #print(stu.__age)  #AttributeError 报错:加__后变成私有属性,不能在类外使用
    #如果实在是想在类外调用
    print(dir(stu))
    print(stu._Student__age)

    运行结果:

    全家福 100
    ['_Student__age', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'name', 'show']
    100

    二、继承:提高代码的复用性

    1、语法格式:class 子类类名(父类1,父类2...)

    • 如果一个类没有继承任何类,那就默认继承了Object
    • python支持多继承
    • 定义子类时,必须在其构造函数中调用父类的构造函数
    class Person(object):#object可以省略
        def __init__(self,name,age):
            self.name=name
            self.age=age
    
        def info(self):
            print(self.name,self.age)
    
    class Student(Person):
        def __init__(self,name,age,stu_no):
            super().__init__(name,age)
            self.stu_no=stu_no
    
    class Teacher(Person):
        def __init__(self,name,age,teachofyear):
            super().__init__(name,age)
            self.teachofyear=teachofyear
    
    stu=Student('张三',20,'20170314333')
    teacher=Teacher('梅达',50,10)
    
    stu.info()
    teacher.info()
    
    运算结果:
    
    张三 20
    梅达 50
    

    2、方法重写

    • 如果子类对继承自父类的某个属性或方法不满意,可以在子类中对其(方法体)进行重新编写
    • 子类重写后的方法中可以通过super()调用父类中被重写的方法
    #方法重写
    class Person(object):#object可以省略
        def __init__(self,name,age):
            self.name=name
            self.age=age
    
        def info(self):
            print(self.name,self.age)
    
    class Student(Person):
        def __init__(self,name,age,stu_no):
            super().__init__(name,age)
            self.stu_no=stu_no
        #重写info()方法
        def info(self):
            super().info()
            print(self.stu_no)
    
    class Teacher(Person):
        def __init__(self,name,age,teachofyear):
            super().__init__(name,age)
            self.teachofyear=teachofyear
        def info(self):
            super().info()
            print(self.teachofyear)
    
    student=Student('秦健峰',22,'201703164333')
    student.info()
    teacher=Teacher('小骄傲',23,2)
    teacher.info()
    
    运算结果:
    
    秦健峰 22
    201703164333
    小骄傲 23
    2

    3、Object类

    • object类是所有类的父类,因此所有类都有object类的属性和方法
    • 内置函数dir()可以查看指定对象所有属性
    • object有一个str()方法,用于返回一个对于”对象的描述“,对应于内置函数str()经常用于print()方法,帮我们查看对象的信息,所以我们经常会对str()进行重写
    class Student:
        def __init__(self,name,age):
            self.name=name
            self.age=age
        def __str__(self):
            return '我的名字是{0},今年{1}岁'.format(self.name,self.age)
    
    student=Student('小秦同学',23)
    print(student)#print默认调用__str__()这样的方法
    
    #没有重写str方法:print输出结果: <__main__.Student object at 0x000001FC920B1250>
    #重写str方法:print输出结果:  我的名字是小秦同学,今年23岁

    三、多态:提高程序的可拓展性和可维护性

    • 简单来说,多态就是“具有多种形态”,它指的是:即便不知道一个变量所引用的对象到底是什么类型,任然可以通过这个变量调用方法,在运行过程中根据变量所引用对象的类型,动态决定调用哪个对象中的方法
    • python的多态是“鸭子类型”,在鸭子类型中,不需要关心对象是什么类型,到底是不是鸭子,只关心对象的行为。
    class Animal(object):
        def eat(self):
            print('动物会吃...')
    
    class Dog(Animal):
        def eat(self):
            print('狗会吃骨头')
    class Cat(Animal):
        def eat(self):
            print('猫会吃鱼')
    
    class Person:
        def eat(self):
            print('人会吃肉')
    
    def fun(obj):
        obj.eat()
    
    #调用函数
    fun(Dog())
    fun(Cat())
    fun(Animal())
    fun(Person())
    

      

  • 相关阅读:
    ubuntu-14.04.2-desktop-amd64.iso:ubuntu-14.04.2-desktop-amd64:安装Oracle11gR2
    ubuntu-15.04-desktop-amd64.iso:ubuntu-15.04-desktop-amd64:安装Oracle11gR2
    Ubuntu 使用文件 casper-rw 镜像文件 保存变更内容
    continue — Skip to the next iteration of a loop in a shell script
    Is there a TRY CATCH command in Bash
    tar 打包压缩
    oracle 重复只保留一条
    sed 删除 51, 54 行 输出到原文件
    ORA-01000: maximum open cursors exceeded
    Oracle 在字符串中输入单引号或特殊字符
  • 原文地址:https://www.cnblogs.com/xiaoqing-ing/p/14992558.html
Copyright © 2011-2022 走看看