python删除类方法_026.Python面向对象类的相关操作以及对象和类的删除操作

类的相关操作

定义的类访问共有成员的成员和方法

定义的类动态添加公有成员的属性和方法

定义的类删除公有成员的属性和方法

1 定义一个基本的类

#定义一个类

classPlane():#添加一个共有成员属性

capitain = "John"

#添加一个私有成员属性

__flight_attendant = 20

#共有绑定方法

deffly(self):print ("飞机飞行速度更快")#共有普通方法,这个只能是使用类来调用

deffly2():print("飞机是速度最快的交通工具")#定义的类访问公有成员的属性和方法

print(Plane.capitain)

Plane.fly2()

执行

[root@node10 python]#python3 test.py

John

飞机是速度最快的交通工具

类外无法调用一个私有成员

classPlane():#添加一个共有成员属性

capitain = "John"

#添加一个私有成员属性

__flight_attendant = 20

#共有绑定方法

deffly(self):print ("飞机飞行速度更快")#共有普通方法,这个只能是使用类来调用

deffly2():print("飞机是速度最快的交通工具")#定义的类访问公有成员的属性和方法

print(Plane.capitain)

Plane.fly2()print(Plane.__flight_attendant)

执行报错

1624149-20200215133750337-2083858002.png

普通的方法无法调用,因为形参实参不匹配

classPlane():#添加一个共有成员属性

capitain = "John"

#添加一个私有成员属性

__flight_attendant = 20

#共有绑定方法

deffly(self):print ("飞机飞行速度更快")#共有普通方法,这个只能是使用类来调用

deffly2():print("飞机是速度最快的交通工具")#定义的类访问公有成员的属性和方法

print(Plane.capitain)

Plane.fly2()

obj=Plane()

obj.fly2()

调用报错

1624149-20200215134022960-44158439.png

2 定义的类动态添加公有成员属性和方法

类只有一个,而对象可以实例化多个

多个对象都可以访问类中的公有成员属性方法

而类无法访问对象中的成员

对象和对象之间彼此独立,资源不共享.

对象可以调用类中公有成员,有使用权,没有归属权(不能修改或者删除)

classPlane():#添加一个共有成员属性

capitain = "John"

#添加一个私有成员属性

__flight_attendant = 20

#共有绑定方法

deffly(self):print ("飞机飞行速度更快")#共有普通方法,这个只能是使用类来调用

deffly2():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': , 'fly2': , '__dict__': , '__weakref__': , '__doc__': None, 'logo': '波音747'}

添加方法

classPlane():#添加一个共有成员属性

capitain = "John"

#添加一个私有成员属性

__flight_attendant = 20

#共有绑定方法

deffly(self):print ("飞机飞行速度更快")#共有普通方法,这个只能是使用类来调用

deffly2():print("飞机是速度最快的交通工具")#定义的类访问公有成员的属性和方法

print(Plane.capitain)

Plane.fly2()

Plane.logo= "波音747"res= Plane.__dict__

print(res)#添加无参方法

defraining():print ("飞机可以人工降雨")

Plane.raining=raining

Plane.raining()#添加有参方法

defscanning(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': , 'fly2': , '__dict__': , '__weakref__': , '__doc__': None, 'logo': '波音747'}

飞机可以人工降雨

飞机可以用来侦察敌情

飞机可以用来紧急救援

{'__module__': '__main__', 'capitain': 'John', '_Plane__flight_attendant': 20, 'fly': , 'fly2': , '__dict__': , '__weakref__': , '__doc__': None, 'logo': '波音747', 'raining': , 'scanning': , 'save': at 0x7f295f0f8268>}

{}

飞机飞行速度更快

3 对象去调用方法

当对象去调用方法时,系统会自动把obj当成参数进行传递,fly中的self自动进行接收

在类中要么使用对象.属性或者方法 要么使用类.属性或者方法,其他调用情况都是错误的

classPlane():#添加一个共有成员属性

capitain = "John"

#添加一个私有成员属性

__Price = "飞机的价格是5亿元"

#共有绑定方法

deffly(self):print ("飞机飞行速度更快,机长是:",self.capitain)#共有普通方法,这个只能是使用类来调用

defraining():print ("飞机可以人工降雨,机长是",Plane.capitain)#私有绑定方法

def __radar_frequency(self):print("雷达的频率是10万兆赫,价格是:",self.__Price)#私有普通方法

defplane_price():print ("我的飞机飞行很快,价格是:",Plane.__Price)obj=Plane()

obj.fly() #obj当成参数进行传递,fly中的self自动进行接收print (obj.capitain) #self.capitain <==> obj.capitain

执行

飞机飞行速度更快,机长是: John

John

4 类外调用私有成员

在类外不可以调用私有的成员属性方法,可以在类内使用公有方法调用私有成员属性和方法

classPlane():#添加一个共有成员属性

capitain = "John"

#添加一个私有成员属性

__Price = "飞机的价格是5亿元"

#共有绑定方法

deffly(self):print ("飞机飞行速度更快,机长是:",self.capitain)#共有普通方法,这个只能是使用类来调用

defraining():print ("飞机可以人工降雨,机长是",Plane.capitain)#私有绑定方法

def __radar_frequency(self):print("雷达的频率是10万兆赫,价格是:",self.__Price)#私有普通方法

defplane_price():print ("我的飞机飞行很快,价格是:",Plane.__Price)obj=Plane()

obj.fly()print(obj.capitain)

obj.__radar_frequency()

执行

1624149-20200215144911601-1472559311.png

在类内使用公有方法调用私有成员属性和方法

classPlane():#添加一个共有成员属性

capitain = "John"

#添加一个私有成员属性

__Price = "飞机的价格是5亿元"

#共有绑定方法

deffly(self):print ("飞机飞行速度更快,机长是:",self.capitain)#共有普通方法,这个只能是使用类来调用

defraining():print ("飞机可以人工降雨,机长是",Plane.capitain)#私有绑定方法

def __radar_frequency(self):print("雷达的频率是10万兆赫,价格是:",self.__Price)#私有普通方法

def __plane_price():print ("我的飞机飞行很快,价格是:",Plane.__Price)#公有方法调用私有成员,用对象来调用

defplane_price_info(self):print (self.__Price)

self.__radar_frequency()#也可以使用类来调用

defplane_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 类外直接调用私有成员

需要用到改名机制:

私有成员的名字 => _类名+私有成员本身

其他语言当中,如果是私有的,无论用什么方式都调用不了.

classPlane():#添加一个共有成员属性

capitain = "John"

#添加一个私有成员属性

__Price = "飞机的价格是5亿元"

#共有绑定方法

deffly(self):print ("飞机飞行速度更快,机长是:",self.capitain)#共有普通方法,这个只能是使用类来调用

defraining():print ("飞机可以人工降雨,机长是",Plane.capitain)#私有绑定方法

def __radar_frequency(self):print("雷达的频率是10万兆赫,价格是:",self.__Price)#私有普通方法

def __plane_price():print ("我的飞机飞行很快,价格是:",Plane.__Price)#公有方法调用私有成员,用对象来调用

defplane_price_info(self):print (self.__Price)

self.__radar_frequency()#也可以使用类来调用

defplane_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': , 'raining': , '_Plane__radar_frequency': , '_Plane__plane_price': , 'plane_price_info': , 'plane_price_info2': , '__dict__': , '__weakref__': , '__doc__': None}

飞机的价格是5亿元

雷达的频率是10万兆赫,价格是: 飞机的价格是5亿元

6 删除对象中或者类的成员

用关键字del

capitain 默认归属于类中的,obj对象可以使用,

但是无权修改或者删除,除非obj当中也有capitain属性.

实例化的对象删除公有成员属性和方法,定义的类删除公有成员属性和方法

classPlane():#添加一个共有成员属性

capitain = "John"

#添加一个私有成员属性

__Price = "飞机的价格是5亿元"

#共有绑定方法

deffly(self):print ("飞机飞行速度更快,机长是:",self.capitain)#共有普通方法,这个只能是使用类来调用

defraining():print ("飞机可以人工降雨,机长是",Plane.capitain)#私有绑定方法

def __radar_frequency(self):print("雷达的频率是10万兆赫,价格是:",self.__Price)#私有普通方法

def __plane_price():print ("我的飞机飞行很快,价格是:",Plane.__Price)#公有方法调用私有成员,用对象来调用

defplane_price_info(self):print (self.__Price)

self.__radar_frequency()#也可以使用类来调用

defplane_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

执行

1624149-20200215152657052-434759219.png

可以定义一个再删除

classPlane():#添加一个共有成员属性

capitain = "John"

#添加一个私有成员属性

__Price = "飞机的价格是5亿元"

#共有绑定方法

deffly(self):print ("飞机飞行速度更快,机长是:",self.capitain)#共有普通方法,这个只能是使用类来调用

defraining():print ("飞机可以人工降雨,机长是",Plane.capitain)#私有绑定方法

def __radar_frequency(self):print("雷达的频率是10万兆赫,价格是:",self.__Price)#私有普通方法

def __plane_price():print ("我的飞机飞行很快,价格是:",Plane.__Price)#公有方法调用私有成员,用对象来调用

defplane_price_info(self):print (self.__Price)

self.__radar_frequency()#也可以使用类来调用

defplane_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__)delobj.capitainprint (obj.__dict__)

执行

飞机飞行速度更快,机长是: John

John

飞机的价格是5亿元

雷达的频率是10万兆赫,价格是: 飞机的价格是5亿元

飞机的价格是5亿元

我的飞机飞行很快,价格是: 飞机的价格是5亿元

{'__module__': '__main__', 'capitain': 'John', '_Plane__Price': '飞机的价格是5亿元', 'fly': , 'raining': , '_Plane__radar_frequency': , '_Plane__plane_price': , 'plane_price_info': , 'plane_price_info2': , '__dict__': , '__weakref__': , '__doc__': None}

飞机的价格是5亿元

雷达的频率是10万兆赫,价格是: 飞机的价格是5亿元

{}

{'capitain': 'Json'}

{}

从类删除,就没有了

classPlane():#添加一个共有成员属性

capitain = "John"

#添加一个私有成员属性

__Price = "飞机的价格是5亿元"

#共有绑定方法

deffly(self):print ("飞机飞行速度更快,机长是:",self.capitain)#共有普通方法,这个只能是使用类来调用

defraining():print ("飞机可以人工降雨,机长是",Plane.capitain)#私有绑定方法

def __radar_frequency(self):print("雷达的频率是10万兆赫,价格是:",self.__Price)#私有普通方法

def __plane_price():print ("我的飞机飞行很快,价格是:",Plane.__Price)#公有方法调用私有成员,用对象来调用

defplane_price_info(self):print (self.__Price)

self.__radar_frequency()#也可以使用类来调用

defplane_price_info2():print(Plane.__Price)

Plane.__plane_price()

obj=Plane()

obj.fly()print(obj.capitain)print(Plane.capitain)#删除capitain

delPlane.capitainprint (Plane.capitain)

执行

1624149-20200215153355159-298716366.png

删除方法,直接得了删除

classPlane():#添加一个共有成员属性

capitain = "John"

#添加一个私有成员属性

__Price = "飞机的价格是5亿元"

#共有绑定方法

deffly(self):print ("飞机飞行速度更快,机长是:",self.capitain)#共有普通方法,这个只能是使用类来调用

defraining():print ("飞机可以人工降雨,机长是",Plane.capitain)#私有绑定方法

def __radar_frequency(self):print("雷达的频率是10万兆赫,价格是:",self.__Price)#私有普通方法

def __plane_price():print ("我的飞机飞行很快,价格是:",Plane.__Price)#公有方法调用私有成员,用对象来调用

defplane_price_info(self):print (self.__Price)

self.__radar_frequency()#也可以使用类来调用

defplane_price_info2():print(Plane.__Price)

Plane.__plane_price()

obj=Plane()

obj.fly()print(obj.capitain)print (Plane.__dict__)#删除方法

delPlane.rainingprint (Plane.__dict__)

执行

[root@node10 python]#python3 test.py

飞机飞行速度更快,机长是: John

John

{'__module__': '__main__', 'capitain': 'John', '_Plane__Price': '飞机的价格是5亿元', 'fly': , 'raining': , '_Plane__radar_frequency': , '_Plane__plane_price': , 'plane_price_info': , 'plane_price_info2': , '__dict__': , '__weakref__': , '__doc__': None}

{'__module__': '__main__', 'capitain': 'John', '_Plane__Price': '飞机的价格是5亿元', 'fly': , '_Plane__radar_frequency': , '_Plane__plane_price': , 'plane_price_info': , 'plane_price_info2': , '__dict__': , '__weakref__': , '__doc__': None}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值