这是我的代码
public class RegMember {
private int regno;
private Cmember cmember;
private Vipmember vipmember;
private Clubs clubs;
private static int nextNo =1000;
public RegMember(Cmember cmember,Vipmember vipmember,Clubs clubs) {
this.vipmember=vipmember;
this.cmember=cmember;
this.clubs=clubs;
regno=++nextNo;
}
public static int getNextNo(){
return nextNo;
}
public String toString(){
return String.format("%-6d%-25s%-40s",regno,vipmember,cmember,clubs);
}
}
这是主要类
import java.util.Scanner;
public class TestPerson {
/**
* Creates a new instance of <code>TestPerson</code>.
*/
public TestPerson() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
MemberType[] m = { new MemberType("Corporate Member"),new MemberType("VIP Member")
};
RegMember[] r = new RegMember[2];
Cmember cm;
Vipmember vip;
Scanner s = new Scanner(System.in);
for(int i=0;i<r.length;i++){
System.out.println("your reg no is :"+(RegMember.getNextNo()+1));
for(int a=0;a<m.length;a++){
System.out.println((a+1)+". "+m[a].toString());
}
System.out.println("Enter Your selection:");
int sel=s.nextInt();
if(sel==1){
s.nextLine();
System.out.println(" enter name:");
String Name=s.nextLine();
System.out.println(" enter Handphone:");
String Hpnum=s.next();
System.out.println(" enter Age:");
int age=s.nextInt();
System.out.println("enter Company Name:");
String CompanyName=s.nextLine();
cm = new Cmember(Name,Hpnum,age,CompanyName);
}
else{
s.nextLine();
System.out.println(" enter name:");
String Name=s.nextLine();
System.out.println(" enter Handphone:");
String Hpnum=s.next();
System.out.println(" enter Age:");
int age=s.nextInt();
System.out.println("enter Email:");
String email=s.next();
vip = new Vipmember(Name,Hpnum,age,email);
}
if(sel==1){
r[i]=new RegMember(cm);
}
else{
r[i]=new RegMember(vip);
}
s.nextLine();
}
displayInfor(r);
}
public static void displayInfor( RegMember[]r){
for(int i =0; i<r.length;i++)
System.out.println( r[i].toString());
}
}
这是我面临的问题..
error: constructor
RegMember类中的RegMember不能应用于给定类型;
r[i]=new RegMember(cm);
^
必需:Cmember,Vipmember,Clubs
发现:Cmember
原因:实际和正式的参数列表长度不同
error: constructor
RegMember类中的RegMember不能应用于给定类型;
r[i]=new RegMember(vip);
^
必需:Cmember,Vipmember,Clubs
发现:Vipmember
原因:实际和正式的参数列表长度不同
2个错误
答案 0 :(得分:1)
你有构造函数
public RegMember(Cmember cmember,Vipmember vipmember,Clubs clubs) {
this.vipmember=vipmember;
this.cmember=cmember;
this.clubs=clubs;
regno=++nextNo;
}
所以你不能打电话
r[i]=new RegMember(cm);
只有一个参数Cmember
。定义构造函数
public RegMember(Cmember cmember) {
}
或使用现有构造函数创建新实例。
答案 1 :(得分:1)
RegMember
没有构造函数将Cmember
作为参数。这就是新的新RegMember(cm)
无法编译的原因。
同样,它没有构造函数只需要一个Vipmember
。所以新的RegMember(vip)
也无法编译。