创建一个对象并调用它

时间:2015-03-26 07:10:16

标签: java oop

这是我当前存储房间的代码(它编译得很好)但是在UML中有一个名为addEquipment的变量,还有另一个名为Equipment的类要定义。我无法绕过我应该做的事情。我应该创建并调用一个名为Equipment的对象吗? addEquipment的内容是什么?

public class Room {
    //begin variable listing
    private String name;
    private int id;
    private int capacity;
    private String equipmentList;

    //begins get methods for variables  
    public String getName(){ 
        return name;
    }
    public int getID(){ 
        return id;
    }
    public int getCapacity(){
        return capacity;
    }
    public String getEquipmentList(){
        return equipmentList;
    }

    //  Set the variables
    public void setName(String aName){
        name=aName;
    }

    public void setID(int anID){
        id=anID;
    }

    public void setCapacity(int aCapacity){
        capacity=aCapacity; 
    }

    public void setEquipmentList(String anEquipmentList){
        equipmentList=anEquipmentList; 
    }

    public String addEquipment(String newEquipment, String currentEquipment){

    }

    //Create room object
    public Room(int capacity, String equipmentList) {
        setCapacity(capacity);
        setEquipmentList(equipmentList);
    }

    //Convert variables to string version of room
    public String toString(){
        return "Room "+name+", capacity: "+capacity+", equipment: "+getEquipmentList();
    }
}

2 个答案:

答案 0 :(得分:0)

您可以创建新课程Equipment并将属性equipmentList修改为List

 public class Equipment {
        private String name;

        public Equipment(String name) {
           this.name = name;
        }
}

public class Room {
    //begin variable listing
    private String name;
    private int id;
    private int capacity;
    private List<Equipment> equipmentList = new ArrayList<Equipment>();

    //begins get methods for variables  
    public String getName(){ 
        return name;
    }
    public int getID(){ 
        return id;
    }
    public int getCapacity(){
        return capacity;
    }
    public List<Equipment> getEquipmentList(){
        return equipmentList;
    }

    //  Set the variables
    public void setName(String aName){
        name=aName;
        }

    public void setID(int anID){
        id=anID;
    }

    public void setCapacity(int aCapacity){
            capacity=aCapacity; 
    }

    public void setEquipmentList(List<Equipment> anEquipmentList){
        equipmentList=anEquipmentList; 
    }

    public String addEquipment(String newEquipment, String currentEquipment){
        Equipment oneEquipment = new Equipment(newEquipment);
        equipmentList.add(oneEquipment);
    }

    //Create room object
    public Room() {
        setCapacity(capacity);
        setEquipmentList(equipmentList);
    }

    //Convert variables to string version of room
    public String toString(){
        String capacity=String.valueOf(getCapacity());
        String room = "Room "+name+", capacity: "+capacity+", equipment:     "+getEquipmentList();
        return room;
    }
}

在方法addEquipment中,您可以创建新设备并将其添加到equipmentList,就像上面的代码一样。

答案 1 :(得分:0)

装备类可以是任何东西。让我们假设&#34;设备&#34; -class有一个名为&#34; name&#34;作为它的属性

public class Equipment {
  String name;

  public Equipment( String name) {
    this.name = name;
  }

  public String getName() {
    return this.name
  }   
}

当您按照要求的&#34; addEquipment&#34;扩展您的Room类时方法,你可以这样做。

public class Room {

  ... // Your code

  private int equipmentIndex = 0;
  private Equipment[] equipment = new Equipment[10]; // hold 10 Equipment objects

  public void addEquipment( Equipment eq ) {
    if ( equipmentIndex < 10 ) {
      equipment[ equipmentIndex ] = eq;
      equipmentIndex++;
      System.out.println("Added new equipment: " + eq.getName());
    } else {
      System.out.println("The equipment " + eq.getName() + " was not added (array is full)");
    }
  }
}

现在打电话

 room.addEquipment( new Equipment("Chair") );

在您之前初始化的Room-class对象上,您将获得

 "Added new equipment: Chair"

希望这有点帮助。

PS:代码是untestet(可能在某处隐藏了语法错误)