【问题标题】:How to add an object to an object in java? [closed]如何将对象添加到java中的对象?
【发布时间】:2021-06-20 17:51:27
【问题描述】:

我是 Java 新手,我正在处理如何将对象附加到另一个对象以及如何将对象添加到其他类中的 ArrayList。我有 3 个班级学校,学科,员工。我必须将 2 个 Staffs 对象添加到 2 个 Subject 对象,然后将 4 个 Staff 对象添加到 School 的 ArrayList 中。这是我的代码

    import java.util.ArrayList;
    import java.util.Scanner;
    
    public class School {
        public String name;
        public Staff headOfSchool;
        private Staff schoolManager;
        private ArrayList<Staff> staffList;
        public ArrayList<Subject> subjectList;
    
    
    
        public School(String name) {
            this.name = name;
        }
    
    
        public School(Staff schoolManager){
            this.schoolManager = schoolManager;
        }
    
        public School() {
    
        }
    
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            ArrayList<Staff>  staffList = new ArrayList<>();
            ArrayList<Subject>  subjectList = new ArrayList<>();
            System.out.println("Enter the school name: ");
            String name  = scan.next();
            School school = new School(name);
        }
    
    
    }
    
    public class Staff {
        public int id;
        public String name;
        public String gender;
        public String title;
        public String email;
        public int phone;
        public String position;
        public String location;
    
        public Staff(int id, String name, String gender, String title,
                     String email, int phone, String position, String location) {
            this.id = id;
            this.name = name;
            this.gender = gender;
            this.title = title;
            this.email = email;
            this.phone = phone;
            this.position = position;
            this.location = location;
        }
        public void getStaffDetail() {
            Scanner scan = new Scanner(System.in);
            for(int i = 0; i < 4; i++){
                System.out.println("Please enter title: ");
                String title = scan.next();
                System.out.println("Please enter name: ");
                String name = scan.next();
                System.out.println("Please enter ID: ");
                int id = scan.nextInt();
                System.out.println("Please enter gender: ");
                String gender = scan.next();
                System.out.println("Please enter phone number: ");
                int phone = scan.nextInt();
                System.out.println("Please enter email: ");
                String email = scan.next();
                System.out.println("Please enter location: ");
                String location = scan.next();
                System.out.println("Please enter position: ");
                String position = scan.next();
                switch (i){
                    case 0:
                        Staff staff = new Staff(id, name, gender, title,
                                email, phone, position, location);
                    case 1:
                        Staff staff1 = new Staff(id, name, gender, title,
                                email, phone, position, location);
                    case 2:
                        Staff staff2 = new Staff(id, name, gender, title,
                                email, phone, position, location);
                    case 3:
                        Staff staff3 = new Staff(id, name, gender, title,
                                email, phone, position, location);
                }

public class Subject {
    public String name;
    public int code;
    public String session;
    public int year;
    public Staff coordinator;
    public String lecture;
    public String lab;
    public String campus;

    public Subject(String name, int code, String session, int year,
                   String lecture, String lab, String campus) {
        this.name = name;
        this.code = code;
        this.session = session;
        this.year = year;
        this.lecture = lecture;
        this.lab = lab;
        this.campus = campus;
    }

    public Subject(Staff coordinator) {
        this.coordinator = coordinator;
    }

    public String getName() {
        return name;
    }

    public int getCode() {
        return code;
    }

    public String getSession() {
        return session;
    }

    public int getYear() {
        return year;
    }


    public String getLecture() {
        return lecture;
    }

    public String getLab() {
        return lab;
    }
    public String getCampus(){
        return campus;
    }
}

如果有人可以帮助我,我将不胜感激。非常感谢

【问题讨论】:

    标签: java object oop arraylist attachment


    【解决方案1】:

    创建我们的StaffPerson 类。

    package work.basil.example;
    
    import java.util.Objects;
    
    public class StaffPerson
    {
        // Members
        private String name, title;
    
        // Constructors
        public StaffPerson ( String name , String title )
        {
            this.name = Objects.requireNonNull( name );
            this.title = Objects.requireNonNull( title );
        }
    
        // Accessors
        public String getName ( ) { return this.name; }
    
        public String getTitle ( ) { return this.title; }
    
    
        // `Object` overrides
    
        @Override
        public boolean equals ( Object o )
        {
            if ( this == o ) return true;
            if ( o == null || getClass() != o.getClass() ) return false;
            StaffPerson that = ( StaffPerson ) o;
            return name.equals( that.name ) && title.equals( that.title );
        }
    
        @Override
        public int hashCode ( )
        {
            return Objects.hash( name , title );
        }
    
        @Override
        public String toString ( )
        {
            return "StaffPerson{ " +
                    "name='" + name + '\'' +
                    " | title='" + title + '\'' +
                    " }";
        }
    }
    

    制作几个员工对象。

    StaffPerson alice = new StaffPerson( "Alice" , "Instructor" ) ;
    StaffPerson bob = new StaffPerson( "Bob" , "Secretary" ) ;
    

    使用List#add 方法将它们添加到您的List

    List< StaffPerson > staff = new ArrayList<>() ;
    staff.add( alice ) ;
    staff.add( bob ) ;
    

    或者,您可以创建unmodifiable list.

    List< StaffPerson > staff = List.of( alice , bob ) ;
    

    转储到控制台。

    System.out.println( staff.toString() ) ;
    for( StaffPerson staffPerson : staff )
    {
        System.out.println( staffPerson.toString() ) ;
    }
    

    您可以对问题中提到的其他作业执行相同的操作。

    请参阅code above run live at IdeOne.com

    [StaffPerson{ name='Alice' | title='Instructor' }, StaffPerson{ name='Bob' | title='Secretary' }]
    StaffPerson{ name='Alice' | title='Instructor' }
    StaffPerson{ name='Bob' | title='Secretary' }
    

    提示:将所有 Scanner 代码移动到您的 business object 类之外的其他位置StaffPerson 类应该知道如何代表现实世界中的员工,但不应该为知道如何通过控制台与用户交谈而感到负担。在控制台中处理用户以收集数据输入值应该由其他一些类来处理。请参阅Separation of Concerns 上的维基百科。

    【讨论】:

      猜你喜欢
      • 2014-09-14
      • 2023-01-19
      • 2014-07-16
      • 2017-02-24
      • 2013-11-06
      • 2023-04-06
      • 2013-10-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多