简体   繁体   English

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

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

So I am trying to write a method that writes a list of students put into a treemap, but my treemap is in my StudentViewcontroller. 因此,我正在尝试编写一种方法,该方法将放入树状图中的学生列表写入,但树状图位于StudentViewcontroller中。 Am I able to call it from my into my write student method. 我可以从我的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){
  }
  }

Here is my treemap for the studentviewcontroller: 这是我为studentviewcontroller的树图:

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

If you had a private constructor then you couldn't invoke it directly from any other class. 如果您有私有构造函数,则无法直接从任何其他类调用它。 However, you don't have a private constructor. 但是,您没有私有的构造函数。 Your class has a field (with private access permissions) which is of type TreeMap<String,Student> and named studentMap . 您的班级有一个字段(具有私有访问权限),该字段的类型为TreeMap<String,Student> ,名称为studentMap This is a private constructor. 这是一个私有构造函数。

private StudentViewController() {
  super();
}

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

暂无
暂无

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

相关问题 为什么不能从构造函数是私有的类继承? - Why can you not inherit from a class whose constructor is private? 如何从子类构造函数调用超类的私有构造函数? - How to call private constructor of super class from child class constructor? 在Java中,你可以为另一个类的构造函数中的一个类调用一个setter吗? - In Java, can you call a setter for one class inside a constructor for another class? 在groovy中仅使用来自另一个类的私有构造函数的类的实例化 - Instantiation of a class with only private constructor from another class in groovy 从另一个类使用私有类构造函数访问内部子类 - Accessing an inner subclass with private class constructor from another class 如何从另一个类中的public类型的参数化构造函数中调用默认类型的参数化构造函数? - How can I call parameterized constructor of default type from a parameterized constructor of type public which is in another class? 如何从没有构造函数的另一个类调用方法 - How to call a method from another class with no constructor 防止在 Java 类中调用私有构造函数 - Preventing call of a private constructor from within the class in Java 可以在类的构造函数中使用“new”来调用Java中的另一个构造函数吗? - Can “new” be used inside the constructor of the class to call another constructor in Java? 如何使用来自另一个类的私有变量打印默认构造函数 - how to print the default constructor with private variables from another class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM