接口实现重写其自己的方法以将其自身创建为DEFAULT的对象 [英] Interface implements overriding its own methods to create an object of itself as DEFAULT

查看:103
本文介绍了接口实现重写其自己的方法以将其自身创建为DEFAULT的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了许多有关问题的主题,这些问题看起来像我要问的问题.但是,我找不到可以用于我的问题的令人满意的答案,因为我的问题有很多折,分为三个方面.

I've read many threads with questions which looks like questions I am going to ask. However, I can not find satisfactory answers which could be applied to my questions, since there are more than one fold in my questions, seperated in three aspects.

这是据我了解,接口是抽象的,不能具体实现方法(也许新的Java允许这样做,但是由于我是Java的新手,所以请坚持使用Java的一般规则).因此,对我来说有意义的是,接口中有两个抽象方法声明,分别是布尔值supportsFormat(格式格式)"和"SubtitleDecoder createDecoder(格式格式)".但是我不明白,我的问题是:

As I understand, an interace is abstraction and can not have concrete implementation of methods (maybe new java allows this, but since I am new to java, let's stick with the genaral rules of Java). So It makes sense to me that there are two absract mothod declarations in the interface, which are "boolean supportsFormat(Format format)" and "SubtitleDecoder createDecoder(Format format)". But what I don't understand and my questions are:

  1. 为什么此接口可以实现"SubtitleDecoderFactory DEFAULT = new SubtitleDecoderFactory()",它看起来像是初始化方法的实现?

  1. Why can this interface implements "SubtitleDecoderFactory DEFAULT = new SubtitleDecoderFactory()", which looks like an implementation of initializing method?

此接口通过覆盖其自身的方法来初始化自身,这是没有意义的动作吗? (接口是要有其他类来实现它,而不是本身,是吗?)

This interface initializes itself by overriding its own methods, is it an meaningless action? (an interface is meant to have other classes to implement it, not itself, am I correct?)

假设问题1和2有效,在接口中创建自身对象的好处是什么?

What are the benifits of creating an object of itself in an interface, assuming questions 1 and 2 are valid?

实现该接口的类的实例是否具有该界面的DEFAULT实例?

Is it that an instance of a class which implements this interface will have the instance of DEFAULT of this intetface?


public interface SubtitleDecoderFactory {


  boolean supportsFormat(Format format);


  SubtitleDecoder createDecoder(Format format);


  SubtitleDecoderFactory DEFAULT =
      new SubtitleDecoderFactory() {

        @Override
        public boolean supportsFormat(Format format) {
          @Nullable String mimeType = format.sampleMimeType;
          return MimeTypes.TEXT_VTT.equals(mimeType)
              || MimeTypes.TEXT_SSA.equals(mimeType)
              || MimeTypes.APPLICATION_TTML.equals(mimeType)
              || MimeTypes.APPLICATION_MP4VTT.equals(mimeType)
              || MimeTypes.APPLICATION_SUBRIP.equals(mimeType)
              || MimeTypes.APPLICATION_TX3G.equals(mimeType)
              || MimeTypes.APPLICATION_CEA608.equals(mimeType)
              || MimeTypes.APPLICATION_MP4CEA608.equals(mimeType)
              || MimeTypes.APPLICATION_CEA708.equals(mimeType)
              || MimeTypes.APPLICATION_DVBSUBS.equals(mimeType)
              || MimeTypes.APPLICATION_PGS.equals(mimeType);
        }

        @Override
        public SubtitleDecoder createDecoder(Format format) {
          @Nullable String mimeType = format.sampleMimeType;
          if (mimeType != null) {
            switch (mimeType) {
              case MimeTypes.TEXT_VTT:
                return new WebvttDecoder();
              case MimeTypes.TEXT_SSA:
                return new SsaDecoder(format.initializationData);
              case MimeTypes.APPLICATION_MP4VTT:
                return new Mp4WebvttDecoder();
              case MimeTypes.APPLICATION_TTML:
                return new TtmlDecoder();
              case MimeTypes.APPLICATION_SUBRIP:
                return new SubripDecoder();
              case MimeTypes.APPLICATION_TX3G:
                return new Tx3gDecoder(format.initializationData);
              case MimeTypes.APPLICATION_CEA608:
              case MimeTypes.APPLICATION_MP4CEA608:
                return new Cea608Decoder(mimeType, format.accessibilityChannel);
              case MimeTypes.APPLICATION_CEA708:
                return new Cea708Decoder(format.accessibilityChannel, format.initializationData);
              case MimeTypes.APPLICATION_DVBSUBS:
                return new DvbDecoder(format.initializationData);
              case MimeTypes.APPLICATION_PGS:
                return new PgsDecoder();
              default:
                break;
            }
          }
          throw new IllegalArgumentException(
              "Attempted to create decoder for unsupported MIME type: " + mimeType);
        }
      };
}

推荐答案

  1. 为什么此接口可以实现"SubtitleDecoderFactory DEFAULT = new SubtitleDecoderFactory()",它看起来像是初始化方法的实现?

该接口未执行任何操作.您拥有的是名为DEFAULTpublicstaticfinal 字段,该字段分配了SubtitleDecoderFactory的实例.

The interface is not implementing anything. What you have is a public, static, final field named DEFAULT which is assigned an instance of SubtitleDecoderFactory.

  1. 此接口通过覆盖其自身的方法来初始化自身,这是没有意义的动作吗? (接口是要有其他类来实现它,而不是本身,是吗?)

同样,该接口既不是初始化自身"也不是实现自身.您所拥有的是一个匿名类的示例.这个匿名类正在实现SubtitleDecoderFactory.实例化匿名类,并将实例分配给DEFAULT字段;当初始化 时会发生这种情况.

Again, the interface is not "initializing itself" nor implementing itself. What you have is an example of an anonymous class. It is this anonymous class which is implementing SubtitleDecoderFactory. The anonymous class is instantiated and the instance is assigned to the DEFAULT field; this happens when the Class is initialized.

  1. 假设问题1和2有效,在接口中创建自身对象的好处是什么?

在这种情况下,提供了SubtitleDecoderFactory的默认实现.这个默认实现只有一个实例,因为在接口中声明的所有字段都是隐式的public,static和final.这是一个 singleton 的示例.

In this case, a default implementation of SubtitleDecoderFactory is being provided. There's also only ever a single instance of this default implementation, since all fields declared in an interface are implicitly public, static, and final; this is an example of a singleton.

这里的一个可能的优点是您不必声明另一个命名的类.而且由于目标显然是单身,因此命名类也不需要 .

One possible advantage here is you don't have to declare another named class. And since the goal is apparently to have a singleton, there's no need for a named class either.

  1. 实现该接口的类的实例是否具有此接口[ sic ]的DEFAULT实例?
  1. Is it that an instance of a class which implements this interface will have the instance of DEFAULT of this intetface [sic]?

请记住,DEFAULT是引用匿名类实例的字段. SubtitleDecoderFactory的所有实现都可以访问此字段吗?是的,但不是特别因为他们正在实现接口.

Remember that DEFAULT is a field which references an instance of an anonymous class. Will all implementations of SubtitleDecoderFactory have access to this field? Yes, but not specifically because they are implementing the interface.

由于接口中声明的所有字段都是公共字段,静态字段,并且最后一个DEFAULT字段是 constant (尽管不是 compile-time constant ).静态字段与该类关联,而不是与该类的实例关联.换句话说,任何可以看到" SubtitleDecoderFactory的类都可以访问DEFAULT字段.例如:

Since all fields declared in an interface are public, static, and final the DEFAULT field is a constant (though not a compile-time constant). Static fields are associated with the class instead of instances of that class. In other words, any class which can "see" SubtitleDecoderFactory has access to the DEFAULT field. For example:

/* import SubtitleDecoderFactory interface as needed */

public class Main {

  public static void main(String[] args) {
    System.out.println(SubtitleDecoderFactory.DEFAULT == SubtitleDecoderFactory.DEFAULT);
  }

}

这篇关于接口实现重写其自己的方法以将其自身创建为DEFAULT的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆