语法
类class
public class Puppy {
private int age;
private String name;
public List[Puppy] puppys;
// 构造函数,初始化实例
public Puppy(String name) {
this.name = name;
System.out.println("来了新小狗,名字: " + name);
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
}
class Puppy:
puppys = []
def __init__(self, name):
self.name = name
self.age = 0
print(f"来了新小狗,名字: {name}")
def setAge(self, age):
self.age = age
def get_age(self):
return self.age
- python的类函数会显式传入
self
- python使用
puppys
(静态)类变量时,需要Puppy.puppys
;java限制,只能在static静态方法才可以使用静态类变量