该Java代码是否应该返回错误?在线编译器运行良好

时间:2018-09-06 17:12:54

标签: java getter

如果我只能键入yourDog._name,为什么我需要getter.dod.getName()?This Java Compiler gives no errors这是因为我正在使用在线编译器吗?

public class Dog {

private String _name;
private int _age;

public Dog (String name, int age){
    _name = name;
    _age = age;
}

public void setAge (int age){
    _age = age;
}

public int getAge (){
    return _age;
}

public String getName(){
    return _name;
}

public static void main(String args[]) {

    Dog yourDog = new Dog("Tommy", 50);

    System.out.println(yourDog.getName());
    System.out.println(yourDog.getAge());
    System.out.println(yourDog._name);

}

0 个答案:

没有答案