将对象作为构造函数的参数传递

本治

我有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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将对象作为参数传递给构造函数?

在构造函数中将对象作为参数传递

将对象作为函数参数传递

C++,在另一个对象构造函数中将对象作为参数传递

在保存上下文的同时,将对象作为构造函数的参数传递

将对象传递给函数时,如何防止模板化构造函数将类作为参数

当将对象作为参数传递时,为什么要调用析构函数但不调用构造函数?

如何将对象作为参数传递给函数?

如何将对象作为参数传递给函数

如何将对象属性作为函数参数传递?

如何将对象/类作为函数的参数传递

将对象的任何方法作为函数中的参数传递

在Javascript中将对象作为参数传递给函数

我应该将对象作为函数的参数传递吗?

javaScript-将对象作为函数参数传递

将对象HTMLInputElement作为参数传递给函数

如何将对象的属性作为参数传递给函数

将对象作为参数传递给函数:MATLAB

将对象作为参数传递给C ++中的函数

如何将对象类型作为参数传递给构造函数并向其强制转换变量(JAVA)?

在Javascript构造函数中将键/值对象作为参数传递

参数作为对象传递时的 Typescript 构造函数简写

如何将多个参数作为输入发送到 WorkManager?或者如何创建构造函数?或者如何将对象作为输入传递?

将对象数组传递给构造函数

将对象列表作为单个参数传递

在AsyncTask中将对象作为参数传递

Arduino C ++将对象作为参数传递

将对象作为参数传递给链接

C++:将对象作为参数传递