python Class:面向对象高级编程
2018-07-19 15:46
741 查看
一、Class添加新方法: MethodType
外挂类
class Animal(object):
def __init__(self, name, age):
self.name = name
self.age = age
def run(self):
print 'Animal is run'
def set_color(self, color):
self.color = color;
print color
dog = Animal('Pity', 9)
cat = Animal('Miumiu', 9)
from types import MethodType
dog.set_color = MethodType(set_color, dog, Animal)
dog.set_color('yellow')
![](https://oscdn.geek-share.com/Uploads/Images/Content/201912/02/8947a195e5b04334ec5c8056baab1efa)
print dog.color
![](https://oscdn.geek-share.com/Uploads/Images/Content/201912/02/1fe3ce85298bae3e89b8604f8fb7465c)
cat.set_color('white_yellow')
![](https://oscdn.geek-share.com/Uploads/Images/Content/201912/02/9e032899ff8a48adbf25f493ed9a8478)
由此可见,MethodType指定添加在dog对象中,而非Animal中
2.内嵌类
class Animal(object):
def __init__(self, name, age):
self.name = name
self.age = age
def run(self):
print 'Animal is run'def set_color(self, color):
self.color = color;
print color
dog = Animal('Pity', 9)cat = Animal('Miumiu', 9)
from types import MethodType
Animal.set_color = MethodType(set_color, None, Animal)
dog.set_color('yellow')
![](https://oscdn.geek-share.com/Uploads/Images/Content/201912/02/c56561e2f9f9e2e3c92b4ec46ad5ac9a)
cat.set_color('white_yellow')
![](https://oscdn.geek-share.com/Uploads/Images/Content/201912/02/fdece0d6391f63963ff5d8ca2be9bb7b)
格式图:
![](https://oscdn.geek-share.com/Uploads/Images/Content/201912/02/ca501c5e69d96ae6387b96f96e2c4532)
二、Class限制添加新元素:__slots__
普通的Class没有限制添加元素
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
class Animal(object):
def __init__(self, name, age):
self.name = name
self.age = age
def run(self):
print 'Animal is run'
dog = Animal('Pity', 9)
dog.color = 'yellow' #dog实体添加了新元素color
print dog.color
![](https://oscdn.geek-share.com/Uploads/Images/Content/201912/02/5bb25c2d8bd10a8d3f7f10fa31fb556f)
2.限制添加新元素:__slots__
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
class Animal(object):
__slots__ = ('name', 'age') #限制实体添加其他元素
def __init__ (self, name, age):
self.name = name
self.age = age
def run(self):
print 'Animal is run'
dog = Animal('Pity', 9)
print dog.name
![](https://oscdn.geek-share.com/Uploads/Images/Content/201912/02/967626454d8ed9adc70bc47bc72ebafc)
dog.color = 'yellow'
![](https://oscdn.geek-share.com/Uploads/Images/Content/201912/02/d5fc610806c0da0395ee8fe8a17e1266)
!!!但是!!!!!
对于继承Animal的子类来讲,这个方法无效,除非在子类中也添加__slots__
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
class Animal(object):
__slots__ = ('name', 'age')
def __init__ (self, name, age):
self.name = name
self.age = age
def run(self):
print 'Animal is run'
class Cat(Animal): #添加继承Animal的子类
pass
cat = Cat('Miumiu', 9)
cat.color = 'white_yellow'
print cat.color
外挂类
class Animal(object):
def __init__(self, name, age):
self.name = name
self.age = age
def run(self):
print 'Animal is run'
def set_color(self, color):
self.color = color;
print color
dog = Animal('Pity', 9)
cat = Animal('Miumiu', 9)
from types import MethodType
dog.set_color = MethodType(set_color, dog, Animal)
dog.set_color('yellow')
print dog.color
cat.set_color('white_yellow')
由此可见,MethodType指定添加在dog对象中,而非Animal中
2.内嵌类
class Animal(object):
def __init__(self, name, age):
self.name = name
self.age = age
def run(self):
print 'Animal is run'def set_color(self, color):
self.color = color;
print color
dog = Animal('Pity', 9)cat = Animal('Miumiu', 9)
from types import MethodType
Animal.set_color = MethodType(set_color, None, Animal)
dog.set_color('yellow')
cat.set_color('white_yellow')
格式图:
二、Class限制添加新元素:__slots__
普通的Class没有限制添加元素
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
class Animal(object):
def __init__(self, name, age):
self.name = name
self.age = age
def run(self):
print 'Animal is run'
dog = Animal('Pity', 9)
dog.color = 'yellow' #dog实体添加了新元素color
print dog.color
2.限制添加新元素:__slots__
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
class Animal(object):
__slots__ = ('name', 'age') #限制实体添加其他元素
def __init__ (self, name, age):
self.name = name
self.age = age
def run(self):
print 'Animal is run'
dog = Animal('Pity', 9)
print dog.name
dog.color = 'yellow'
!!!但是!!!!!
对于继承Animal的子类来讲,这个方法无效,除非在子类中也添加__slots__
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
class Animal(object):
__slots__ = ('name', 'age')
def __init__ (self, name, age):
self.name = name
self.age = age
def run(self):
print 'Animal is run'
class Cat(Animal): #添加继承Animal的子类
pass
cat = Cat('Miumiu', 9)
cat.color = 'white_yellow'
print cat.color
相关文章推荐
- python Class:面向对象高级编程 @property
- python Class:面向对象高级编程 __getattr__
- python Class:面向对象高级编程 __str__ / __repr__
- python Class:面向对象高级编程 @property
- python Class:面向对象高级编程 __call__& callable()
- python Class:面向对象高级编程 Enum(枚举)、@unique
- python Class:面向对象高级编程 元类:type
- python Class: 面向对象高级编程 __iter__ 和 next()
- python Class:面向对象高级编程 元类:metaclass
- python Class:面向对象高级编程 __getitem__
- 【Python】学习笔记——-8.6、面向对象高级编程:6.使用元类
- Python 面向对象高级编程――使用__slots__
- python面向对象高级编程
- python 2.7 : 面向对象高级编程
- 廖雪峰Python教程 学习笔记11 面向对象高级编程
- Python 面向对象高级编程――使用@property
- Python 第五章 面向对象高级编程
- Python3.X之面向对象高级编程笔记
- Python学习笔记 - 面向对象高级编程
- Python面向对象高级编程:@property--把方法变为属性