我正在学习Java中的OOP。完成编码并对其进行编译时,编译器会显示此Manager类对Photographer类具有私有访问权限。我已经工作了一整晚,但我仍然找不到问题。任何人都可以告诉我如何解决它?
public class Manager
{
private ArrayList<Assignment> toDoList;
private ArrayList<Photographer> employees;
public Manager()
{
this.toDoList = new ArrayList<Assignment>();
this.employees = new ArrayList<Photographer>();
}
public void hire(String photographer)
{
employees.add(new Photographer(photographer));
}
public void giveOutAssignments()
{
int maxId;
if(toDoList.size()!=0 && employees.size()!=0){
for(Photographer p: employees){
maxId = 0;
//get highest priority
for(int i = 1; i<toDoList.size();i++){
//just check the unfinished assigns
if(!toDoList.get(i).getStatus()){
if(toDoList.get(i).getPriority()>toDoList.get(maxId).getPriority())
maxId = i;
}
}
//take the highest priority
Assignment currentAssign = toDoList.get(maxId);
//HERE IS THE PROBLEM
p.takePicture(currentAssign.getDescription());
//set it as finished
toDoList.get(maxId).setStatus();
}
}
}
}
这是摄影师班: 公共课摄影师 { 私人地图照片; 私有字符串名称;
public Photographer(String name)
{
photos = new HashMap(); // An important line. Must go in the constructor.
readPhotos(); // A very important line. this must go in the Photographer
// constructor so that the photographer will be able to take Pictures.
this.name = name;
}
private String takePicture(String description)
{
return photos.get(description);
}
private void readPhotos()
{
Pattern commentPattern = Pattern.compile("^//.*");
Pattern photoPattern = Pattern.compile("([a-zA-Z0-9\\.]+) (.*)");
try
{
Scanner in = new Scanner(new File("photos.txt"));
while (in.hasNextLine())
{
String line = in.nextLine();
Matcher commentMatcher = commentPattern.matcher(line);
Matcher photoMatcher = photoPattern.matcher(line);
if (commentMatcher.find())
{
// This line of the file is a comment. Ignore it.
}
else if (photoMatcher.find())
{
String fileName = photoMatcher.group(1);
String description = photoMatcher.group(2);
photos.put(description, fileName);
}
}
}
catch (FileNotFoundException e)
{
System.out.println(e);
}
}
}
答案 0 :(得分:1)
takePicture
被声明为private
,除了Photographer
之外的任何其他上下文都无法访问...
private String getDescription() {
将其更改为public
...
public String getDescription() {
请查看Controlling Access to Members of a Class了解详情
PS -
我在takePicture
...
Photographer
也遇到了问题
private String takePicture(String description)
{
return photos.get(description);
}
不得不改为更像......
public String takePicture(String description) {
return (String)photos.get(description);
}