当前位置:网站首页>定义一个类,super的使用,私有属性
定义一个类,super的使用,私有属性
2022-07-31 05:18:00 【m0_59138290】
定义一个类
要求:
a.需要有一个类变量
b.需要有>=2个的对象变量
c.定义一个方法:打印类变量和对象变量
d.使用print打印对象->输出为This is a object
e.实例化两个对象:且两个对象相加等于2
f.为对象添加一个临时变量temp_var
class Person:
country = "china"
def __init__(self, name, age, gender,temp_var):
self.name = name
self.age = age
self.gender = gender
self.temp_var = temp_var
def __add__(self, other):
if self.age + other.age:
return 2
def jump(self):
print("jump")
def run(self):
print("run")
def walk(self):
print("walk")
def obj_show(self):
print("This is a object")
print(Person.country)
person1 = Person("王某", 20, "男","a")
person1.walk()
person1.obj_show()
person2 = Person("李思", 21, "女","b")
print(person1.__add__(person2))
super的使用
定义一个类A, 里面又一个方法print_info
定义一个类B, 里边有一个方法print_info和一个方法say_something
定义一个类C, 里边有一个方法say_something
定义一个类D, 里边有一个方法print_info和一个方法say_something
定义一个类E,继承类A, 类B, 类C,类D
实例化类E的对象
执行print_info和say_something方法
利用super,让执行print_info时调用B中print_info
利用super, 让执行say_something时调用C中say_something
利用super, 让执行print_info和say_something时调用
D中print_info和say_something
class A:
def print_info(self):
print("Hello A")
class B:
def say_something(self):
print("say Something B")
def print_info(self):
print("Hello B")
class C(A):
def say_something(self):
print("say Something C")
pass
class D(B):
def say_something(self):
print("say Something D")
def print_info(self):
print("Hello D")
pass
class E(C, D, A, B,):
def print_info(self):
super(A, E).print_info(self)
def say_something(self):
super(C, E).say_something(self)
pass
e = E()
e.print_info()
e.say_something()
定义一个父类
要求:包含三个对象变量,且其中一个对象变量使用_命名 定义一个方法:命名使用
__命名 定义一个子类继承上边的父类:且定义一个和父类方法名相同的方法(__) 实例化子类的对象 访问带_的对象变量
访问父类中的__xxx方法 访问子类中的__xxx方法
class Person:
def __init__(self):
self._name = None
self.age = 0
self.__gender = "male"
def get_info(self):
print("get_info")
def _get_score(self):
print("get_score")
def __get_url(self):
print("get_url")
pass
per = Person()
print(per.age)
per.get_info()
print(per._name)
per._get_score()
class Student(Person):
def __get_url(self):
print("Student Get Url")
pass
student = Student()
print(student.age)
student.get_info()
print(student._name)
student._get_score()
student._Student__get_url()
student._Person__get_url()
print(student._Person__gender)
边栏推荐
猜你喜欢
Pytorch study notes 13 - Basic_RNN
Remote file xxx is mapped to the local path xxx and can't be found. You can continue debugging....
vs2022 xlua 集成第三方库编译报错Generator Visual Studio 15 2017 could not find any instance of Visual Studio.
Getting Started with MySQL: The Case Statement Works Well
box-shadow related properties
钉钉企业内部-H5微应用开发
Fluorescein-PEG-DSPE Phospholipid-Polyethylene Glycol-Fluorescein Fluorescent Phospholipid PEG Derivatives
Cholesterol-PEG-Azide CLS-PEG-N3 Cholesterol-PEG-Azide MW:3400
Four common ways of POST to submit data
WIN10,配置adb环境
随机推荐
学习JDBC之获取数据库连接的方式
Wlan实验(ENSP)
DSPE-PEG-COOH CAS: 1403744-37-5 Phospholipid-polyethylene glycol-carboxy lipid PEG conjugate
UE5 最新动态虚幻引擎全新版本引爆互联网
哈希表基础
vs2022 xlua 集成第三方库编译报错Generator Visual Studio 15 2017 could not find any instance of Visual Studio.
Remote file xxx is mapped to the local path xxx and can‘t be found. You can continue debugging....
Qt TreeView 问题记录
Pytorch learning notes 09 - multiple classification problem
C语言数组的深度分析
Cholesterol-PEG-Amine CLS-PEG-NH2 Cholesterol-Polyethylene Glycol-Amino Research Use
UR3机器人雅克比矩阵
关于Iframe
力扣.有效的字母异位词
a:自我介绍
ES6-模块
记一次QT 2D 画图 实现3D动态效果
关于网络安全法的个人理解
box-shadow related properties
C语言结构体(必须掌握版)