阅读背景:

java - 无法创建构造函数

来源:互联网 
class Human{

    // declared instance variables 
    String name;
    int age;

    // instance method
    void speak(){
        System.out.println("My name is: " + name);
    }

    int calculateYearsToRetirement(){
        int yearsLeft = 65 - age;
        return yearsLeft;
    }

    int getAge(){
        return age;
    }

    String getName(){
        return name;
    }

    // so when I create an instance, i can't have constructor?
    // error here
    Human(int age){
        age = this.age;
    }
}



}
public class GettersAndReturnValue {

    public static void main(String[] args) {
        // error here because I created a constructor Human(int a)
        Human human1 = new Human();
        human1.name = "Joe";
        human1.age = 25;

        human1.speak();
        int years = human1.calculateYearsToRetirement();

        System.out.println("Years till retirements " + years);

        int age = human1.getAge();
        System.out.println(age);
    }

}
class Human{

    // declared instance variable



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: