zoukankan      html  css  js  c++  java
  • python06——面向对象基础(2)

    1.析构方法

    当一个对象被删除或者被销毁时,python解释器会默认调用一个方法,这个方法为__del__ ()方
    法,也称为析构方法

    ##程序执行结束自动调用__del__方法
    class Animal(object):
       def __init__ (self, name):
               self.name = name
               print('__init__方法被调用')
    #析构方法,当对象被销数时python解析器会自动调用
       def __del__(self):
              print('__del__方法被调用')
              print('%s对象被销毁'%self.name)
    
    dog = Animal('旺柴')

    也可手动删除: del dog

    2.单继承

    python中面向对象的三大特征:封装、继承、多态

    封装:把内容封装到某个地方,便于后面的使用。对于封装来说其实就是使用初始化构造方法将内容封装到对象中,然后通过对象直接或者self来获取被封装的内容

    继承:子类可以继承父类的方法

    3.多继承

    可以继承多个父类

    class Animal:
        def eat(self):
            print('吃饭啦')
        def drink(self):
            pass
            print("喝水啦")
    class say:
            def said(self):
                print('我是一只小动物')
    
    class Dog(Animal):   ##继承了Animal父类的方法
        def wwj(self):
            print('汪汪')
    
    class Cat(Animal,say):  ###继承了Animal、say类
        def mmj(self):
            print('喵~~')
    
    d1=Dog()
    d1.eat()
    d2=Cat()
    d2.said()
    class D():
        def eat(self):
            print('D方法')
    class C(D):
        def eat(self):
            print('C方法')
    class B(D):
        pass
    class A(B,C):
        pass
    
    a=A()
    a.eat()
    print(A.__mro__)
    
    ####A->B->C->D 广度优先
    
    
    class E:
        def eat(self):
            print('E方法')
    class D:
        def eat(self):
            print('D方法')
    class C(D):
        pass
    class B(E):
        pass
    class A(B,C):
        pass
    
    a=A()
    a.eat()   ##调用eat类
    print(A.__mro__)
    
    ####A->B->E->C 深度优先
    继承顺序

    4.继承的传递、重写父类方法、调用父类方法

     son类继承了father类,father类继承了grandfather类,那么son也继承了grandfather类

    class Dog():
         def __int__(self,name,color):
                Dog.name=name
                Dog.color=color
         def  bark():
               print('汪汪')
    
    class kejiquan(Dog):
          def __int__(self,name,color):
                Dog.__int__(self,name,color)###调用父类
                #自动继承父类super().__int__(name,color)
                ##拓展其他属性
                self.age=12
           def __str__(self):
                return '{}的颜色是{} 年龄是{}'.format(self.name,self.color,self.age)
           def bark(self):   ##重写类  
                    super().bark()
                    print('好凶')    

    5.多态

     多态——定义时的类型和运行时的类型不一样。

    优点:增加程序的灵活性和扩展性。要实现多态,有两个前提需要遵守:①继承②重写

    ##父类
    class Animal:
        def say_who(self):
            print('我是一个动物')
    ##子类
    class Duck(Animal):
        def say_who(self):       ##子类重写父类
            print('我是一只小鸭子')
    class Dog(Animal):
        def say_who(self):
            print('我是哈趴狗')
    class Cat(Animal):
        def say_who(self):
            print('我是小猫咪')
    
    def commonInvoke(obj):
        ##统一调用方法
        obj.say_who()
    
    
    listobj=[Duck(),Dog(),Cat()]
    for i in listobj:
        commonInvoke(i)

    8.类属性和实例属性

    类属性——类对象所拥有的属性,它被所有类对象的实例对象所共有,类对象和实例对象可以访问

    实例属性——实例对象所拥有的属性,只能通过实例对象访问

    class Student:
        name='李明'  ##属于类属性,就是student类对象所拥有的的
        def __init__(self,age):
            self.age=age  ##实例属性
    lm=Student(18)
    print(lm.name)   #通过实例对象去访问类属性
    print(lm.age)
    xh=Student(28)
    print(xh.name)   
    print(xh.age)
    
    print(lm.name)   #通过实例对象去访问类属性
    print(lm.age)
    print(Student.name) ###通过类对象student去访问name print(Student.age) ###访问不了

    9.类方法和静态方法

    类方法:

    类对象所拥有的方法,需要用装饰器@classmethod来标识其为类方法,对于类方法,第一个参数必须是类对象,一般以cls作为第一个参数,类方法可以通过类对象,实例对象调用

    class Person(object):
        country = 'china' #类属性
            def_init (self,name):
                self.name = name
    #类方法,用装饰器classmethod装饰
        @classmethod
            def get_country(cls)
                print(cls.country)
       
        @classmethod
            def get_country(cls,data)
                cls.country=data ##修改类属性的值,在类方法中
      
    people = Person('xiaoming')
    result = Person.get_country() #获取类属性
    #打印出'china'    
    Person.get_country('英国')
    print(Person.get_country())  ##通过类对象去引用

    静态方法:

    类对象所拥有的方法,需要用@staticmethod来表示静态方法,静态方法不需要任何參数。由于静态方法主要来存放逻辑性的代码,本身和类以及实例对象没有交互,也就是说在静态方法中,不会涉及到类中方法和属性的操作

    class Person(objed):
    country= 'china'   #类属性
        def __init__(self, name):
            self.name = name
    #静态方法,用装饰器staticmethod装饰
            @staticmethod
        def get country(): #静态方法不用传任何参数
            print(Person.country)
    
    people = Person('xiaoming')
    result = Person.get_country() #获取类属性
    #输出'china'                

    比如:返回当前系统时间

    import time
    class TimeTest:
        def __int__(self,hpur,min,second):
             self.hour=hour
             self.min=min
             self.second=second   
       
        @staticmethod
        def showTime():
           return  time.strftime("%H:%M:%S",time.localtime())
    print(TimeTest.shouTime())
    t=TimeTest(2,10,15)
    print(t.showTime)  ##没有必要通过这种方法去访问静态方法
                

    几种方法的对比:

    类方法——第一个参数是类对象cls,通过cls引用的类对象的属性和方法。

    实例方法——第一个参数是实例对象self,通过self引用的可能是类属性、也有可能是实例属性(这个需要具体分析),不过在存在相同名称的类属性和实例属性的情况下,实例属性优先级更高。

    静态方法——不需要额外定义参数,因此在静态方法中引用类属性的话,必须通过类对象来引用。

     

  • 相关阅读:
    数据库范式
    将DBF,XLS,XML,MDB文件导入C#DataGrid的方法
    在类文件中引用Server对象
    在使用了母版页的页面里加载css和js文件
    IIS 7.5 URL重写参数
    hdu Can you solve this equation?
    hdu Dome of Circus
    hdu Bone Collector
    hdu Turn the corner
    hdu 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活
  • 原文地址:https://www.cnblogs.com/yilia-er/p/14096880.html
Copyright © 2011-2022 走看看