day07-01-面向对象进阶

:属性(实例变量,类变量,私有属性__var)

       方法(构造方法,析构函数:默认就存在,写了就是重构了函数)

对象:封装(把一些功能实现细节不对外暴露)

           继承(代码重用,单继承,多继承:3.x均是广度优先)新式类class  foo(object)

    super(子类名,self).init(name.age.other)

           self.other = other       

           重构父类构造函数,可继承父类并添加子类新属性

多态:一个接口,多种实现

 

class Dog:
    def __init__(self,name):
        self.name = name
    def bulk(self):
        print("%s: wang wang wang!" % self.name)
d1 = Dog("陈1")
d2 = Dog("陈2")
d3 = Dog("陈3")
d1.bulk()
d2.bulk()
d3.bulk()
# 陈1: wang wang wang!
# 陈2: wang wang wang!
# 陈3: wang wang wang!

 

 

 

静态方法:(实际上跟类没神魔关系了,相当于单纯的函数,但必须通过类名或实例名调用,类似台湾和大中华的关系。故实例调用时需要传入实例对象)@staticmethod声明

 

class Foo(object):
    def __init__(self):
        pass

    @staticmethod
    def do_something():
        '''
           无法访问类属性、实例属性,相当于一个相对独立的方法,
           跟类其实没什么关系,换个角度来讲,其实就是放在一个
           类的作用域里的函数而已。
        '''
        print 'I am just a static method'

# 使用
Foo.do_something()
f = Foo()
f.do_something()

  

类方法:@classmethod,只能访问类变量,不能访问实例变量

 

class Foo(object):
    msg = 'hello world'

    @classmethod
    def do_something(cls):
        '''
        可以访问类属性,无法访问实例属性。
        '''
        print cls.msg

# 使用
Foo.do_something()
f = foo()
f.do_something()

 

  

实例方法

class Foo(object):
    cls_msg = 'I am a cls msg'

    def __init__(self):
        self.ins_msg = 'I am a instance msg'

    @staticmethod
    def static_do():
        print 'I am a static msg

    @classmethod
    def class_do(cls):
        print cls.cls_msg

    def instance_do(self):
        '''可以访问类属性'''
        print self.ins_msg
        print Foo.cls_msg

# 使用
f = Foo()

f.static_do()
# I am a static msg

f.class_do()
# I am a cls msg

f.instance_do()
# I am a instance msg
# I am a cls msg

  

 

属性方法:属性特点赋值,函数方法处理。@property,把一个方法变成一个静态属性,调用时,d.eat()-->d.eat

对属性方法赋值,在实例化之前建立一个私有属性,再建立一个方法,用@eat.setter声明,接受下面实例对象的传值,赋给私有变量保存,再在另一个函数输出类的私有属性值,删除属性方法,需要再写一个方法用@eat.deleter声明:

@Property

def eat(self)

    print("%s"%(self.__food))

@eat.setter

def eat(self,food):

    print("set food",food)

    self.__food=food

@eat.deleter

def eat(self)

    del self.__food

    print("删除__food")

d=Dog("zlz")

d.eat = "包子"

d.eat

del d.eat

属性方法案例:

 

class Flight(object):
    def __init__(self,name):
        self.flight_name = name
    def checking_status(self):
        print("checking flight %s status " % self.flight_name)
        return  0 #返回航班状态情况,此时为canceled
    @property
    def flight_status(self):
        status = self.checking_status()#  0
        if status == 0 :
            print("flight got canceled...")
        elif status == 1 :
            print("flight is arrived...")
        elif status == 2:
            print("flight has departured already...")
        else:
            print("cannot confirm the flight status...,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 = 0# 触发@flight_status.setter输出航班改动信息,直接给属性方法赋值即可轻松进行路线输出
f.flight_status
f.flight_status = 2
f.flight_status
f.flight_status = 3
# del f.flight_status #  触发@flight_status.deleter
# 输出结果
# checking flight CA980 status 
# flight got canceled...
# flight CA980 has changed status to 0
# checking flight CA980 status 
# flight got canceled...
# flight CA980 has changed status to 2
# checking flight CA980 status 
# flight got canceled...
# flight CA980 has changed status to 3

 

posted @ 2017-10-31 17:25  雷大侠!  阅读(146)  评论(0编辑  收藏  举报