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静态方法才可以使用静态类变量

本文作者:Nolca

本文链接:https://www.cnblogs.com/nolca/p/18573609

版权声明:本作品采用 收益分享revenue sharing 许可协议进行许可。

posted @   Nolca  阅读(5)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 ⏩intro 山姆精
  2. 2 🎸吉他 马叉
  3. 3 ☁升调 山姆精
  4. 4 🐦Flutter Virtual Riot/Madi
  5. 5 🎶纯律 山姆精
  6. 6 👻yeah~Color Bass! VR
🎸吉他 - 马叉
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.