码迷,mamicode.com
首页 > 编程语言 > 详细

学习Python基础--------6面对对象进阶

时间:2017-09-22 00:52:43      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:类的变量   super   strong   调用   none   school   ann   类变量   rtu   

上节回顾


属性
实例变量(存在每个实例的内存里)
类变量(存在类里,共享使用)
私有属性__var

方法
构造方法 __init__
析构方法 __del__ (实例打开链接临时文件关闭后销毁)
私有方法

对象:实例化一个类之后得到的对象

 


封装
把一些功能的实现细节不对外暴露

续承
继承方式
继承
组合

代码的重用
单级成
多继承
2.7 广度优先新式类,深度优先经典类
3.0 都是广度优先
class Foo(School)

  def __init__(self,name,age,sex,salary,course):
  #新式类
  super(Foo,self).__init__(name,age,sex)
  #经典类
  School.__init__(self,name,age,sex)
  #组合
  self.person = person

    self.salary = salary
    self.course = course
多态
接口重用,一种接口多种实现

 

静态方法

通过@staticmethod装饰器即可把其装饰的方法变为一个静态方法,什么是静态方法?

普通方法可以实例化调用,并且方法里可以通过self.通用实例变量或类变量,但静态方法是不可以访问实例变量和实例变量,它与类唯一的关联就是需要通过类名来调用这个方法

class Dog(object):
    def __init__(self,name):
        self.name = name

  #方法
    def eat(self,food):
        print(%s is eating %s%(self.name,food))

d = Dog(cehngronghua)

d.eat(baozi)

#输出cehngronghua is eating baiozi



##############
class Dog(object):
    def __init__(self,name):
        self.name = name

    @staticmethod    #和类什么关系  ,只是相当于函数
    def eat(self,food):
        print(%s is eating %s%(self.name,food))

d = Dog(cehngronghua)

d.eat(d,zhiyu)   #独立的函数重新传入值
#输出  cehngronghua is eating zhiyu

类方法

类方法通过@classmethod装饰器实现,类方法和普通方法的区别是, 类方法只能访问类变量,不能访问实例变量

# Author:Zhiyu Su

class Dog(object):
    name = 1233
    def __init__(self,name):
        self.name = name

    @classmethod
    def eat(self):
        print(%s is eating %s%(self.name,dd))

d = Dog(cehngronghua)

d.eat() 
#输出  1233 is eating dd    直接调用类的变量

 

属性方法

把一个方法变成一个属性  所以调用直接调用不用输入括号

# Author:Zhiyu Su

class Dog(object):

    def __init__(self,name):
        self.name = name

    @property   #attribute  属性
    def eat(self):
        print(%s is eating %s%(self.name,dd))

d = Dog(cehngronghua)

# d.eat()#调用时会报错‘NoneType‘ object is not callable

d.eat
#输出cehngronghua is eating dd




属性方法@eat.setter
##################
# Author:Zhiyu Su

class Dog(object):

    def __init__(self,name):
        self.name = name


    #方法变成属性
    @property   #attribute  属性
    def eat(self):
        print(%s is eating %s%(self.name,dd))

    #如果要给eat设置属性需要重新设立一个eat
    @eat.setter
    def eat(self, food):
        print(set to food :, food)

d = Dog(cehngronghua)

# d.eat()#调用时会报错‘NoneType‘ object is not callable

d.eat

d.eat = baozi

#  输出 cehngronghua is eating dd    set to food : baozi

##################

#调用 @eat.deleter删除

# Author:Zhiyu Su

class Dog(object):

    def __init__(self,name):
        self.name = name
        self.__food = None



    @property
    def eat(self):
        print(%s is eating %s%(self.name,self.__food))


    @eat.setter
    def eat(self, food):
        print(set to food :, food)
        self.__food = food

    @eat.deleter
    def eat(self):
        del self.__food
        print(删完了)

d = Dog(cehngronghua)

d.eat#调用 @property  eat

d.eat = baozi  #调用@eat.setter
del d.eat        # 调用 @eat.deleter

属性方法的实例

技术分享
# Author:Zhiyu Su

class Flight(object):
    def __init__(self,name):
        self.flight_name = name


    def checking_status(self):
        print(cheking flight %s tatus % self.flight_name)
        return 1

    @property
    def flight_status(self):
        status = self.checking_status()
        if status == 0:
            print(filight got cancelend...)
        elif status == 1:
            print(flighrt is arried...)
        elif status == 2:
            print(filght has de partured already...)
        else:
            print(cannot confirm the flight staus....,please check later)
    @flight_status.setter
    def flight_status(self,status):
        print(Flight %s has changed status to %s%(self.flight_name,status))

f = Flight(CA980)
f.flight_status
#对用户端口
f.flight_status = 2
f.flight_status
View Code

 

类的特殊成员方法

1.__doc__ 表示类的描述信息

技术分享
# Author:Zhiyu Su

class Dog(object):
    ‘‘‘这个对象是狗‘‘‘

    def __init__(self,name):
        self.name = name
        self.__food = None

    @property
    def eat(self):
        print(%s is eating %s%(self.name,self.__food))



d = Dog(cehngronghua)

print(Dog.__doc__)
#输出 打印类的描述性信息‘这个对象是狗’
View Code

2.__module__和__class__

__module__ 表示当前操作的对象在哪个模块

__class__  表示当前操作的对象的类是什么

 

学习Python基础--------6面对对象进阶

标签:类的变量   super   strong   调用   none   school   ann   类变量   rtu   

原文地址:http://www.cnblogs.com/szy413227820/p/7571969.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!