有时,您可能希望限制可在参数化类型中用作类型参数的类型。例如,对数字进行操作的方法可能只希望接受数字或其子类的实例。这就是有界类型参数的用途。
null
- 有时我们不希望整个类被参数化。在这种情况下,我们可以创建一个Java 仿制药 方法由于构造函数是一种特殊的方法,我们也可以在构造函数中使用泛型类型。
- 假设我们想要限制可以在参数化类型中使用的对象的类型。例如,在比较两个对象的方法中,我们希望确保接受的对象是可比较的。
- 这些方法的调用类似于无界方法,只是如果我们试图使用任何不可比较的类,它将抛出编译时错误。
如何在Java中声明有界类型参数?
- 列出类型参数的名称,
- 以及extends关键字
- 根据它的上限。(在下面的示例c中 A. .)
语法
<T extends superClassName>
请注意,在本文中,extends在一般意义上表示“extends”(如在类中)。此外,它还指定T只能由超类名或超类名的子类替换。因此,超类定义了一个包含的上限。
让我们举一个例子,说明如何用泛型实现有界类型(扩展超类)。
JAVA
// This class only accepts type parametes as any class // which extends class A or class A itself. // Passing any other type will cause compiler time error class Bound<T extends A> { private T objRef; public Bound(T obj){ this .objRef = obj; } public void doRunTest(){ this .objRef.displayClass(); } } class A { public void displayClass() { System.out.println( "Inside super class A" ); } } class B extends A { public void displayClass() { System.out.println( "Inside sub class B" ); } } class C extends A { public void displayClass() { System.out.println( "Inside sub class C" ); } } public class BoundedClass { public static void main(String a[]) { // Creating object of sub class C and // passing it to Bound as a type parameter. Bound<C> bec = new Bound<C>( new C()); bec.doRunTest(); // Creating object of sub class B and // passing it to Bound as a type parameter. Bound<B> beb = new Bound<B>( new B()); beb.doRunTest(); // similarly passing super class A Bound<A> bea = new Bound<A>( new A()); bea.doRunTest(); } } |
输出
Inside sub class CInside sub class BInside super class A
现在,我们仅限于类型A及其子类,因此它将为任何其他类型的子类抛出一个错误。
JAVA
// This class only accepts type parametes as any class // which extends class A or class A itself. // Passing any other type will cause compiler time error class Bound<T extends A> { private T objRef; public Bound(T obj){ this .objRef = obj; } public void doRunTest(){ this .objRef.displayClass(); } } class A { public void displayClass() { System.out.println( "Inside super class A" ); } } class B extends A { public void displayClass() { System.out.println( "Inside sub class B" ); } } class C extends A { public void displayClass() { System.out.println( "Inside sub class C" ); } } public class BoundedClass { public static void main(String a[]) { // Creating object of sub class C and // passing it to Bound as a type parameter. Bound<C> bec = new Bound<C>( new C()); bec.doRunTest(); // Creating object of sub class B and // passing it to Bound as a type parameter. Bound<B> beb = new Bound<B>( new B()); beb.doRunTest(); // similarly passing super class A Bound<A> bea = new Bound<A>( new A()); bea.doRunTest(); Bound<String> bes = new Bound<String>( new String()); bea.doRunTest(); } } |
输出:
error: type argument String is not within bounds of type-variable T
多重界限
有界类型参数可以与方法、类和接口一起使用。
Java泛型还支持多个边界,即在本例中,A可以是接口或类。如果A是类,那么B和C应该是接口。在多个边界中不能有多个类。
语法:
<T extends superClassName & Interface>
JAVA
class Bound<T extends A & B> { private T objRef; public Bound(T obj){ this .objRef = obj; } public void doRunTest(){ this .objRef.displayClass(); } } interface B { public void displayClass(); } class A implements B { public void displayClass() { System.out.println( "Inside super class A" ); } } public class BoundedClass { public static void main(String a[]) { //Creating object of sub class A and //passing it to Bound as a type parameter. Bound<A> bea = new Bound<A>( new A()); bea.doRunTest(); } } |
输出
Inside super class A
本文由 萨凯特·库马尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END