我正在尝试使用构造函数创建具有完整数据的每个Man和Woman类的两个对象,该构造函数具有所有可能的参数。
我收到一条错误消息:
“无效的方法声明;需要返回类型”。
我的代码:
public class Solution {
public static void main(String[] args) {
Man man1 = new Man();
System.out.println(man.name + "" + man.age + "" + man.address);
Man man2 = new Man();
System.out.println(man.name + "" + man.age + "" + man.address);
Woman woman1 = new Woman();
System.out.println(woman.name + "" + woman.age + "" + woman.address);
Woman woman2 = new Woman();
System.out.println(woman.name + "" + woman.age + "" + woman.address);
//write your code here
}
private String name = "Mark";
private int age = 23;
private String address = 16527;
public Man(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
public Woman(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;//write your code here
}
}
有人可以帮帮我吗 :(
最佳答案
您的类名为Solution
,这意味着您的构造函数只能命名为Solution
。创建分别名为Man
和Woman
的类,然后在其中添加构造函数。
另外,由于要使用默认构造函数创建对象,因此请确保也将它们添加到上述类中。