2016-05-26 69 views
-3

我不知道如何創建和使用其他類中的其他類的對象。例如,我想從另一個類創建發佈者(Item)如何創建和使用Java中其他類的對象

public class Item { 
    private String title; 
    private String publisher; 
    private int price,year,quantity; 

    Item(String title,String publisher,int price,int year,int quantity){ 
     //Constructor goes here 
    } 
} 

//this is the publisher class: 

public class Publisher { 

    private String name,address,country,city; 

    Publisher(String name,String address,String country,String city){ 
     this.setName(name); 
     this.setAddress(address); 
     this.setCountry(country); 
     this.setCity(city); 
    } 
} 

回答

1

你試過了什麼?這很簡單:

public class Item { 
    .... 
    void Test() { 
     Publsiher p = new Publisher("a","b","c","d"); 
     //do some things 
    } 
} 
+0

thaaaaaaaaank你 –

0
public class Item{ 
    public Item(){ 
     Publisher p = new Publisher('Name','Address','Country','City'); 
    } 
} 

裏面的構造函數項()創建一個新的發行對象「P」與您選擇(「姓名」,「地址‘國家’,‘城市’)的輸入值。

相關問題