简体   繁体   English

为什么任何类型的EXTENDS接口?

[英]Why any type can EXTENDS interface?

Today I got lost in Java polymorphism. 今天我迷失了Java多态性。 I have done something that I have thought that is impossible. 我做过一些我认为不可能完成的事情。 Namely - I have EXTENDED interface. 即 - 我有EXTENDED界面。

I have created empty interface: 我创建了空接口:

public interface Figure {}

Nothing new right now. 现在没什么新鲜的。 Then I have created parametrized class Board with paramether that EXTENDS my interface! 然后我创建了带参数的参数化类板,扩展了我的界面!

public class Board< T extends Figure > { 
    T[][] board;
}

I thought it is not be possible! 我以为这是不可能的! Nevertheless I was going further with my not understanding. 尽管如此,我还是因为不理解而走得更远。

I have created class implementing Figure interface: 我已经创建了实现Figure接口的类:

public class Cross implements Figure{}

And to my suprise I was able to do that: 令我惊讶的是,我能够做到这一点:

Board b = new Board<Cross>();

Please help me and explain me this strange situation. 请帮帮我解释一下这种奇怪的情况。
I know a little about polymorphism and I understand that Cross is Figure. 我对多态性了解不多,我知道Cross就是图。 I am confused howcome any type is able to EXTEND interface, and howcome classes that implements interaface (not extending interface) are correct as paramether which extends interface. 我很困惑,任何类型都能够EXTEND接口,并且实现交互式(不扩展接口)的howcome类是正确的,作为扩展接口的paramether。

Please help me and explain this polymorphicall mess. 请帮助我解释这个多态的混乱。 Thanks. 谢谢。

When you say public class Board< T extends Figure > , it means that the Board can accept either Figure or sub-interfaces of Figure or any classes which implement these interfaces. 当你说public class Board< T extends Figure > ,这意味着董事会可以接受Figure或子接口Figure或实现这些接口的类。 So, when you say: 所以,当你说:

public class Cross implements Figure{}

and

Board b = new Board<Cross>();

This means that Cross has implemented Figure , and thus Board can accept Cross , since Cross IS-A Figure 这意味着Cross已经实现了Figure ,因此Board可以接受Cross ,因为Cross IS-A Figure

As far as extending an interface is concerned, only interfaces can extend interfaces. 就扩展接口而言,只有接口可以扩展接口。 So, the following is legal: 所以,以下是合法的:

public interface TwoDimensionalFigure extends Figure{}.

So any class which implements TwoDimensionalFigure can also be accepted by Board . 因此,任何实现TwoDimensionalFigure类也可以被Board接受。

By doing 通过做

public class Board< T extends Figure > { 
    T[][] board;
}

You are creating a bounded generic type, T which should have its super class of type Figure 您正在创建一个有界泛型类型, T应该具有类型为 Figure 超类

As explained in this thread, there is no difference between definig a generic type which extends an interface or a class . 正如主题中所解释的那样,定义扩展 接口的泛型类型之间没有区别。

What compiler makes sure is that, when you create Board , T will be supplied with any concrete class that either extends Figure or implements Figure 编译器确保的是,当您创建BoardT将提供任何具体的类,可以extends Figureimplements Figure

Actully you didn't extend the interface. 实际上你没有扩展界面。 You just create a generic class with the type Cross. 您只需创建一个Cross类型的泛型类。 The definition of your class merely specifies that the the parameterized type must be derived from Figure, ie implementing Figure or a derivative from it. 您的类的定义仅指定参数化类型必须从图中派生,即实现图或其衍生。

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

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