繁体   English   中英

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

[英]Can you call a private constructor from another class?

因此,我正在尝试编写一种方法,该方法将放入树状图中的学生列表写入,但树状图位于StudentViewcontroller中。 我可以从我的write Student方法中调用它吗?

  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>();

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

private StudentViewController() {
  super();
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM