08.面向对象程序设计

类的创建:

class Class1:
    """类的帮助信息"""
    pass  # 没有具体功能实现


c = Class1() # 创建Class1类的实例
print(c)  # <__main__.Class1 object at 0x10ed722b0>

类的构造方法:

class Class1:
    """类的帮助信息"""

    def __init__(self):  # 构造方法
        print("我是Class1类")


c = Class1()  # 创建Class1类的实例

自定义构造参数:

class People:
    """People类"""

    # 自定义参数
    def __init__(self, name, age):  # 构造方法
        print("我叫%s,今年%d岁了" % (name, age))


p = People('sam', 12)  # 我叫sam,今年12岁了

类的成员:

类属性
实例属性
实例方法
class People:
    """People类"""
    name = '匿名'  # 类属性,可以通过实例名或类名访问
    age = 0

    # 自定义参数
    def __init__(self, name='', age=0):  # 构造方法
        People.name = name if name else People.name
        People.age = age if age else People.age
        print("我叫%s,今年%d岁了" % (People.name, People.age))

    # self: 表示类的实例
    def do(self, ing):  # 实例方法
        print(ing)


p = People()  # 我叫匿名,今年0岁了
p.do("写字")
print(People.age)  # 0
p2 = People('tom', 12)  # 我叫tom,今年12岁了
print(People.age)  # 12
# 添加类属性
People.other = 'other'
print(People.other)  # other
p.other = 'p_other'
print(p.other)  # p_other


class class2():
    def __init__(self):
        self.a = 1111  # 实例属性,只能通过实例名访问


c2 = class2()
print(c2.a)  # 1111
# 通过类名访问实例属性报错
# class2.a  # AttributeError: type object 'class2' has no attribute 'a'

访问限制:

# 以单下划线开头的表示protect(保护)类型的成员,只允许类本身和子类进行访问,但不能使用
# from module import * 语句导入
class C1:
    _a = '保护属性'  # 定义保护属性

    def __init__(self):
        print(C1._a)  # 在实例方法中访问保护属性


c = C1()
print(c._a)  # 通过实例名访问保护属性
print(C1._a)  # 通过类名访问保护属性


class C2:
    __aa = '私有属性'  # 定义私有属性

    def __init__(self):
        print(C2.__aa)  # 在实例方法中访问私有属性


c2 = C2()
# 通过实例名访问私有属性
# print(c2.__aa)  #AttributeError: 'C2' object has no attribute '__aa'
# 通过类名访问私有属性
# print(C2.__aa)  #AttributeError: type object 'C2' has no attribute '__aa'
print(c2._C2__aa)  # 私有属性可以通过'实例名._类名__xx'方式访问
print(C2._C2__aa)  # 私有属性可以通过'类名._类名__xx'方式访问

@property:

class C1:
    def __init__(self, w, h):
        self.w = w
        self.h = h

    @property  # 将方法转换为属性
    def area(self):
        return self.w * self.h


c = C1(12, 12)
print(c.area)  # 144

为属性添加安全保护:

class C1:
    def __init__(self, w, h):
        self.__res = w * h

    @property  # 将方法转换为属性
    def area(self):
        return self.__res  # 返回私有属性


c = C1(12, 12)
print(c.area)  # 144
# 修改只读属性保存
# c.area = 12  # AttributeError: can't set attribute

属性的setter方法:

class C1:
    list_s = ['a', 'b', 'c']

    def __init__(self, s):
        self.__res = s

    @property  # 将方法转换为属性
    def area(self):
        return self.__res  # 返回私有属性

    @area.setter  # 设置setter方法,让属性可修改
    def area(self, value):
        if value in C1.list_s:
            self.__res = '你选择了' + value
        else:
            self.__res = '你的选择不存在'


c = C1('1')
c.area = 'a'
print(c.area)  # 你选择了a
c.area = 'e'
print(c.area)  # 你的选择不存在

继承、方法重写:

class Fruit:
    color = '绿色'

    def harvest(self, c):
        print(c)
        print("原来的颜色" + Fruit.color)


class Apple(Fruit):
    color = '红色'

    def __init__(self):
        print('apple')

    def harvest(self, c):  # 方法重写
        print(c)
        print(Apple.color)


class Orange(Fruit):
    color = '橙色'

    def __init__(self):
        print('orange')


apple = Apple()
apple.harvest(apple.color)
orange = Orange()
orange.harvest(orange.color)
# apple
# 红色
# 红色
# orange
# 橙色
# 原来的颜色绿色

派生类中调用基类的方法:

class Fruit:
    def __init__(self, c):
        Fruit.color = c

    def harvest(self):
        print("原来的颜色" + Fruit.color)


class Apple(Fruit):
    def __init__(self):
        print('apple')


class Apple2(Fruit):
    def __init__(self, c):
        # Fruit.__init__(self,c) # 直接使用基类名调用__init__()方法
        # super(Apple2,self).__init__(c)  # 使用super写法一
        super().__init__(c) # 调用基类的__init__()方法
        print('apple')


# a = Apple()
# a.harvest()  # AttributeError: type object 'Fruit' has no attribute 'color'
a2 = Apple2('red')
a2.harvest()

猜你喜欢

转载自www.cnblogs.com/fly-book/p/11737953.html