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: ...
随机推荐
- 自动化kolla-ansible部署ubuntu20.04+openstack-victoria之镜像制作ubuntu16.04-16
自动化kolla-ansible部署ubuntu20.04+openstack-victoria之镜像制作ubuntu16.04-16 欢迎加QQ群:1026880196 进行交流学习 制作Ope ...
- [Vue warn]: Unknown custom element: <terminal-process> - did you register the component correctly? For recursive components, make sure to provide the "name" option.
Vue组件注册报错问题 import 不要加{},排查出如果页面引用单个组件的时候不要加上{}中括号,引入多个组件时才能派上用场,中括号去除问题即可解决.
- 算法、数据结构、与设计模式等在游戏开发中的运用 (一):单例设计(Singleton Design)
算法.数据结构.与设计模式等在游戏开发中的运用 (一):单例设计(Singleton Design) 作者: Compasslg 李涵威 1. 什么是单例设计(Singleton Design) 在学 ...
- 1017. Convert to Base -2
Given a number N, return a string consisting of "0"s and "1"s that represents it ...
- NetCore去注册Eureka
首先先安装nuget组件:Steeltoe.Discovery.ClientCore 然后在ConfigureServices中进行注入 services.AddDiscoveryClient(Con ...
- hdu2167 方格取数 状态压缩dp
题意: 方格取数,八个方向的限制. 思路: 八个方向的不能用最大流了,四个的可以,八个的不能抽象成二分图,所以目测只能用dp来跑,dp[i][j]表示的是第i行j状态的最优,具体看 ...
- POJ3277 线段树段更新,点询问+二分离散化+暴力
题意: x轴上有一些矩形,问你这些矩形覆盖的面积和是多少. 思路: 首先范围很大,n很小,果断离散化,然后我们就是求出任意区间的最大值作为当前区间的高,最后在算一遍答案就行了, ...
- 用PS给视频磨皮美颜
无意间找到的,但是一个10分钟的视频渲染了我一天的时间,但是效果是不错的 参考视频链接 https://www.bilibili.com/video/BV1b7411m74e 视频中涉及的添加插件链接 ...
- @shiro.hasPermission 使用
在页面上加上@shiro.hasPermission 如下用.ftl为例子: 当加上shiro标签后,会与后台代码结合使用: 需要继承AuthorizingRealm 下的 protected Au ...
- SwiftUI 简明教程之指示器
本文为 Eul 样章,如果您喜欢,请移步 AppStore/Eul 查看更多内容. Eul 是一款 SwiftUI & Combine 教程 App(iOS.macOS),以文章(文字.图片. ...