bg:记录一下,怕忘了;先写一点,后面再补充。
1、没有方法重载
2、字段都是公共字段
3、都是类似C#中顶级语句的写法
4、对类的定义直接:
python
class Student:
创建对象不需要new关键字,直接stu = Student()
5、方法
方法的定义用关键词def
python
class Student:
def SayHello(self):
print("Hello")
构造方法是:def init(self, 参数1, 参数2)
6、没有严格的"字段"和"属性"区分,类中定义的字段默认是公共变量
7、对类、字段、方法写了个样例方便理解一下
python
class Student:
Name = "aaaaaaa"
# Python中没有方法重载
def __init__(self):
print("运行了")
def __init__(self, name = None, age = None):
self.name = name
self.age = age
def SayHello(self):
# Name = name
print(f"{self.Name}")
def SayHello2(self, woc):
print(f"{woc}")
stu = Student("李华", 19)
stu1 = Student()
stu.Name = "aaaaaaabbbb"
stu.SayHello()
stu.SayHello2(woc="aaabbbb")
8、Python没有方法重载,方法覆盖就有。
没有方法重载可能是因为:传参的"*args"可以接受任意数量参数
python
class Example:
def show(self, *args): # 接受任意数量参数
if len(args) == 1:
print(args[0])
elif len(args) == 2:
print(args[0] + args[1])
obj = Example()
obj.show(1) # 输出: 1
obj.show(1, 2) # 输出: 3
9、数据容器
