如何使用Student类对象访问Department类的方法

时间:2019-06-07 18:26:54

标签: java class object getter-setter

使用以下私有成员变量创建类Department

int did
String dname

在Department类中包括适当的getter和setter方法。

使用以下私有成员变量创建班级学生

int sid
String sname
Department department

在Student类中包括适当的getter和setter方法。

创建一个具有main方法的TestMain类。

除了main方法之外,创建一个方法

       public static Student createStudent() -  All input as shown in the sample input should be got in this method.  Set the values to the Student object and return that object

注意:在main方法中,调用createStudent方法并打印该方法返回的对象的详细信息。

我已经尝试使用Department的名称创建一个新对象,但是我无法访问这些方法

import java.util.Scanner;

public class TestMain {

    public static Student student;
    public static Department department;

    public static void main(String args[]) {
        createStudent();
        System.out.println("Department id:" + department.getDid());
        System.out.println("Department name:" + department.getDname());
        System.out.println("Student id:" + student.getSid());
        System.out.println("Student name:" + student.getSname());
    }

    public static Student createStudent() {
        Scanner sc = new Scanner(System.in);
        student = new Student();
        department = new Department();
        System.out.println("Enter the Department id:");
        department.setDid(sc.nextInt());
        System.out.println("Enter the Department name:");
        department.setDname(sc.next());
        System.out.println("Enter the Student id:");
        student.setSid(sc.nextInt());
        System.out.println("Enter the Student name:");
        student.setSname(sc.next());
        return student;
    }
}

public class Student {

    private int sid;
    private String sname;
    private Department department;

    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }
}

public class Department {

    private int did;
    private String dname;

    public int getDid() {
        return did;
    }

    public void setDid(int did) {
        this.did = did;
    }

    public String getDname() {
        return dname;
    }

    public void setDname(String dname) {
        this.dname = dname;
    }
}

样本输入1:

Enter the Department id:
100
Enter the Department name:
Computerscience

Enter the Student id:
123
Enter the Student name:
Sudha

示例输出1:

Department id:100
Department name:Computerscience
Student id:123
Student name:sudha

4 个答案:

答案 0 :(得分:0)

您不见了:

student.setDepartment(department);

之后,您可以执行以下操作:

student.getDepartment().getSname();

答案 1 :(得分:0)

import java.util.*;
public class TestMain
{
    public static Department d1;
    public static Student s1;
    public static Student createStudent()
    {
        Scanner sc=new Scanner(System.in);
        Student s1=new Student();
        Department d1=new Department();
        System.out.println("Enter the Department id:");
        int did=sc.nextInt();
        sc.nextLine();
        System.out.println("Enter the Department name:");
        String dname=sc.nextLine();
        System.out.println("Enter the Student id:");
        int sid=sc.nextInt();
        sc.nextLine();
        System.out.println("Enter the Student name:");
        String sname=sc.nextLine();
        d1.setDid(did);
        d1.setDname(dname);
        s1.setSid(sid);
        s1.setSname(sname);
        s1.setDepartment(d1);
        return s1;
    }
    public static void main(String[] args)
    {
        s1=TestMain.createStudent();
        System.out.println("Department id:"+d1.getDid());
        System.out.println("Department name:"+d1.getDname());
        System.out.println("Student id:"+s1.getSid());
        System.out.println("Student name:"+s1.getSname());
    }
}

答案 2 :(得分:0)

public static void main(String args[]) throws NumberFormatException, IOException
{
 s1 =  createStudent();
 d1 = s1.getDepartment(); // THIS WAS MISSING IN ANSWER 2
 System.out.println("Department id:"+d1.getDid());
 System.out.println("Department name:"+d1.getDname());
 System.out.println("Student id:"+s1.getSid());
 System.out.println("Student name:"+s1.getSname());

}

答案 3 :(得分:0)

在这种情况下,必须完成两件事。

  1. 为所有id和名称“设置”值时,在设置did和dname之后,为了通过学生对象访问该值,必须将did&dname组合“设置”为部门对象在学生班里。

    department.setDid(did);
    department.setDname(dname);
    student.setSid(sid);
    student.setSname(sname);
    student.setDepartment(department);
    
  2. 此外,现在,一旦为它设置了正确的值,在Main中的Sysout语句之前,您必须为Department类创建一个对象。数据是通过在学生班级中调用getDepartment()来获得的(请记住,此函数返回Department对象)。

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Student student = createStudent();
        Department department = student.getDepartment();
        System.out.printf("Department id:%d",department.getDid()).println();
        System.out.printf("Department name:%s",department.getDname()).println();
        System.out.printf("Student id:%d",student.getSid()).println();
        System.out.printf("Student name:%s",student.getSname()).println();
    }