-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathoop_extends.py
51 lines (38 loc) · 1.09 KB
/
oop_extends.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# github:https://github.com/tangthis
# 面向对象(高级)
class Student(object):
pass
std = Student()
std.name="张三"
print(std.name)
#实例绑定一个方法
def set_age(self,age):
self.age = age
from types import MethodType
std.set_age = MethodType(set_age,std)
std.set_age(25)
print(std.age)
#给一个实例绑定的方法,对另一个实例是不起作用的
#为了给所有实例都绑定方法,可以给class绑定方法
def set_socre(self,score):
self.score = score
Student.set_socre = MethodType(set_socre,Student)
std.set_socre(99)
print(std.score)
std1 = Student()
std1.set_socre(88)
print(std1.score)
#Python允许在定义class的时候,定义一个特殊的__slots__变量,来限制该class实例能添加的属性
class Student(object):
__slots__ = ('name','age')
s = Student()
s.name = '李理理'
print(s.name)
#使用__slots__要注意,__slots__定义的属性仅对当前类实例起作用,对继承的子类是不起作用的
class GraduateStudent(Student):
pass
g = GraduateStudent();
g.score = 110
print(g.score)