[Python笔记]第十三篇:面向对象进阶

面向对象进阶

类里面继承与函数的调用关系

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
类里面继承和函数调用的关系
执行self.f1()
self是D
所以还是还会按照D的继承顺序去找self.f1()
"""

class A:
    def bar(self):
        print('bar')
        self.f1()

class B(A):
    def f1(self):
        print('b')

class C:
    def f1(self):
        print('c')

class D(C,B):
    pass


d1 = D()
d1.bar()
View Code

 

执行基类的构造方法的两种方式

#!/usr/bin/env python
# -*- coding: utf-8 -*-


# 执行基类的构造方法的两种方式
class Animal:
    def __init__(self):
        print('这是基类的构造方法')
        self.tp = "动物"


class Cat(Animal):
    def __init__(self):
        print('这是派生类的构造方法')
        self.n = "猫"
        super(Cat, self).__init__()      # <-------- 方式一
        # Animal.__init__()          # <--------  方式二


c = Cat()
print(c.__dict__)     # 列出类里面的成员

  

类里面的成员

字段

普通字段

class Provice:

    def __init__(self, name):
        self.temp = "TEMP"  # 普通字段设置了一个默认值
        self.name = name    # 普通字段,保存在对象里

    def show(self):
        print("This is: %s provice!" % self.name)


hebei = Provice("河北")

hebei.show()
print(hebei.temp)
普通字段

 

静态字段

#!/usr/bin/env python
# -*- coding: utf-8 -*-


class Provice:
    country = "中国"     # 静态字段,保存在类里

    def __init__(self, name):
        self.temp = "TEMP"  # 普通字段设置了一个默认值
        self.name = name    # 普通字段,保存在对象里

    def show(self):
        print("This is: %s provice!" % self.name)


hebei = Provice("河北")

hebei.show()
print(hebei.temp)

print(Provice.country)
静态字段

 

 

方法

构造方法

#!/usr/bin/env python
# -*- coding: utf-8 -*-


class Provice:

    def __init__(self, name):  # 构造方法
        self.name = name   # 普通字段,保存在对象中
构造方法

 

普通方法

#!/usr/bin/env python
# -*- coding: utf-8 -*-


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

    def f2(self):   # 普通方法
        print('hello')
普通方法

 

类方法

 

# 类方法
# @classmethod
# 获取当前类的类名
# 类方法只能通过类进行访问   默认参数cls --->类名


class Provice:

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

    @classmethod    # 类方法
    def ccte(cls):
        print(cls)



Provice.ccte()

# 执行结果:
# <class '__main__.Provice'>
类方法

 

 

静态方法

关键字为@staticmethod,保存在类里面,所有用类来调用,执行方式为ClassName.method(arg1, arg2)
注意:静态方法括号里面不加self

# 静态方法
# @staticmethod
# 装饰类中的方法  这个方法变成静态方法  属于类  所以用类访问 执行的时候:  类名 + 静态方法(根据规范)
# 里面不加self


class Provice:

    def __init__(self, name):
        self.name = name   # 普通字段,保存在对象中

    @staticmethod
    def f1(arg1, arg2): # 静态方法可以传参数
        print(arg1, arg2)



    def f2(self):
        pass

Provice.f1("这是", "静态方法")
静态方法

 

 

特性

说明

#!/usr/bin/env python
# -*- coding: utf-8 -*-


# "特性"  访问时将方法伪造成字段
# @property

class Provice:

    def __init__(self, name):
        self.name = name  # 普通字段,保存在对象中

    def start(self):           # 普通方法
        temp = "%s nb" % self.name
        return temp

    @property              # "特性"--->将方法伪造成字段
    def end(self):
        temp = "%s nb too" % self.name
        return temp

    @end.setter
    def end(self, value):
        print(value)
        self.name = value      # self.name重新赋值


obj = Provice('john')

ret1 = obj.start()   # 获取普通方法的值
ret2 = obj.end     # 获取特性
print(ret1, '|',  ret2)

print('\n')
# 设置特性的值    要配合@end.setter一起使用 用value参数来接收值
obj.end = 'rtmp'

print(obj.start(), '|', obj.end)
特性

 

posted @ 2016-06-13 17:51  hallo.world  阅读(90)  评论(0)    收藏  举报