Java获取和设置用法

时间:2016-12-01 10:13:18

标签: java get set runtime-error

我是Java和编程的新手,我有一个问题。我的代码编译,但给我错误:无法找到或加载主类PatienTest。你能解释一下我的错误在哪里以及我做错了什么。这是我的代码:

class Patient {

    private String name;
    private int age;
    public String newName;

    public String getName() {
        return name;
    }

    public void setName() {
        name = newName;
    }

    public int getAge() {
        return age;
    }

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

class PatientTest {

    private Patient pat;

    public boolean isChild() {
        return (pat.getAge() < 15);
    }

    public void main(String[] args) {
        System.out.println(isChild);
    }
}


class PatientTest {

    private Patient pat;

    public boolean isChild() {  
        return (pat.getAge() < 15);
    }
    public  void main(String[] args) {
        System.out.println(isChild);
    }
}

1 个答案:

答案 0 :(得分:2)

为了编写正确的java代码,当您想要运行main方法时,您必须执行以下操作:

在Patient.java文件中:

public class Patient {
    private String name ;
    private int age ;
    public String newName;

    public String getName() {
        return name;
    }
    public void setName(){
        name = newName;
    }
    public int getAge(){
        return age;
    }
    public void setAge(int newAge){
        age = newAge;
    }
}

在PatientTest.java文件中:

public class PatientTest {

    public static boolean isChild(Patient patientToBeTested) {
        return (patientToBeTested.getAge() < 15);
    }

    public static void main(String[] args) {
        Patient patient = new Patient()
        patient.setAge(12) // add your age
        System.out.println(this.isChild(patient));
    }
}