首页 技术 正文
技术 2022年11月15日
0 收藏 775 点赞 3,747 浏览 10730 个字

类的相关操作

  1. 定义的类访问共有成员的成员和方法
  2. 定义的类动态添加公有成员的属性和方法
  3. 定义的类删除公有成员的属性和方法

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}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,994
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,507
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,350
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,135
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,768
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,845