第九节
方法
方法没有重载
私有属性和私有方法(实现封装)
#测试私有属性
class Employee:
def __init__(self,name,age):
self.name=name
self.__age=age
e=Employee('高琪',18)
print(e.name)
#print(e.age)
print(e._Employee__age)
print(dir(e))
输出:
高琪
18
['_Employee__age', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'name']
Process finished with exit code 0
#测试私有属性
class Employee:
__company='程序员'
def __init__(self,name,age):
self.name=name
self.__age=age #私有属性
def __work(self): #私有方法
print('好好工作!')
print('年龄:{0}'.format(self.__age))
e=Employee('高琪',18)
print(e.name)
print(e._Employee__age)
print(dir(e))
e._Employee__work()
print(Employee._Employee__company)
输出:
高琪
18
['_Employee__age', '_Employee__company', '_Employee__work', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'name']
好好工作!
年龄:18
程序员
@property装饰器_get和set方法
普通调用:
class Employee:
def salary(self):
print('salary run ...')
return 10000
emp1=Employee()
emp1.salary()
结果:
salary run ...
Process finished with exit code 0
采用@property
class Employee:
@property
def salary(self):
print('salary run ...')
return 10000
emp1=Employee()
#emp1.salary()
print(emp1.salary)
结果:
salary run ...
10000
Process finished with exit code 0
相当于变成属性的调用
但是当赋值时出现报错
class Employee:
@property
def salary(self):
print('salary run ...')
return 10000
emp1=Employee()
#emp1.salary()
print(emp1.salary)
emp1.salary=30000
salary run ...
10000
Traceback (most recent call last):
File "D:/PycharmProjects/MyTest/Day_0722/mytest05.py", line 18, in <module>
emp1.salary=30000
AttributeError: can't set attribute
Process finished with exit code 1
简单测试@property
class Employee:
def __init__(self,name,salary):
self.name = name
self.salary = salary
emp1=Employee('高琪',30000)
print(emp1.salary)
emp1.salary=20000
print(emp1.salary)
30000
20000
这个时候没问题 简单的读取,但是封装化要求用户在使用的时候要完善,不能当输入-2000时仍然输出-2000,需要有提示的细节
首先是不使用@的
#简单测试@property
class Employee:
def __init__(self,name,salary):
self.__name = name
self.__salary = salary
def get_salary(self):
return self.__salary
def set_salary(self,salary):
if 1000<salary<50000:
self.__salary=salary
else:
print('录入错误!薪水在1000-50000这个范围')
emp1=Employee('高琪',30000)
print(emp1.get_salary())
emp1.set_salary(-20000)
print(emp1.get_salary())
30000
录入错误!薪水在1000-50000这个范围
30000
Process finished with exit code 0
#简单测试@property
class Employee:
def __init__(self,name,salary):
self.__name = name
self.__salary = salary
def get_salary(self):
return self.__salary
def set_salary(self,salary):
if 1000<salary<50000:
self.__salary=salary
else:
print('录入错误!薪水在1000-50000这个范围')
emp1=Employee('高琪',30000)
print(emp1.get_salary())
emp1.set_salary(20000)
print(emp1.get_salary())
30000
20000
加入@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 1000<salary<50000:
self.__salary=salary
else:
print('录入错误!薪水在1000-50000这个范围')
emp1=Employee('高琪',30000)
print(emp1.salary)
emp1.salary=-20000
print(emp1.salary)
30000
录入错误!薪水在1000-50000这个范围
30000
面向对象的三大特征说明(封装、继承、多态)
继承
class Person:
def __init__(self,name,age):
self.name=name
self.age=age
def say_age(self):
print('不知道')
class Student(Person):
def __init__(self,name,age,score):
Person.__init__(self,name,age)
self.score=score
print(Student.mro())
s=Student('高琪',18,60)
s.say_age()
print(s.name)
[<class '__main__.Student'>, <class '__main__.Person'>, <class 'object'>]
不知道
高琪
#测试继承的基本使用
class Person:
def __init__(self,name,age):
self.name=name
self.__age=age #私有属性
def say_age(self):
print('不知道')
class Student(Person):
def __init__(self,name,age,score):
Person.__init__(self,name,age) #必须显式的调用父类初始化方法,不然解释器不会去调用
self.score=score
print(Student.mro())
s=Student('高琪',18,60)
s.say_age()
print(s.name)
print(dir(s))
print(s._Person__age)
[<class '__main__.Student'>, <class '__main__.Person'>, <class 'object'>]
不知道
高琪
['_Person__age', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'name', 'say_age', 'score']
18
方法的重写(类成员的继承和重写)
class Person:
def __init__(self,name,age):
self.name=name
self.__age=age
def say_age(self):
print('我的年龄是:',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('高琪',18,60)
s.say_age()
s.say_introduce()
我的年龄是: 18
报告老师,我的名字是高琪
Process finished with exit code 0
重写是对父类的方法进行修改
查看类的继承结构
object根类_dir() 查看对象属性
重写__str__()方法
#测试重写object的__str__()
class Person:
def __init__(self,name):
self.name=name
def __str__(self):
return '名字是:{0}'.format(self.name)
p=Person('高琪')
print(p)
名字是:高琪
Process finished with exit code 0
多重继承
mro()
#多重继承
class A:
def aa(self):
print('aa')
def say(self):
print('say AA')
class B:
def bb(self):
print('bb')
def say(self):
print('say BB')
class C(B,A):
def cc(self):
print('cc')
c=C()
print(C.mro()) #打印类的层次结构
c.say() #解释器寻找的方法是从左到右的方式寻找,此时会执行B类中的say()
[<class '__main__.C'>, <class '__main__.B'>, <class '__main__.A'>, <class 'object'>]
say BB
Process finished with exit code 0
super()获得父类的定义
多态
特殊方法和运算符重载
#测试运算符的重载
class Person:
def __init__(self,name):
self.name=name
def __add__(self,other):
if isinstance(self,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('高琪')
p2=Person('高嘻嘻')
x = p1+p2
print(x)
print(p1*3)
高琪--高嘻嘻
高琪高琪高琪
Process finished with exit code 0
特殊属性
对象的浅拷贝和深拷贝
组合
设计模式_工厂模式
设计模式_单例模式