2012-03-15 135 views
0

我对Java很新,我想知道是否可以将对象分配给另一个对象。例如,假设我有一个名为“课堂”的对象。在这个教室里,我有椅子,桌子,桌子等。 椅子,桌子和桌子都是物体本身,它们有自己独立的属性,比如椅子腿的数量,桌腿的宽度等等。所以基本上,我有4班。现在我想分配椅子,桌子,棋盘作为ClassRoom的属性。我不知道如何去做这件事。将对象分配给对象Java

任何帮助,将不胜感激, 在此先感谢 酯

回答

0

我不是一个大java家伙,但我怀疑它类似于在C++中这样做...... 只要定义了类,就应该能够将它们声明为另一个对象的属性,就像你会用诸如String之类的东西。
只要确保每个对象都已定义,并且支持方法在该类中定义。
这可能不是完全语法正确,但它应该是这个样子:

class classRoom { 
    Chair chair; 
    Table table; 
    Board board; 
    String name; 

    add_chair(); 
    add_table(); 
    remove_table(); 

} 

class Chair { 
    Leg leg1; 
    Leg leg2; 
} 

class Leg { 
    int height; 
    set_height(); 
    get_height(); 
} 

class Board { 
    int width; 
    int height; 
} 

现在,如果你要访问的教室椅子你只是做这样的事情:

classRoom room = new classRoom(); 
height = room.chair.leg.get_height(); 

虽然记这是不好的做法,你应该设置函数来获取这些值,而不是直接访问它们。

+0

这不会编译,因为方法没有body和返回类型 - 而且教室通常有多个桌子或椅子 – 2012-03-15 14:37:27

+0

是的我意识到这一点,我引用“这可能不完全是语法正确,但它应该看起来像这样:“ 我认为伪代码在这种情况下更有用 – Prediluted 2012-03-15 14:43:48

+0

谢谢!对此,我真的非常感激 – Ester 2012-03-15 19:26:47

1

你需要让课堂“复合”类,一说认为,指的是你提到的其他类领域。例如,“课堂”可容纳ArrayList<Furnature>(或称为 Furnature的ArrayList ),并且您的主席,表格和类似课程可以从抽象Furnature课程扩展而来。然后,您可以授予“课堂”方法,以允许您从ArrayList添加和移除Furnature项目。

+0

哦,并且当你看到他时尽我所能给莫底凯叔叔。 – 2012-03-15 14:28:37

0

我想一个简单的方法是列出

class ClassRoom { 
    private List<Chair> chairs = new ArrayList<Chair>(); 
    private List<Table> tables = new ArrayList<Chair>(); 
    ... 

    void addChair(Chair chair) { 
     chairs.add(chair); 
    } 

    List<Chair> getChairs() { 
    .... 
1

编辑去:添加对象

public final class Classroom { 
    public Classroom() { 
    this.tables = new ArrayList<Table>; 
    this.chairs = new ArrayList<Chair>; 
    this.boards = new ArrayList<Board>; 
    } 

    public void addTable(final Table table) { 
    this.tables.add(table); 
    } 

    public void addChair(final Chair chair) { 
    this.chairs.add(chair); 
    } 

    public void addBoard(final Board board) { 
    this.boards.add(board); 
    } 


    private final List<Table> tables; 
    private final List<Chair> chairs; 
    private final List<Board> boards; 
} 

外,从主例如

Table table = new Table(param1, param2); 
Table anotherTable = new Table(param1, param2); 
Chair chair = new Chair(param1, param2); 
Board board = new Board(param1, param2); 

现在让你的教室:

Classroom classroom = new Classroom(); 

// adding object 
classrooom.addTable(table); 
classroom.addChair(chair); 
classroom.addTable(anotherTable); 

// and so on... 
+0

真实世界的教室通常有多个桌子或椅子。 – 2012-03-15 14:35:04

+0

是的,我编辑模仿真实世界的教室:)。谢谢 – 2012-03-15 14:36:01

1

你有一个has-many教室和桌子之间的关系(例如)。基本设计如下:

public class Classroom { 
    List<Table> tables = new ArrayList<Table>(); 

    // you may want to add a table to the classroom 
    public void addTable(Table table) { 
    tables.add(table); 
    } 

    // you may want to remove one 
    public void removeTable(Table table) { 
    tables.remove(table); 
    } 

    // here we can replace (a broken) one 
    public void replaceTable(Table oldTable, Table newTable) { 
    tables.set(tables.indexOf(oldTable), newTable); 
    } 

    // and: the inventory 
    public List<Table> getTables() { 
    return tables; 
    } 
}