你可以从另一个类调用私有构造函数吗?

时间:2013-11-26 02:09:54

标签: java constructor tree

所以我正在尝试编写一个方法来编写放入树形图的学生列表,但我的树形图在我的StudentViewcontroller中。我可以从我的写作学生方法中调用它。

  Here is my WriteStudent method so far:
  Public void WriteStudent(PrintWriter pw) {
   try{
      for (Map.Entry<String, Student> s : studentMap.entrySet()){
      }
     }catch(FileNotFoundException e){
  }
  }

这是我的studentviewcontroller树形图:

  public class StudentViewController {
    //A tree map to store the students
    private TreeMap<String, Student> studentMap = new TreeMap<String, Student>();

2 个答案:

答案 0 :(得分:1)

如果您有私有构造函数,那么您无法直接从任何其他类调用它。但是,您没有私有构造函数。您的类具有一个字段(具有私有访问权限),其类型为TreeMap<String,Student>且名为studentMap。这是一个私人构造函数。

private StudentViewController() {
  super();
}

答案 1 :(得分:0)

你不能从另一个类调用私有构造函数,私有构造函数,方法和变量只能由它的所有者类调用。