我有3节课。这些类是Class1,Parent和Child。我在弄清楚如何编写我的Child类所需的构造函数时遇到了一些麻烦。
public Class1
{
private String firstName;
private String lastName;
public Class1()
{
firstName="";
lastName="";
}
public Class1(String firstName, String lastName)
{
this.firstName=firstName;
this.lastName=lastName;
}
//Methods and stuff
}
public Parent
{
private Class1 class1;
private double number;
public Parent();
{
class1=new Class1();
number=0;
}
public Parent(Class1 c, double n)
{
Class1=c;
number=n;
}
//Methods and stuff
}
public Child extends Parent
{
private String string;
private Boolean boolean;
public Child(Class1 class1, double n, String s, Boolean b)
{
//Don't know how to get the Class1 part to work
//Don't know how to get the double to work
string=s;
boolean=b;
//Methods and stuff
}
我不知道如何编写代码,这样我就可以让我的构造函数接受这样的参数:
new Child(new Class1("String", "String"), 10, "String", true);
我希望这有助于弄清我的问题所在。
创建Child
构造函数为
public Child(Class1 objClass1, double number, string str, boolean bool){
super(objClass1,number);
this.str=str;
this.bool=bool;
}
创建Parent
构造函数为
public Parent(Class1 objClass1, double number){
this.objClass1=objClass1;
this.number=number;
}
您可以将子构造函数称为
Child objChild=new Child(new Class1(str1,str2),number,str,bool);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句