类的相关操作

  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}

026.Python面向对象类的相关操作以及对象和类的删除操作的更多相关文章

  1. DOM对象本身也是一个js对象,所以严格来说,并不是操作这个对象慢,而是说操作了这个对象后,会触发一些浏览器行为(转)

    一直都听说DOM很慢,要尽量少的去操作DOM,于是就想进一步去探究下为什么大家都会这样说,在网上学习了一些资料,这边整理出来. 首先,DOM对象本身也是一个js对象,所以严格来说,并不是操作这个对象慢 ...

  2. Python面向对象 -- slots, @property、多重继承MixIn、定制类(str, iter, getitem, getattr, call, callable函数,可调用对象)、元类(type, metaclass)

    面向对象设计中最基础的3个概念:数据封装.继承和多态 动态给class增加功能 正常情况下,当定义了一个class,然后创建了一个class的实例后,可以在程序运行的过程中给该实例绑定任何属性和方法, ...

  3. python 面向对象静态方法、类方法、属性方法、类的特殊成员方法

    静态方法:只是名义上归类管理,实际上在静态方法里访问不了类或实例中的任何属性. 在类中方法定义前添加@staticmethod,该方法就与类中的其他(属性,方法)没有关系,不能通过实例化类调用方法使用 ...

  4. python面向对象编程(2)—— 实例属性,类属性,类方法,静态方法

    1  实例属性和类属性 类和实例都是名字空间,类是类属性的名字空间,实例则是实例属性的名字空间. 类属性可通过类或实例来访问.只有通过类名访问时,才能修改类属性的值. 例外的一种情况是,当类属性是一个 ...

  5. Python学习第十五课——类的基本思想(实例化对象,类对象)

    类的基本思想 类:把一类事物的相同的特征和动作整合到一起就是类类是一个抽象的概念 对象:就是基于类而创建的一个具体的事物(具体存在的)也是特征和动作整合到一块 对象写法 # 对象写法 def scho ...

  6. Python面向对象 -- 继承和多态、获取对象信息、实例属性和类属性

    继承和多态 继承的好处: 1,子类可以使用父类的全部功能 2,多态:当子类和父类都存在相同的方法时,子类的方法会覆盖父类的方法,即调用时会调用子类的方法.这就是继承的另一个好处:多态. 多态: 调用方 ...

  7. Python面向对象总结及类与正则表达式

    Python3 面向对象 一丶面向对象技术简介 类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法.对象是类的实例. 方法:类中定义的函数. 类变 ...

  8. python基础-9.1 面向对象进阶 super 类对象成员 类属性 私有属性 查找源码类对象步骤 类特殊成员 isinstance issubclass 异常处理

    上一篇文章介绍了面向对象基本知识: 面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 是一个模板,模板中包装了多个“函数”供使用(可以讲多函数中公用的变量封装到对象中) 对象 ...

  9. python开发学习-day07(面向对象之多态、类的方法、反射、新式类and旧式类、socket编程)

    s12-20160227-day07 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...

随机推荐

  1. 【笔记】《Redis设计与实现》chapter13 客户端

    服务器为每个客户端建立相应的redis.h/redisClient结构,这个结构保存了客户端当前的状态信息 客户端的套接字描述符 客户端的名字 客户端的标志值 只想客户端正在使用的数据库的指针,以及该 ...

  2. Linux终端更改字体

    1 概述 这里的终端是指通过F1-F6/F2-F7进入的纯命令终端. 修改字体可以通过setfont命令. 2 查看字体 可以通过查找目录consolefonts来确定本地机器上的字体位于哪里: fi ...

  3. Tony老师带你来看Java设计模式:代理模式

    目录 定义 作用 意图 主要解决问题 优缺点 与装饰者模式的区别 结构 从Tony老师来看实现方式 静态代理 动态代理 JDK动态代理的实现 cglib动态代理的实现 定义 为其他对象提供一种代理来控 ...

  4. Borrowers UVA - 230

      I mean your borrowers of books - those mutilators of collections, spoilers of the symmetry of shel ...

  5. Django 模型(Model)

    1. 模型简介 ORM 简介 使用 Mysql 数据库的环境配置 2. 定义模型 1)定义属性 2)字段类型 3)字段选项 4)关系 5)元选项 6)范例 3. 模型成员&管理器 1)类属性 ...

  6. 一次错误使用 go-cache 导致出现的线上问题

    话说一个美滋滋的上午, 突然就出现大量报警, 接口大量请求都响应超时了. 排查过程 查看服务器的监控系统, CPU, 内存, 负载等指标正常 排查日志, 日志能够响应的结果也正常. request.l ...

  7. 【C#】一个Loading窗体载入与销毁的方法

    写在前面 Minecraft Command Editor 2跳票了近两年的时间(对不起!!).2021年2月,我重启了MCE项目,并正式命名为Minecraft Command Editor 202 ...

  8. input 的各种属性的验证 checkValidity兼容性

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  9. vue.js中使用set方法 this.$set

    vue教程中有这样一个注意事项: 第一种具体情况如下: 运行结果: 当利用索引改变数组某一项时,页面不会刷新.解决方法如下: 运行结果: 三种方式都可以解决,使用Vue.set.vm.$set()或者 ...

  10. UVA10881蚂蚁

    题意:      在一个木棍上有只小蚂蚁,他们的移动速度都是1,移动的时候如果和别的蚂蚁碰面,那么碰面的这两只小蚂蚁会马上掉头继续走,给你每只蚂蚁的初始距离木棒左端点的距离和方向,以及木棍长度,问你t ...