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: ...
随机推荐
- @PostConstruct 使用记录
@PostConstruct 从Java EE5规范开始,Servlet增加了两个影响Servlet生命周期的注解(Annotation):@PostConstruct和@PreConstruct.这 ...
- MindSpore函数拟合
技术背景 在前面一篇博客中我们介绍过基于docker的mindspore编程环境配置,这里我们基于这个环境,使用mindspore来拟合一个线性的函数,演示一下mindspore的基本用法. 环境准备 ...
- .NET HttpWebRequest(请求被中止: 未能创建 SSL/TLS 安全通道)和(基础连接已经关闭: 发送时发生错误)问题查找解决
前言: 前段时间在对接第三方接口的时候发生了一个非常奇葩的问题,就是使用 .NET Framework 4.6 HttpWebRequest进行网络请求的相关问题.背景,关于调用第三方的接口都是使用使 ...
- LinearDiscriminantAnalysis参数、属性和方法
[LDA]线性判别分析 参数: solver:一个字符串,指定了求解最优化问题的算法,可以为如下的值. 'svd':奇异值分解.对于有大规模特征的数据,推荐用这种算法. 'lsqr':最小平方差,可以 ...
- 计算机网络-已知IP地址和子网掩码,求广播地址
首先说结论--广播地址=该IP所在的下一跳-1 例题: 已知IP地址是192.72.20.111,子网掩码是255.255.255.224,求广播地址 要知道下一跳就需要先求出网段间隔,网段间隔=25 ...
- Sql server注入一些tips
sql server环境测试: 几个特性: 1.sql server兼容性可以说是最差的. 举例: select x from y where id=1 字符串查询 select x from y w ...
- Python表达式进阶——列表表达式
x = 0 y = x*2 if x >= 0 else x print(y) # [表达式for变量in列表] l1 = [] l2 = [i for i in range(100) if i ...
- 移动端小总结(1)---meta、input和单行多行文字溢出省略号
一.常用META 1. 添加到主屏后的标题(IOS) 1 <meta name="apple-mobile-web-app-title" content="标题&q ...
- node-util
Node.js 常用工具 util 是一个Node.js 核心模块,提供常用函数的集合,用于弥补核心JavaScript 的功能 过于精简的不足. util.inherits util.inherit ...
- 学习Canvas绘图与动画基础 为多边形着色(三)
1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <meta charset="U ...