开发者社区> 问答> 正文

如何访问另一个类中定义的对象?

我有一个叫做Student的类,当然,这不是全部代码,我只是想了解这个概念。我想在创建了大学类obj的User类中使用另一个obj。大学班是其中包含另一班obj的班。到目前为止,我只做过关联,还没有继承,因此与关联有关的解决方案会有所帮助。

public class College
{
  private Student[] student;
  private Teacher[] teacher;
  int count;
  public College()
  {
    student = new Student[9];
    teacher = new Teacher[9];
    count = 0;
  }

  public Student[] getStudent()
  {
    return student;
  }

  public void addStudent(Student inStudent)
  {
      student[count] = new Student(inStudent);
      count++;
  }

  public void setStudent(Student[] student)
  {
    this.student = student;
  }

  public Teacher[] getTeacher()
  {
    return teacher;
  }

  public void setTeacher(Teacher[] teacher)
  {
    this.teacher = teacher;
  }

  public boolean equals(Object inObj)
  {
    boolean isEqual = true;
    College inCollege = (College)inObj;

    if(student.length == inCollege.getStudent().length)
    { 
      for(int i = 0; i < student.length; i++)
      {
        if(!student[i].equals(inCollege.student[i]))
        {
            isEqual = false;
        }
      }
    }
    if((teacher.length == inCollege.getTeacher().length) )
    {
      isEqual = false;
      for(int i = 0; i < inCollege.getTeacher().length; i++ )
      {
        if(teacher[i].equals(inCollege.teacher[i]))
        { System.out.println("im in");
          isEqual = true;
        }
      }
    }
    return isEqual;
  }
}

问题来源:Stack Overflow

展开
收起
montos 2020-03-26 10:21:40 511 0
1 条回答
写回答
取消 提交回答
  • 您需要具有public访问器(获取器),private才能在其他类中访问成员。

    如下进行:

    import java.util.Scanner;
    
    class Engine {
        String name;
        int year;
        String manufacturer;
    
        public Engine() {
        }
    
        public Engine(String name, int year, String manufacturer) {
            this.name = name;
            this.year = year;
            this.manufacturer = manufacturer;
        }
    
        // getters and setters of instance variables
    
        @Override
        public String toString() {
            return "Engine [name=" + name + ", year=" + year + ", manufacturer=" + manufacturer + "]";
        }
    }
    
    class Submarine {
        private String id;
        private Engine engine;
    
        public Submarine(String id, Engine engine) {
            this.id = id;
            this.engine = engine;
        }
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public void setEngine(Engine engine) {
            this.engine = engine;
        }
    
        public Engine getEngine() {
            return engine;
        }
    
        @Override
        public String toString() {
            return "Submarine [id=" + id + ", engine=" + engine + "]";
        }
    }
    
    class ShipStorage {
        private Submarine submarine;
        private Submarine[] submarines;
    
        public void setSubmarine(Submarine submarine) {
            this.submarine = submarine;
        }
    
        public Submarine getSubmarine() {
            return submarine;
        }
    
        public void setSubmarines(Submarine[] submarines) {
            this.submarines = submarines;
        }
    
        public Submarine[] getSubmarines() {
            return submarines;
        }
    }
    
    public class Main {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            ShipStorage store = new ShipStorage();
    
            // Input submarines
            Submarine[] submarines = new Submarine[3];
            String id, name, manufacturer;
            int year;
            for (int i = 0; i < submarines.length; i++) {
                System.out.print("Enter the ID of the sumarine: ");
                id = in.nextLine();
                System.out.print("Enter the name of its engine: ");
                name = in.nextLine();
                System.out.print("Enter the manufacturing year of its engine: ");
                year = Integer.parseInt(in.nextLine());
                System.out.print("Enter the manufacturer's name of its engine: ");
                manufacturer = in.nextLine();
    
                submarines[i] = new Submarine(id, new Engine(name, year, manufacturer));
            }
    
            // Store submarines to ShipStorage
            store.setSubmarines(submarines);
    
            // Display submarines
            System.out.println("Displaying the data: ");
            for (Submarine submarine : store.getSubmarines()) {
                System.out.println(submarine);
            }
        }
    }
    

    运行示例:

    Enter the ID of the sumarine: 1
    Enter the name of its engine: A
    Enter the manufacturing year of its engine: 2010
    Enter the manufacturer's name of its engine: X
    Enter the ID of the sumarine: 2
    Enter the name of its engine: B
    Enter the manufacturing year of its engine: 2011
    Enter the manufacturer's name of its engine: Y
    Enter the ID of the sumarine: 3
    Enter the name of its engine: C
    Enter the manufacturing year of its engine: 2018
    Enter the manufacturer's name of its engine: Z
    Displaying the data: 
    Submarine [id=1, engine=Engine [name=A, year=2010, manufacturer=X]]
    Submarine [id=2, engine=Engine [name=B, year=2011, manufacturer=Y]]
    Submarine [id=3, engine=Engine [name=C, year=2018, manufacturer=Z]]
    

    回答来源:Stack Overflow

    2020-03-26 10:22:24
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
建立联系方法之一 立即下载
对象的生命期管理 立即下载
继承与功能组合 立即下载