[英]java - can't create a constructor
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);
}
}
我试图创建一个构造函数Human(int age)来练习'this'关键字,并将年龄从25更改为其他值,但是由于我有一个Human类和一个Human构造函数,所以出现了错误。 当我尝试在主要方法中创建Human Type实例时,eclipse要求我删除构造函数
您已在作业中交换了订单,
Human(int age){
age = this.age;
}
应该是这样的(不要忘了初始化name
)
Human(int age){
this.age = age;
this.name = "Unknown";
}
您正在将默认值0
分配给传入的参数。 如果您提供了构造函数,则编译器将不再插入默认的构造函数,
Human() {
this.age = 0;
this.name = "Unknown";
}
并且您最好添加一个使用该名称的构造函数,
Human(int age, String name) {
this.age = age;
this.name = name;
}
那么您可以像这样称呼它(在main
)
Human human1 = new Human(25, "Joe");
// human1.name = "Joe";
// human1.age = 25;
当您创建一个非空的构造函数时,该空的构造函数将不再可用。 您确实可以拥有多个构造函数,但是如果您希望无参数构造函数与其他构造函数一起使用,则必须重新创建它。
//Please, make it public for constructors
public Human(int age){
this.age = age; //this.age first, to receive the parameter age
}
public Human() {} //Empty constructor. It doesn't has to be a content.
因此,您致电:
Human humanOne = new Human(); //Using no-argument constructor
Human humanTwo = new Human(25); //Using constructor with int to set age
您必须创建一个无参数的构造函数,因为调用Human h = new Human();
,您正在调用无参数构造函数。
尝试这样做:
Human h = new Human(age);
这是工作代码:
创建一个类GettersAndReturnValue
并将其添加。 您需要一个空的构造函数。
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){
this.age = age;
}
public Human() {
// TODO Auto-generated constructor stub
}
}
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);
}
}
输出:
My name is: Joe
Years till retirements 40
25
在类中创建构造函数时,它将不再使用默认构造函数。 在您的代码中,您已经创建了一个public Human(int)
构造函数,因此没有默认的构造函数。 因此,您不能像这样创建人类对象:
Human a = new Human();
为此,您必须手动实现无参数的Human构造函数。
这是一个解决方案:
class Human{
String name;
int age;
//default constructor
public Human (){
}
//paramete constructor
public Human(int a){
this.age=a;
}
void speak(){
System.out.println("My name is: " + this.name);
}
int calculateYearsToRetirement(){
int yearsLeft = 65 - age;
return yearsLeft;
}
int getAge(){
return this.age;
}
String getName(){
return this.name;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.