2016-11-20 52 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或程序包保护的访问修饰符。