Java创建对象的实例

时间:2014-04-13 19:14:34

标签: java database class instance

我正在尝试创建2个类。

第一个被称为酒窖,我没有任何问题:

class Cellar{   
    private String Name_Cellar;
    private String D_O;

    public String toString(){String values= Name_Cellar +" "+ D_O;return values;}

    public Cellar(String NameCellar, String DO) {
        this.Name_Cellar= NameCellar;
        this.D_O= DO;
    }
    public void setName(String NameCellar) {this.Name_Cellar= NameCellar;}
    public String getName() {return Name_Cellar;}

    public void setDO (String DO) {this.D_O = DO;}
    public String getDO() {return D_O;}
}

问题始于第二节课:

class Wines{

    private String Name_Wine;
    private Cellar Fabricant;
    private String Type;
    private float Grad_Alch;
    private int Date;
}

Fabricant必须是对象类型Cellar的实例。

我怎么能这样做?

先谢谢你们!

1 个答案:

答案 0 :(得分:3)

确实,您已在代码中正确完成了此操作。

private Cellar Fabricant;//Definition
//...
Fabricant = new Cellar(NameCellar, DO);//Initialization

例如:

private Cellar Fabricant;//Definition
//...
String NameCellar = "what you want", Do = "whatever";
Fabricant = new Cellar(NameCellar, DO);//Initialization