文章目录
- 1.方法没有重载
- 2.方法的动态性
- 3. 私有属性和私有方法
- 4. @property装饰器
- 5. 继承
- 6. 类成员继承和重写
- 7. 查看类的继承层次结构
- 8. dir()查看对象属性
- 9. 重写__str__()方法
- 10.多重继承
- 11.super()获得父类定义
- 12.多态
- 13.特殊方法和运算符重载
- 14.特殊属性
- 15.对象的浅拷贝和深拷贝
- 16. 组合
- 17. 设计模式_工厂模式实现
- 18. 设计模式_单例模型实现
1.方法没有重载
- python中,方法的参数没有类型(调用时确认参数的类型),参数的数量也可以由可变参数控制.因此,python中是没有方法的重载的
- 定义一个方法即可有多种调用方法,相当于实现了其他语言中的方法的重载
- 如果在我们类体中定义了多个重名的方法,只有最后一个方法有效
class Person:
def say_hi(self):
print('hello')
def say_hi(self,name):
print('{0},hello'.format(name))
p1 = Person()
p1.say_hi('zs')
2.方法的动态性
- python是动态语言,可以动态的为类添加新的方法,或者动态的修改类已有的方法
class Person:
def work(self):
print('努力工作')
def play_game(a):
print('{0}在玩游戏'.format(a))
def work2(s):
print('好好学习,天天向上')
Person.play = play_game
p = Person()
p.work()
p.play()
Person.work = work2
p.work()
3. 私有属性和私有方法
- python对于类的成员没有严格的访问控制限制,这与其他面向对象语言有区别.关于私有属性和方法要点如下:
- 通常两个下划线开头的是私有属性和方法
- 类内部可以访问私有属性(方法)
- 类外部不能直接访问私有属性
- 类外部通过"_类名__私有属性名"访问私有属性(方法)
class Employee:
def __init__(self, name, age):
self.name = name
self.__age = age
def __work(self):
print('好好学习,天天向上')
print('年龄,{0}'.format(self.__age))
e = Employee('zs', 24)
print(e.name)
print(e._Employee__age)
print(dir(e))
e._Employee__work()
4. @property装饰器
- @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这个范围 ")
e = Employee('zs',20000)
print(e.get_salary())
e.set_salary(2000)
print(e.get_salary())
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这个范围 ")
e = Employee('zs',30000)
e.salary = 20000
print(e.salary)
5. 继承
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('zs', 18, 80)
s.say_age()
6. 类成员继承和重写
- 成员继承:子类继承父类除构造方法之外的所有成员
- 方法重写:子类可以重新定义父类中的方法,这样覆盖父类的方法
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
def say_age(self):
print('我很年轻')
print(Student.mro())
s = Student('zs', 18, 80)
s.say_age()
7. 查看类的继承层次结构
class A:pass
class B(A):pass
class C(B):pass
print(C.mro())
8. dir()查看对象属性
- dir(),可以让我们方便的看到指定对象所有的属性
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def say_age(self):
print(self.name, '的年龄是:', self.age)
obj = object()
print(dir(obj))
s1 = Person('zs', 18)
print(dir(s1))
9. 重写__str__()方法
- object有一个__str__()方法,用于返回一个对于"对象的描述",
- 对于内置函数str(),经常用于print()方法,帮助我们查看对象信息
- __str()可以重写
class Person:
def __init__(self, name):
self.name = name
def __str__(self):
return "名字是{}".format(self.name)
p = Person('zs')
print(p)
print(p.name)
10.多重继承
class A:
def aa(self):
print('aa')
def say(self):
print("say AAA")
class B:
def bb(self):
print('bb')
def say(self):
print("say BBB")
class C(B, A):
def cc(self):
print('cc')
c = C()
c.cc()
c.say()
11.super()获得父类定义
- 在子类中,想要获取父类方法时,通过super()来做
- super()是父类的定义,不是父类的对象
class A:
def say(self):
print("A",s elf)
class B(A):
def say(self):
super().say()
print("B", self)
B().say()
12.多态
- 多态指一个方法调用由于对象不同可能会产生不用的行为
- 多态是方法的多态,属性没有多态
- 多态的存在有2个必要条件:继承和方法重写
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('不能吃饭')
manEat(Chinese())
manEat(Indian())
13.特殊方法和运算符重载
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, Person):
return self.name*other
else:
return "不是同类,不能相加"
p1 = Person('zs')
p2 = Person('ls')
x = p1 +p2
print(x)
14.特殊属性
- python对象中包含了很多双下划线开始和结束的属性
|特殊方法|含义 |
|–|--|
|obj.dict | 对象的属性属性字典 |
|obj.class | 对象所属的类 |
|class.bases | 类的基类元祖 |
|class.base | 类的基类|
|class.mro | 类层次结构 |
15.对象的浅拷贝和深拷贝
- 变量的赋值操作
- 浅拷贝
- 深拷贝
import copy
class MobilePhone:
def __init__(self, cpu, screen):
self.cpu = cpu
self.screen = screen
class CPU:
def calculate(self):
print("计算点什么")
class Screen:
def show(self):
print("screen对象", self)
c1 = CPU
c2 = c1
print(c1)
print(c2)
print('*'*10)
s1 = Screen()
m1 = MobilePhone(c1,s1)
m2 =copy.copy(m1)
print(m1, m1.cpu, m1.screen)
print(m2, m2.cpu, m2.screen)
print('*'*10)
m3 =copy.deepcopy(m1)
print(m1, m1.cpu, m1.screen)
print(m3, m3.cpu, m3.screen)
16. 组合
- “is - a"关系,我们可以使用"继承”
- “has - a"关系,我们可以使用"组合”
class A2:
def say_a2(self):
print("a2")
class B2:
def __init__(self, a):
self.a = a
a2 = A2()
b2 = B2(a2)
b2.a.say_a2()
17. 设计模式_工厂模式实现
- 设计模式是面向对象语言特有的内容
- 常用的模式:工厂模式和单例模式
- 工厂模式实现了创建者和调用者而的分离,使用专门的工厂类将选择实现类,创建对象进行统一的管理和控制
18. 设计模式_单例模型实现
- 单例模型的核心作用是确保一个类只有一个实例,并且提供一个访问该实例的全局访问点
- 单例模式只生成一个实例对象,减少了对系统资源的开下,当需要多资源是,可以产生一个"单例对象",然后永久驻留在内存中,从而极大的降低开销
class Mysingleton:
__obj = None
__init_flag =True
def __new__(cls, *args, **kwargs):
if cls.__obj == None:
cls.__obj = object.__new__(cls)
return cls.__obj
def __init__(self, name):
if Mysingleton.__init_flag:
print("init...")
self.name = name
Mysingleton.__init_flag = False
a = Mysingleton('aa')
b = Mysingleton('bb')
print(a)
print(b)
c = Mysingleton('cc')
print(c)