Day8.

Day8.,第1张

私有属性和私有方法(实现封装)

# 测试私有属性
class Employee:
    __company = 'sxt'  # 私有类属性

    def __init__(self, name, age):
        self.name = name
        self._age = age  # 私有属性

    def __work(self):  # 私有方法
        print('好好工作')
        print('年龄:{0}'.format(self._age))

    def get_age(self):  # 外部访问私有实例属性
        return self._age


e = Employee('zf', 18)
print(e.name)
print(e._age)  # 外部访问私有属性
print(dir(e))  # 查看实例对象所有属性方法
print(e.__dict__)  # 查看实例对象属性字典
print(isinstance(e, Employee))
e._Employee__work()
print(e._Employee__company) #访问私有类属性

@property装饰器

可以将一个方法的调用方式变成属性调用

# 测试@property最简化的使用
class Employee:

    def __init__(self, name, salary):
        self._name = name
        self._salary = salary

    @property
    def salary(self):
        return self._salary

    @salary.setter
    def salary(self, salary):
        if 10000 < salary < 50000:
            self._salary = salary
        else:
            print('录入错误,薪水在10000-50000之内')


'''
    def get_salary(self):
        return self._salary

    def set_salary(self, salary):
        if 10000 < salary < 50000:
            self._salary = salary
        else:
            print('录入错误,薪水在10000-50000之内')
'''

emp1 = Employee('zf', 30000)
print(emp1.salary)
emp1.salary = 30001
print(emp1.salary)

面向对象三大特性

封装(隐藏):隐藏对象的属性和方法,只对外提供必要的方法

继承:继承可以让子类具有父类的特性,提高了代码的重用性

多态:同一个方法调用,对象不同而产生不同的行为,例如:同样是休息,人不同休息方法不同,张三休息是睡觉,李四休息是玩游戏

继承

class Person:
    def __init__(self, name, age):
        self.name = name
        self.__age = age  # 私有属性

    def say_age(self):
        print('年龄我也不知道')

    @property
    def age(self):
        return self.__age


class Student(Person):
    def __init__(self, name, age, score):
        Person.__init__(self, name, age)
        self.score = score


print(Student.mro())

s = Student('zf', 18,60)
s.say_age()
print(s.name)
print(dir(s))
print(s._Person__age)

重写:子类可以重新定义父类的方法,这样会覆盖父类的方法,称为重写

class Person:
    def __init__(self, name, age):
        self.name = name
        self.__age = age  # 私有属性

    def say_age(self):
        print('我的年龄是:{0}'.format(self.__age))

    def say_introduce(self):
        print('我的名字是:{0}'.format(self.name))


class Student(Person):
    def __init__(self, name, age, score):
        Person.__init__(self, name, age)
        self.score = score
    '''重写父类的方法'''
    def say_introduce(self):
        print('报告老师我的名字是:{0}'.format(self.name))


s = Student('zf', 18, 80)
s.say_age()
s.say_introduce() #调用了子类重写的方法

Object根类

object类是所有类的父类,因此所有的类都有 object 类的属性和方法

重写__str__方法

class Person:   #默认继承object类
    def __init__(self, name):
        self.name = name

    def __str__(self):
        return '名字是:{0}'.format(self.name)
p = Person('zf')
print(p)

mro()

python支持多继承,如果父类中有重名的方法,在子类没有指定父类的情况下,解释器会按照从左向右按顺序搜索

super()获取父类的定义

class A:
    def say(self):
        print('A', self)


class B(A):
    def say(self):
        super().say()
        print('B')


b = B()
b.say()

多态

多态是方法的多态,属性是没有多态的

多态存在有两个必要条件:继承和方法重写

class Man:
    def eat(self):
        print('饿了,吃饭了')


class Chinese(Man):
    def eat(self):
        print('中国人用筷子吃饭')


class English(Man):
    def eat(self):
        print('英国人用叉子吃饭')


class Indian(Man):
    def eat(self):
        print('印度人用手吃饭')


def manEat(m):
    if isinstance(m, Man):
        m.eat()
    else:
        print('不能吃饭')

特殊方法和运算符重载
class Person:
    def __init__(self, name):
        self.name = name

    def __add__(self, other):
        if isinstance(other, Person):
            return ('{0}--{1}'.format(self.name, other.name))
        else:
            return '不是同类对象,不能相加'

    def __mul__(self, other):
        if isinstance(other, int):
            return self.name * other
        else:
            return '不是同类对象不能想乘'


p1 = Person('zf')
p2 = Person('zf')

p = p1 + p2
print(p)

print(p2 * 2)

特殊属性

# 测试特殊属性
class A:
    pass


class B:
    pass


class C(B, A):
    def __init__(self, nn):
        self.nn = nn

    def cc(self):
        print("cc")


class D(A):
    pass


c = C(33)
print(c.__dict__)   #对象属性字典
print(c.__dir__())  #
print(c.__class__)  #对象所属的类
print(C.__bases__)  #类的基类元组(多继承)
print(C.__base__)   #类的基类
print(C.__mro__)    #类的层次结构
print(A.__subclasses__())   #类的子类列表

组合

“is-a”关系,我们可以使用“继承”。


从而实现子类拥有的父类的方法和属性。


“is-a” 关系指的是类似这样的关系:狗是动物,dog is animal。


狗类就应该继承动物类。


“has-a”关系,我们可以使用“组合”,也能实现一个类拥有另一个类的方法和属性。


” has-a”关系指的是这样的关系:手机拥有 CPU。


MobilePhone has a CPU。


class MobilePhone:
    def __init__(self, cpu, screen):
        self.cpu = cpu
        self.screen = screen


class CPU:
    def c(self):
        print('计算')


class Screen:
    def show(self):
        print('显示')


x = CPU()
y = Screen()
z = MobilePhone(x, y)
z.cpu.c()
z.screen.show()

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/571133.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-09
下一篇 2022-04-09

发表评论

登录后才能评论

评论列表(0条)

保存