python Class:面向对象高级编程
admin
2023-07-23 22:22:56
0

一、Class添加新方法: MethodType

  1. 外挂类


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')

python Class:面向对象高级编程

print dog.color

python Class:面向对象高级编程

cat.set_color('white_yellow')

python Class:面向对象高级编程


由此可见,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')

python Class:面向对象高级编程

cat.set_color('white_yellow')
python Class:面向对象高级编程


格式图:

python Class:面向对象高级编程




二、Class限制添加新元素:__slots__

  1. 普通的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

python Class:面向对象高级编程


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

python Class:面向对象高级编程

dog.color = 'yellow'

python Class:面向对象高级编程


!!!但是!!!!!

对于继承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:面向对象高级编程



相关内容

热门资讯

风云对话 | 太空医疗研究:我... 6月23-25日,第十七届夏季达沃斯在大连举办,论坛主题为 “规模化创新”。全球局势纷繁复杂,德国太...
多人将两女子和狗“浸猪笼”游街... 6月25日,有网友发布视频称,湖南岳阳汨罗市一街道上出现多名男女当街拉人“浸猪笼”游行。视频画面显示...
驻委内瑞拉使馆提醒中国公民防范... 驻委内瑞拉使馆提醒在委中国公民密切关注当地地震预警和灾害相关信息,防范余震及地震导致的次生灾害;保持...
空调自清洁可以中断吗 空调自清洁是可以中断的。空调自清洁功能是独立按键,在空调清洁完成前,再次按下自清洁即可结束,如出现不...
前锋燃气灶自动熄火是什么原因引... 造成燃气灶熄火的原因有几种:天然气不足:燃气灶燃烧一段时间出现自动熄灭,同时会出现噔噔能的响声,可能...
燃气灶放上锅以后就自动熄火的原... 主要原因是,如果出现燃气灶不放锅空烧的时候不会熄火,放上锅之后才熄火,那么原因是燃气灶燃烧时火苗无法...
承压式太阳能热水器优点介绍 现如今,热水器在我们的生活中越来越常见了,它已经成为了我们生活中的必备品了。热水器的注重作用就是提供...
洗衣机吊扇的制作方法 制作洗衣机吊扇的方法: 材料准备: - 旧式洗衣机电机 - 电动工具(如手电钻) - 木材(...
唐驳虎:一场7.5级强震,照见... 作者丨唐驳虎核心提要1. 当地时间6月24日傍晚,委内瑞拉加勒比海沿岸一分钟内连续发生两次强震。美国...
念念不忘“未获支持”,特朗普又... 【环球网报道 记者 闫珮云】综合美国《纽约邮报》、俄罗斯卫星通讯社、土耳其阿纳多卢通讯社等媒体报道,...