简体   繁体   English

Java中的构造函数中的super(Myclass.class)是什么意思?

[英]what does super(Myclass.class) in constructor mean in Java?

I am seeing the following kind of code repeatedly in Mapreduce programs. 我在Mapreduce程序中反复看到以下类型的代码。 This is just a snippet of code taken out. 这只是摘录的一小段代码。 The entire code is available here 完整的代码在这里

what does the super call in the constructor do? 构造函数中的super调用做什么? Does it call the constructor of IntPair.class ? 它会调用IntPair.class的构造IntPair.class吗? why is such a call necessary. 为什么需要这样的电话。

public static class KeyComparator extends WritableComparator {
        protected KeyComparator() {
          super(IntPair.class, true);
        }
        @Override
        public int compare(WritableComparable w1, WritableComparable w2) {
          IntPair ip1 = (IntPair) w1;
          IntPair ip2 = (IntPair) w2;
          int cmp = IntPair.compare(ip1.getFirst(), ip2.getFirst());
          if (cmp != 0) {
            return cmp;
          }
          return -IntPair.compare(ip1.getSecond(), ip2.getSecond()); //reverse
        }
      }

super(), when used in the constructor will call the constructor of the class which is extended. super()在构造函数中使用时,将调用扩展类的构造函数。 In this case, it will call the WriteableComparator constructor. 在这种情况下,它将调用WriteableComparator构造函数。 You can see more details on super in the java documentation here: Java super link 您可以在以下Java文档中查看有关super的更多详细信息: Java super链接

In particular, see the section titled "Subclass Constructors". 特别是,请参见标题为“子类构造函数”的部分。

Does it call the constructor of IntPair.class? 它会调用IntPair.class的构造函数吗?

No, It's just a argument that is passed in the super class constructor. 不,这只是超类构造函数中传递的一个参数。

why is such a call necessary? 为什么需要这样的电话?

To avoid the call of no arguments constructor of the super class that is by default added by the compiler. 为避免调用默认情况下由编译器添加的超类的构造函数,该方法不调用任何参数。

If there are multiple constructors in the super class then you can be specified which constructor of the super class should be called based on arguments. 如果超类中有多个构造函数,则可以指定基于参数调用超类的哪个构造函数。


Sample code: (Look at the output when KeyComparator object is created) 示例代码:(在创建KeyComparator对象时查看输出)

class WritableComparator {
    public WritableComparator(){
        System.out.println("default constructor is called");
    }
    public WritableComparator(Class<?> clazz, boolean flag) {
        System.out.println("two arguemnts constructor is called");
    }
}

class KeyComparator extends WritableComparator {
    public KeyComparator() {
        //super(); // by default added by the compiler
        super(IntPair.class, true);
    }
}

IntPair.class notation is used to get object describing this class. IntPair.class表示法用于获取描述此类的对象。 Like here: 像这儿:

Class<?> type = Integer.class;

This type variable is not an instance of Integer class, but instance of object describing Integer type. type变量不是Integer类的实例,而是描述Integer类型的对象的实例。

Some classes requires you to pass them type of objects you want them to work with. 有些类要求您向它们传递要使用的对象type Then, they can for example create new instances of those classes, using this type object. 然后,他们可以例如使用此type对象创建这些类的新实例。

more about Class class 有关班级的更多信息

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

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