假设您有以下 PetDriver 类,其中包含一个 main 方法:
public class PetDriver{
public static void main(String[] args){
int weight = 40;
Pet doggie = new Pet("Rover", weight);
System.out.println("my pet's name is " + doggie.getName());
System.out.println("my pet's weight is " + doggie.getWeight());
}
}
执行 main 会产生以下输出:
我的宠物的名字是 Rover
我的宠物的体重是 40
我的代码如下,但它返回 null。
public class pet {
public String name;
public int weight = 40;
public Pet (String y, int x){
y = name;
x = weight;
}
public String getName(){
return name;
}
public int getWeight(){
return weight;
}
}
谢谢!