gpt4 book ai didi

java - Java中如何在另一个类中调用一个类的参数化构造函数

转载 作者:行者123 更新时间:2023-12-02 12:09:40 24 4
gpt4 key购买 nike

class Department{

int dep_id;
string dep_name;

Department(string dep_id , string dep_name)
{
Scanner input=new Scanner(System.in);
System.out.println("Enter Department ID");
dep_id=input.nextInt();
system.out.println("Enter Department Name");
dep_name=input.nextString();
void Display(){
System.out.println("Deaprtment ID is : " + dep_id);
System.out.println("Deaprtment Name is : " + dep_name);

}

class Professor extends Person
{ //This person is different class

string profName;
Department dept;
Professor(string profName){
Scanner input=new Scanner(System.in);
System.out.println("Enter Professor Name");
profName=input.nextString();


}

现在我必须添加名为 dept 且类型为 Department 的数据字段用于初始化用户定义值的两个属性的双参数构造函数如何在教授类中调用系类

最佳答案

希望这能让大家清楚

    class Professor extends Person { // This person is different class

string profName;
Department dept;

Professor(string profName, int dept_id, String dept_name) {
this.profName = profName;
dept = new Department(dept_id, dept_name);
}
}

在主类中使用扫描仪来获取这些输入

关于java - Java中如何在另一个类中调用一个类的参数化构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46631454/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com