026.Python面向对象类的相关操作以及对象和类的删除操作
类的相关操作
- 定义的类访问共有成员的成员和方法
- 定义的类动态添加公有成员的属性和方法
- 定义的类删除公有成员的属性和方法
1 定义一个基本的类
#定义一个类
class Plane():
#添加一个共有成员属性
capitain = "John"
#添加一个私有成员属性
__flight_attendant = 20
#共有绑定方法
def fly(self):
print ("飞机飞行速度更快")
# 共有普通方法,这个只能是使用类来调用
def fly2():
print("飞机是速度最快的交通工具")
#定义的类访问公有成员的属性和方法
print(Plane.capitain)
Plane.fly2()
执行
[root@node10 python]# python3 test.py
John
飞机是速度最快的交通工具
类外无法调用一个私有成员
class Plane():
#添加一个共有成员属性
capitain = "John"
#添加一个私有成员属性
__flight_attendant = 20
#共有绑定方法
def fly(self):
print ("飞机飞行速度更快")
# 共有普通方法,这个只能是使用类来调用
def fly2():
print("飞机是速度最快的交通工具")
#定义的类访问公有成员的属性和方法
print(Plane.capitain)
Plane.fly2()
print(Plane.__flight_attendant)
执行报错

普通的方法无法调用,因为形参实参不匹配
class Plane():
#添加一个共有成员属性
capitain = "John"
#添加一个私有成员属性
__flight_attendant = 20
#共有绑定方法
def fly(self):
print ("飞机飞行速度更快")
# 共有普通方法,这个只能是使用类来调用
def fly2():
print("飞机是速度最快的交通工具")
#定义的类访问公有成员的属性和方法
print(Plane.capitain)
Plane.fly2() obj = Plane()
obj.fly2()
调用报错

2 定义的类动态添加公有成员属性和方法
- 类只有一个,而对象可以实例化多个
- 多个对象都可以访问类中的公有成员属性方法
- 而类无法访问对象中的成员
- 对象和对象之间彼此独立,资源不共享.
- 对象可以调用类中公有成员,有使用权,没有归属权(不能修改或者删除)
class Plane():
#添加一个共有成员属性
capitain = "John"
#添加一个私有成员属性
__flight_attendant = 20
#共有绑定方法
def fly(self):
print ("飞机飞行速度更快")
# 共有普通方法,这个只能是使用类来调用
def fly2():
print("飞机是速度最快的交通工具")
#定义的类访问公有成员的属性和方法
print(Plane.capitain)
Plane.fly2() Plane.logo = "波音747"
res = Plane.__dict__
print (res)
执行
[root@node10 python]# python3 test.py
John
飞机是速度最快的交通工具
{'__module__': '__main__', 'capitain': 'John', '_Plane__flight_attendant': 20, 'fly': <function Plane.fly at 0x7fd70e7900d0>, 'fly2': <function Plane.fly2 at 0x7fd70e790158>, '__dict__': <attribute '__dict__' of 'Plane' objects>, '__weakref__': <attribute '__weakref__' of 'Plane' objects>, '__doc__': None, 'logo': '波音747'}
添加方法
class Plane():
#添加一个共有成员属性
capitain = "John"
#添加一个私有成员属性
__flight_attendant = 20
#共有绑定方法
def fly(self):
print ("飞机飞行速度更快")
# 共有普通方法,这个只能是使用类来调用
def fly2():
print("飞机是速度最快的交通工具")
#定义的类访问公有成员的属性和方法
print(Plane.capitain)
Plane.fly2() Plane.logo = "波音747"
res = Plane.__dict__
print (res) #添加无参方法
def raining():
print ("飞机可以人工降雨")
Plane.raining = raining
Plane.raining() #添加有参方法
def scanning(behavior):
print ("飞机可以用来"+behavior) Plane.scanning = scanning
Plane.scanning("侦察敌情") #通过lambda表达式添加
Plane.save = lambda : print ("飞机可以用来紧急救援")
Plane.save() print(Plane.__dict__) #使用对象
obj2 = Plane()
#查看这这对象的属性是空的
print (obj2.__dict__)
#但是可以调用类的属性
obj2.fly()
执行
[root@node10 python]# python3 test.py
John
飞机是速度最快的交通工具
{'__module__': '__main__', 'capitain': 'John', '_Plane__flight_attendant': 20, 'fly': <function Plane.fly at 0x7f295f0f80d0>, 'fly2': <function Plane.fly2 at 0x7f295f0f8158>, '__dict__': <attribute '__dict__' of 'Plane' objects>, '__weakref__': <attribute '__weakref__' of 'Plane' objects>, '__doc__': None, 'logo': '波音747'}
飞机可以人工降雨
飞机可以用来侦察敌情
飞机可以用来紧急救援
{'__module__': '__main__', 'capitain': 'John', '_Plane__flight_attendant': 20, 'fly': <function Plane.fly at 0x7f295f0f80d0>, 'fly2': <function Plane.fly2 at 0x7f295f0f8158>, '__dict__': <attribute '__dict__' of 'Plane' objects>, '__weakref__': <attribute '__weakref__' of 'Plane' objects>, '__doc__': None, 'logo': '波音747', 'raining': <function raining at 0x7f295f1d8e18>, 'scanning': <function scanning at 0x7f295f0f81e0>, 'save': <function <lambda> at 0x7f295f0f8268>}
{}
飞机飞行速度更快
3 对象去调用方法
- 当对象去调用方法时,系统会自动把obj当成参数进行传递,fly中的self自动进行接收
- 在类中要么使用对象.属性或者方法 要么使用类.属性或者方法,其他调用情况都是错误的
class Plane():
#添加一个共有成员属性
capitain = "John"
#添加一个私有成员属性
__Price = "飞机的价格是5亿元"
#共有绑定方法
def fly(self):
print ("飞机飞行速度更快,机长是:",self.capitain)
# 共有普通方法,这个只能是使用类来调用
def raining():
print ("飞机可以人工降雨,机长是",Plane.capitain) #私有绑定方法
def __radar_frequency(self):
print("雷达的频率是10万兆赫,价格是:",self.__Price) #私有普通方法
def plane_price():
print ("我的飞机飞行很快,价格是:",Plane.__Price) obj = Plane()
obj.fly() #obj当成参数进行传递,fly中的self自动进行接收
print (obj.capitain) #self.capitain <==> obj.capitain
执行
飞机飞行速度更快,机长是: John
John
4 类外调用私有成员
在类外不可以调用私有的成员属性方法,可以在类内使用公有方法调用私有成员属性和方法
class Plane():
#添加一个共有成员属性
capitain = "John"
#添加一个私有成员属性
__Price = "飞机的价格是5亿元"
#共有绑定方法
def fly(self):
print ("飞机飞行速度更快,机长是:",self.capitain)
# 共有普通方法,这个只能是使用类来调用
def raining():
print ("飞机可以人工降雨,机长是",Plane.capitain) #私有绑定方法
def __radar_frequency(self):
print("雷达的频率是10万兆赫,价格是:",self.__Price) #私有普通方法
def plane_price():
print ("我的飞机飞行很快,价格是:",Plane.__Price)
obj = Plane()
obj.fly()
print (obj.capitain)
obj. __radar_frequency()
执行

在类内使用公有方法调用私有成员属性和方法
class Plane():
#添加一个共有成员属性
capitain = "John"
#添加一个私有成员属性
__Price = "飞机的价格是5亿元"
#共有绑定方法
def fly(self):
print ("飞机飞行速度更快,机长是:",self.capitain)
# 共有普通方法,这个只能是使用类来调用
def raining():
print ("飞机可以人工降雨,机长是",Plane.capitain) #私有绑定方法
def __radar_frequency(self):
print("雷达的频率是10万兆赫,价格是:",self.__Price) #私有普通方法
def __plane_price():
print ("我的飞机飞行很快,价格是:",Plane.__Price)
#公有方法调用私有成员,用对象来调用
def plane_price_info(self):
print (self.__Price)
self.__radar_frequency()
#也可以使用类来调用
def plane_price_info2():
print(Plane.__Price)
Plane. __plane_price()
obj = Plane()
obj.fly()
print (obj.capitain)
#用对象来调用的方法
obj.plane_price_info()
#用类来调用的方法
Plane.plane_price_info2()
执行
[root@node10 python]# python3 test.py
飞机飞行速度更快,机长是: John
John
飞机的价格是5亿元
雷达的频率是10万兆赫,价格是: 飞机的价格是5亿元
飞机的价格是5亿元
我的飞机飞行很快,价格是: 飞机的价格是5亿元
5 类外直接调用私有成员
需要用到改名机制:
私有成员的名字 => _类名+私有成员本身
其他语言当中,如果是私有的,无论用什么方式都调用不了.
class Plane():
#添加一个共有成员属性
capitain = "John"
#添加一个私有成员属性
__Price = "飞机的价格是5亿元"
#共有绑定方法
def fly(self):
print ("飞机飞行速度更快,机长是:",self.capitain)
# 共有普通方法,这个只能是使用类来调用
def raining():
print ("飞机可以人工降雨,机长是",Plane.capitain) #私有绑定方法
def __radar_frequency(self):
print("雷达的频率是10万兆赫,价格是:",self.__Price) #私有普通方法
def __plane_price():
print ("我的飞机飞行很快,价格是:",Plane.__Price)
#公有方法调用私有成员,用对象来调用
def plane_price_info(self):
print (self.__Price)
self.__radar_frequency()
#也可以使用类来调用
def plane_price_info2():
print(Plane.__Price)
Plane. __plane_price()
obj = Plane()
obj.fly()
print (obj.capitain)
#用对象来调用的方法
obj.plane_price_info()
#用类来调用的方法
Plane.plane_price_info2()
print (Plane.__dict__)
print (Plane._Plane__Price)
obj._Plane__radar_frequency()
执行
[root@node10 python]# python3 test.py
飞机飞行速度更快,机长是: John
John
飞机的价格是5亿元
雷达的频率是10万兆赫,价格是: 飞机的价格是5亿元
飞机的价格是5亿元
我的飞机飞行很快,价格是: 飞机的价格是5亿元
{'__module__': '__main__', 'capitain': 'John', '_Plane__Price': '飞机的价格是5亿元', 'fly': <function Plane.fly at 0x7fa2661090d0>, 'raining': <function Plane.raining at 0x7fa266109158>, '_Plane__radar_frequency': <function Plane.__radar_frequency at 0x7fa2661091e0>, '_Plane__plane_price': <function Plane.__plane_price at 0x7fa266109268>, 'plane_price_info': <function Plane.plane_price_info at 0x7fa2661092f0>, 'plane_price_info2': <function Plane.plane_price_info2 at 0x7fa266109378>, '__dict__': <attribute '__dict__' of 'Plane' objects>, '__weakref__': <attribute '__weakref__' of 'Plane' objects>, '__doc__': None}
飞机的价格是5亿元
雷达的频率是10万兆赫,价格是: 飞机的价格是5亿元
6 删除对象中或者类的成员
用关键字del
- capitain 默认归属于类中的,obj对象可以使用,
- 但是无权修改或者删除,除非obj当中也有capitain属性.
实例化的对象删除公有成员属性和方法,定义的类删除公有成员属性和方法
class Plane():
#添加一个共有成员属性
capitain = "John"
#添加一个私有成员属性
__Price = "飞机的价格是5亿元"
#共有绑定方法
def fly(self):
print ("飞机飞行速度更快,机长是:",self.capitain)
# 共有普通方法,这个只能是使用类来调用
def raining():
print ("飞机可以人工降雨,机长是",Plane.capitain) #私有绑定方法
def __radar_frequency(self):
print("雷达的频率是10万兆赫,价格是:",self.__Price) #私有普通方法
def __plane_price():
print ("我的飞机飞行很快,价格是:",Plane.__Price)
#公有方法调用私有成员,用对象来调用
def plane_price_info(self):
print (self.__Price)
self.__radar_frequency()
#也可以使用类来调用
def plane_price_info2():
print(Plane.__Price)
Plane. __plane_price()
obj = Plane()
obj.fly()
print (obj.capitain)
#用对象来调用的方法
obj.plane_price_info()
#用类来调用的方法
Plane.plane_price_info2()
print (Plane.__dict__)
print (Plane._Plane__Price)
obj._Plane__radar_frequency()
del obj.capitain
执行

可以定义一个再删除
class Plane():
#添加一个共有成员属性
capitain = "John"
#添加一个私有成员属性
__Price = "飞机的价格是5亿元"
#共有绑定方法
def fly(self):
print ("飞机飞行速度更快,机长是:",self.capitain)
# 共有普通方法,这个只能是使用类来调用
def raining():
print ("飞机可以人工降雨,机长是",Plane.capitain) #私有绑定方法
def __radar_frequency(self):
print("雷达的频率是10万兆赫,价格是:",self.__Price) #私有普通方法
def __plane_price():
print ("我的飞机飞行很快,价格是:",Plane.__Price)
#公有方法调用私有成员,用对象来调用
def plane_price_info(self):
print (self.__Price)
self.__radar_frequency()
#也可以使用类来调用
def plane_price_info2():
print(Plane.__Price)
Plane. __plane_price()
obj = Plane()
obj.fly()
print (obj.capitain)
#用对象来调用的方法
obj.plane_price_info()
#用类来调用的方法
Plane.plane_price_info2()
print (Plane.__dict__)
print (Plane._Plane__Price)
obj._Plane__radar_frequency()
print (obj.__dict__)
obj.capitain = "Json"
print (obj.__dict__)
del obj.capitain
print (obj.__dict__)
执行
飞机飞行速度更快,机长是: John
John
飞机的价格是5亿元
雷达的频率是10万兆赫,价格是: 飞机的价格是5亿元
飞机的价格是5亿元
我的飞机飞行很快,价格是: 飞机的价格是5亿元
{'__module__': '__main__', 'capitain': 'John', '_Plane__Price': '飞机的价格是5亿元', 'fly': <function Plane.fly at 0x7f76ee20e0d0>, 'raining': <function Plane.raining at 0x7f76ee20e158>, '_Plane__radar_frequency': <function Plane.__radar_frequency at 0x7f76ee20e1e0>, '_Plane__plane_price': <function Plane.__plane_price at 0x7f76ee20e268>, 'plane_price_info': <function Plane.plane_price_info at 0x7f76ee20e2f0>, 'plane_price_info2': <function Plane.plane_price_info2 at 0x7f76ee20e378>, '__dict__': <attribute '__dict__' of 'Plane' objects>, '__weakref__': <attribute '__weakref__' of 'Plane' objects>, '__doc__': None}
飞机的价格是5亿元
雷达的频率是10万兆赫,价格是: 飞机的价格是5亿元
{}
{'capitain': 'Json'}
{}
从类删除,就没有了
class Plane():
#添加一个共有成员属性
capitain = "John"
#添加一个私有成员属性
__Price = "飞机的价格是5亿元"
#共有绑定方法
def fly(self):
print ("飞机飞行速度更快,机长是:",self.capitain)
# 共有普通方法,这个只能是使用类来调用
def raining():
print ("飞机可以人工降雨,机长是",Plane.capitain) #私有绑定方法
def __radar_frequency(self):
print("雷达的频率是10万兆赫,价格是:",self.__Price) #私有普通方法
def __plane_price():
print ("我的飞机飞行很快,价格是:",Plane.__Price)
#公有方法调用私有成员,用对象来调用
def plane_price_info(self):
print (self.__Price)
self.__radar_frequency()
#也可以使用类来调用
def plane_price_info2():
print(Plane.__Price)
Plane. __plane_price()
obj = Plane()
obj.fly()
print (obj.capitain)
print (Plane.capitain)
#删除capitain
del Plane.capitain
print (Plane.capitain)
执行

删除方法,直接得了删除
class Plane():
#添加一个共有成员属性
capitain = "John"
#添加一个私有成员属性
__Price = "飞机的价格是5亿元"
#共有绑定方法
def fly(self):
print ("飞机飞行速度更快,机长是:",self.capitain)
# 共有普通方法,这个只能是使用类来调用
def raining():
print ("飞机可以人工降雨,机长是",Plane.capitain) #私有绑定方法
def __radar_frequency(self):
print("雷达的频率是10万兆赫,价格是:",self.__Price) #私有普通方法
def __plane_price():
print ("我的飞机飞行很快,价格是:",Plane.__Price)
#公有方法调用私有成员,用对象来调用
def plane_price_info(self):
print (self.__Price)
self.__radar_frequency()
#也可以使用类来调用
def plane_price_info2():
print(Plane.__Price)
Plane. __plane_price()
obj = Plane()
obj.fly()
print (obj.capitain)
print (Plane.__dict__)
#删除方法
del Plane.raining
print (Plane.__dict__)
执行
[root@node10 python]# python3 test.py
飞机飞行速度更快,机长是: John
John
{'__module__': '__main__', 'capitain': 'John', '_Plane__Price': '飞机的价格是5亿元', 'fly': <function Plane.fly at 0x7fea9ba7d0d0>, 'raining': <function Plane.raining at 0x7fea9ba7d158>, '_Plane__radar_frequency': <function Plane.__radar_frequency at 0x7fea9ba7d1e0>, '_Plane__plane_price': <function Plane.__plane_price at 0x7fea9ba7d268>, 'plane_price_info': <function Plane.plane_price_info at 0x7fea9ba7d2f0>, 'plane_price_info2': <function Plane.plane_price_info2 at 0x7fea9ba7d378>, '__dict__': <attribute '__dict__' of 'Plane' objects>, '__weakref__': <attribute '__weakref__' of 'Plane' objects>, '__doc__': None}
{'__module__': '__main__', 'capitain': 'John', '_Plane__Price': '飞机的价格是5亿元', 'fly': <function Plane.fly at 0x7fea9ba7d0d0>, '_Plane__radar_frequency': <function Plane.__radar_frequency at 0x7fea9ba7d1e0>, '_Plane__plane_price': <function Plane.__plane_price at 0x7fea9ba7d268>, 'plane_price_info': <function Plane.plane_price_info at 0x7fea9ba7d2f0>, 'plane_price_info2': <function Plane.plane_price_info2 at 0x7fea9ba7d378>, '__dict__': <attribute '__dict__' of 'Plane' objects>, '__weakref__': <attribute '__weakref__' of 'Plane' objects>, '__doc__': None}
026.Python面向对象类的相关操作以及对象和类的删除操作的更多相关文章
- DOM对象本身也是一个js对象,所以严格来说,并不是操作这个对象慢,而是说操作了这个对象后,会触发一些浏览器行为(转)
一直都听说DOM很慢,要尽量少的去操作DOM,于是就想进一步去探究下为什么大家都会这样说,在网上学习了一些资料,这边整理出来. 首先,DOM对象本身也是一个js对象,所以严格来说,并不是操作这个对象慢 ...
- Python面向对象 -- slots, @property、多重继承MixIn、定制类(str, iter, getitem, getattr, call, callable函数,可调用对象)、元类(type, metaclass)
面向对象设计中最基础的3个概念:数据封装.继承和多态 动态给class增加功能 正常情况下,当定义了一个class,然后创建了一个class的实例后,可以在程序运行的过程中给该实例绑定任何属性和方法, ...
- python 面向对象静态方法、类方法、属性方法、类的特殊成员方法
静态方法:只是名义上归类管理,实际上在静态方法里访问不了类或实例中的任何属性. 在类中方法定义前添加@staticmethod,该方法就与类中的其他(属性,方法)没有关系,不能通过实例化类调用方法使用 ...
- python面向对象编程(2)—— 实例属性,类属性,类方法,静态方法
1 实例属性和类属性 类和实例都是名字空间,类是类属性的名字空间,实例则是实例属性的名字空间. 类属性可通过类或实例来访问.只有通过类名访问时,才能修改类属性的值. 例外的一种情况是,当类属性是一个 ...
- Python学习第十五课——类的基本思想(实例化对象,类对象)
类的基本思想 类:把一类事物的相同的特征和动作整合到一起就是类类是一个抽象的概念 对象:就是基于类而创建的一个具体的事物(具体存在的)也是特征和动作整合到一块 对象写法 # 对象写法 def scho ...
- Python面向对象 -- 继承和多态、获取对象信息、实例属性和类属性
继承和多态 继承的好处: 1,子类可以使用父类的全部功能 2,多态:当子类和父类都存在相同的方法时,子类的方法会覆盖父类的方法,即调用时会调用子类的方法.这就是继承的另一个好处:多态. 多态: 调用方 ...
- Python面向对象总结及类与正则表达式
Python3 面向对象 一丶面向对象技术简介 类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法.对象是类的实例. 方法:类中定义的函数. 类变 ...
- python基础-9.1 面向对象进阶 super 类对象成员 类属性 私有属性 查找源码类对象步骤 类特殊成员 isinstance issubclass 异常处理
上一篇文章介绍了面向对象基本知识: 面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 是一个模板,模板中包装了多个“函数”供使用(可以讲多函数中公用的变量封装到对象中) 对象 ...
- python开发学习-day07(面向对象之多态、类的方法、反射、新式类and旧式类、socket编程)
s12-20160227-day07 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...
随机推荐
- 使用Docker Toolbox 创建Docker虚拟机的方法-注意正确使用本地文件 file:参数的路径名
使用Docker Toolbox 创建v1.12.6版的Docker虚拟机的方法, 一定要注意正确使用本地文件 file:// 参数的路径名, 之前尝试创建过多次,一直都没有成功过, 无法使用 fil ...
- 构建之法与CI/CD
项目 内容 这个作业属于哪个课程 2021春季软件工程(罗杰 任健) 这个作业的要求在哪里 个人阅读作业2 我在这个课程的目标是 认识软工,拥抱软工,提升相关能力以便日后与其朝夕相伴 这个作业在哪个具 ...
- 简述Java多线程(一)
JAVA多线程 程序:是指令和数据的有序集合,其本身没有任何运行的含义,是一个静态的概念. 进程:是执行程序的一次执行过程,是一个动态的概念,是系统资源分配的单位. 线程是CPU调度和执行的单位. 创 ...
- 重绘DevExpress的XtraMessageBox消息提示框控件
先来看提示框,可以看到框其实是一个去掉最大化.最小化按钮后的窗体,窗体的内容就是我们想要提示的内容,重绘提示框其实就是重绘窗体以及中间部分的内容. 首先重绘窗体,消息提示框的窗体不是XtraForm而 ...
- Word Reversal(string)
For each list of words, output a line with each word reversed without changing the order of the word ...
- 硬件篇-04-SLAM移动底盘机械设计
这篇比较水,发出来主要是为了呼应专栏主题,既然是实现,那各个方面都得讲一下不是. 底盘SW模型 淘的,主要是看上了它有弹簧阻尼器,适合野外,抗震,但是这种底盘结构转向起来比较吃力.是再有个全轮 ...
- 关于Number、parseInt、isNaN转化参数
1.首先,关于NaN的相等判断 alert(NaN==NaN) //返回的是false: 2.isNaN 确定这个参数是否是数值或者是否可以被转化为数值:NaN是not a number 的缩写,所以 ...
- POJ2553 强连通出度为0的应用
题意: 给你一个有向图,然后问你有多少个满足要求的点,要求是: 这个点能走到的所有点都能走回这个点,找到所有的这样的点,然后排序输出. 思路: 可以直接一遍强连通缩点,所点之后 ...
- UVA11174村民排队问题
题意: 有n个人要排队,给你一些父子关系,要求儿子不能站在自己的父亲前面,问有多少种排队方式? 思路: 白书上的题目,首先我们可以把关系建成树,这样我们就有可能得到一个森林(或者 ...
- Windows核心编程 第26章 窗口消 息
窗 口 消 息 Wi n d o w s允许一个进程至多建立10 000个不同类型的用户对象(User object):图符.光标.窗口类.菜单.加速键表等等.当一个线程调用一个函数来建立某个对象时, ...