赞
踩
大家好,给大家分享一下python的一些简单的程序代码,很多人还不知道这一点。下面详细解释一下。现在让我们来看看!
目录
- class person:
- name=None
- def say(self):
- print(self.name)
- def say2(self,msg):
- print(f"大家好,我是:{self.name},我的年龄是{msg}岁")
- per1=person()
- per1.name='xiao'
- per1.say()
-
- per2=person()
- per2.name='wu'
- per2.say2(19)
'运行
- class Student:
- name=None
- age=None
- tel=None
- # 以上的定义可以省略
- def __init__(self,name,age):
- self.name=name
- self.age=age
- # self.tel=tel
- stu=Student('xiao',19)
- print(stu.age)
-
'运行
- class Student:
- age=None
- name=None
- def __init__(self,age,name):
- self.age=age
- self.name=name
- def __str__(self):
- return f"Student类对象,name:{self.name},age:{self.age}"
- stu=Student(19,'xiao')
- print(stu)
'运行
注释:
# __lt__ 比较小于
# __gt__ 比较大于
# __le__ <=
# __ge__ >=
# __eq__ ==
- class Student:
- age=None
- name=None
- def __init__(self,age,name):
- self.age=age
- self.name=name
- def __str__(self):
- return f"Student类对象,name:{self.name},age:{self.age}"
- def __lt__(self,other):
- return self.age<other.age
- stu1=Student(24,'xiao')
- stu2=Student(22,'wu')
- print(stu1>stu2)
'运行
- from typing import Union
- my_dict:[str,Union[str,int]]={'age':19,'name':'xiao'}
- def func(x:list[int],y:int)->[list]:
- x.append(y)
- return x
- a=[1]
- print(func(a,2))
-
'运行
- class phone1:
- price=2000
- class phone2:
- price=5000
- #继承时以从左到右为优先级
- class phone(phone1,phone2):
- pass
- p=phone()
- print(p.price)
'运行
- #第一种:
- class phone1:
- price=2000
- def find(self):
- print(self.price)
- class phone2:
- price=5000
- def find(self):
- print(self.price)
- class phone(phone1):
- def find(self):
- super().find()
- def find2(self):
- phone1.find(self)
- p=phone()
- p.find()
- p.find2()
-
-
- #第二种:
-
- class phone1:
- price=2000
- def find(self):
- print(self.price)
- class phone2:
- price=5000
- def find(self):
- print(self.price)
- class phone(phone2,phone1):
- def find(self):
- super().find()
- def find2(self):
- phone1.find(self)
- p=phone()
- p.find()
- p.find2()
'运行
对比上方的两次输出结果,聪明的你看出输出不同的原因了吗?
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。