欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > java >内容正文

java

java 一般方法_一般覆盖Java中的方法

发布时间:2025/3/20 java 38 豆豆
生活随笔 收集整理的这篇文章主要介绍了 java 一般方法_一般覆盖Java中的方法 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

JLS # 8.4.2. Method Signature

The signature of a method m1 is a subsignature of the signature of a method m2 if either:

m2 has the same signature as m1, or

the signature of m1 is the same as the erasure (§4.6) of the

signature of m2.

根据上述规则,因为您的父母没有擦除,您的孩子有一个,所以它不是一个有效的覆盖。

示例8.4.8.3-4。擦除影响覆盖

类不能有两个具有相同名称和类型的成员方法擦除:

class C {

T id (T x) {...}

}

class D extends C {

Object id(Object x) {...}

}

这是非法的,因为D.id(Object)是D的成员,C.id(String)被声明为D的超类型,并且:

>这两个方法具有相同的名称,id

> C.id(String)可以访问D

> D.id(Object)的签名不是其中的一个子签名

C.id(String)

>这两种方法有相同的擦除

类的两种不同方法可能不会覆盖具有相同擦除的方法:

class C {

T id(T x) {...}

}

interface I {

T id(T x);

}

class D extends C implements I {

public String id(String x) {...}

public Integer id(Integer x) {...}

}

这也是非法的,因为D.id(String)是D的成员,D.id(Integer)在D中声明,并且:

>这两个方法具有相同的名称,id

> D.id(整数)可以访问D

>这两种方法有不同的签名(也不是一个

另一方的子签)

> D.id(String)覆盖C.id(String)和D.id(Integer)

覆盖了I.id(整数),但是这两个被覆盖的方法都是一样的

擦除

还给出了从超级到小孩允许的情况的例子

The notion of subsignature is designed to express a relationship between two methods whose signatures are not identical, but in which one may override the other. Specifically, it allows a method whose signature does not use generic types to override any generified version of that method. This is important so that library designers may freely generify methods independently of clients that define subclasses or subinterfaces of the library.

考虑一下这个例子:

class CollectionConverter {

List toList(Collection c) {...}

}

class Overrider extends CollectionConverter {

List toList(Collection c) {...}

}}

现在假设这个代码是在引入泛型之前编写的,现在CollectionConverter类的作者决定生成代码,因此:

class CollectionConverter {

List toList(Collection c) {...}

}

没有特别的分配,Overrider.toList将不再覆盖CollectionConverter.toList。相反,代码将是非法的。这将大大抑制泛型的使用,因为图书馆作家会犹豫迁移现有的代码。

总结

以上是生活随笔为你收集整理的java 一般方法_一般覆盖Java中的方法的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。