首页 > 解决方案 > 请让我知道如何在 java 的构造函数中将值作为 3 级传递?

问题描述

这是我的主要功能。

class first{
  public static void main(String ar[]){
    second sc = new second();
  }
}

class second{
  second(third th){
    this.th = th;
  }
}


class third{
  private int i;
  third(int i){
    this.i = i;
  }
}

请在不更改构造函数参数的情况下告诉我解决方案。我不知道如何传递构造函数以及如何将构造函数的参数作为类的对象给出。

标签: javaconstructor

解决方案


在构造函数的参数中调用构造函数second(或首先创建一个变量并传递它)

second sec = new second(new third(12));

推荐阅读