2016-11-20 53 views
-1

我有一個Java類用於公司位於國家周圍的不同分支,並且每個分支都將保留其客戶列表的跟蹤。我如何實現這一點,以便「倫敦」分支只拉取使用該分支的客戶列表,但無法查看其他分支的客戶列表?我想包含數組客戶端類的,但IM苦苦尋找類連接在一起具有數組的Java類

public class Branch implements Manager { 

//instance variables that will be available to children of Branch 
private String branchName; 



// constructor 
public Branch(String name) { 
    this.branchName = name; 
} 


//Returns the location of the branch as a String 
@Override 
public String getBranch() { 
    return this.branchName; 
} 

//Returns a String representation of customers who have used the Branch 
@Override 
public String getAllCustomers() { 
    return "Customer list will go here"; 
} 
+1

你意識到如果你把數組作爲類的私有成員,它將只能被特定對象直接訪問? –

+0

當然,但會創建分支類的實例,這意味着「倫敦」將能夠看到「格拉斯哥」的客戶列表? – user2904400

回答

0

怎麼樣的一種方式:

Map<String, List<Clients>> 

因此,您可以將分支映射到客戶端列表。

0

如果class London extends Branchclass Glasgow extends Branch那麼London將無法​​獲得Glagow的私人字段。 LondonGlasgow甚至無法訪問在Branch中刪除的私人成員

使用protected或程序包保護的訪問修飾符。