python 与 java的不同

语法

类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静态方法才可以使用静态类变量
posted @ 2024-11-28 09:44  Nolca  阅读(5)  评论(0编辑  收藏  举报