- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想知道为什么这是一个有效的覆盖:
public abstract class A {
public abstract <X> Supplier<X> getSupplier();
public static class B extends A {
@Override
public Supplier<String> getSupplier() {
return String::new;
}
}
}
然而这不是:
public abstract class A {
public abstract <X> Supplier<X> getSuppliers(Collection<String> strings);
public static class B extends A {
@Override
public Supplier<String> getSuppliers(Collection<String> strings) {
return String::new;
}
}
}
根据 JLS §8.4.8.1 , B.getSupplier
必须是子签名 A.getSupplier
:
An instance method mC declared in or inherited by class C, overrides from C another method mA declared in class A, iff all of the following are true:
- ...
- The signature of mC is a subsignature (§8.4.2) of the signature of mA.
- ...
子签名在 JLS §8.4.2 中定义:
Two methods or constructors, M and N, have the same signature if they have the same name, the same type parameters (if any) (§8.4.4), and, after adapting the formal parameter types of N to the the type parameters of M, the same formal parameter types.
The signature of a method m1 is a subsignature of the signature of a method m2 if either:
所以看起来 B.getSupplier
是 A.getSupplier
的子签名,但 B.getSuppliers
不是 A.getSuppliers
的子签名。
我想知道怎么会这样。
如果 B.getSupplier
是 A.getSupplier
的子签名,因为它具有相同的删除,则 B.getSuppliers
也必须具有与 A.getSuppliers
相同的删除。这应该足以让 getSuppliers
合法 - 但事实并非如此。
如果 B.getSupplier
是 A.getSupplier
的子签名,因为它具有相同的签名,那么我想知道“相同类型的参数(如果有的话)”到底是什么意味着。
如果考虑类型参数,那么它们应该有不同的类型参数:A.getSupplier
有类型参数X
,B.getSupplier
有没有。
如果不考虑类型参数,那么 getSuppliers
有何不同?
这更像是一个关于覆盖和泛型的学术问题,所以请不要建议重构代码(比如将类型参数 X
移动到类等)。
我正在寻找一个正式的、基于 JLS 的答案。
在我看来,B.getSupplier
不应覆盖 A.getSupplier
,因为它们没有相同的类型参数。这使得以下代码(产生 ClassCastException
)合法:
A b = new B();
URL url = b.<URL>getSupplier().get();
最佳答案
根据编译器输出,两个示例中的方法签名不同(使用 -Xlint:unchecked
选项编译代码以确认):
<X>getSupplier() in A (m2)
1st snippet
getSupplier() in B (m1)
<X>getSuppliers(Collection<String> strings) in A (m2)
2nd snippet
getSuppliers(Collection<String> strings) in B (m1)
根据JLS规范,方法 m1 的签名是方法 m2 签名的子签名,如果:
m2 has the same signature as m1, or
the signature of m1 is the same as the erasure of the signature of m2.
第一个语句不在游戏中 - 方法签名不同。但是第二个语句和删除呢?
B.getSupplier()
(m1) 是 A.<X>getSupplier()
的子签名(m2),因为:
- the signature of m1 is the same as the erasure of the signature of m2
<X>getSupplier()
删除后等于getSupplier()
.
B.getSuppliers(...)
(m1) 不是 A.<X>getSuppliers(...)
的子签名(m2),因为:
- the signature of m1 is not the same as the erasure of the signature of m2
m1的签名:
getSuppliers(Collection<String> strings);
删除m2的签名:
getSuppliers(Collection strings);
从 Collection<String>
更改 m1 参数到原始Collection
消除错误,在这种情况下 m1 成为 m2 的子签名。
第一个代码片段(有效覆盖):父类和子类中的方法签名最初是不同的。但是,在对父方法应用删除后,签名变得相同。
第二个代码片段(无效覆盖):方法签名最初不同,在对父方法应用删除后仍然不同。
关于java - 添加参数后覆盖具有泛型返回类型的方法失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50296958/
我想了解 Ruby 方法 methods() 是如何工作的。 我尝试使用“ruby 方法”在 Google 上搜索,但这不是我需要的。 我也看过 ruby-doc.org,但我没有找到这种方法。
Test 方法 对指定的字符串执行一个正则表达式搜索,并返回一个 Boolean 值指示是否找到匹配的模式。 object.Test(string) 参数 object 必选项。总是一个
Replace 方法 替换在正则表达式查找中找到的文本。 object.Replace(string1, string2) 参数 object 必选项。总是一个 RegExp 对象的名称。
Raise 方法 生成运行时错误 object.Raise(number, source, description, helpfile, helpcontext) 参数 object 应为
Execute 方法 对指定的字符串执行正则表达式搜索。 object.Execute(string) 参数 object 必选项。总是一个 RegExp 对象的名称。 string
Clear 方法 清除 Err 对象的所有属性设置。 object.Clear object 应为 Err 对象的名称。 说明 在错误处理后,使用 Clear 显式地清除 Err 对象。此
CopyFile 方法 将一个或多个文件从某位置复制到另一位置。 object.CopyFile source, destination[, overwrite] 参数 object 必选
Copy 方法 将指定的文件或文件夹从某位置复制到另一位置。 object.Copy destination[, overwrite] 参数 object 必选项。应为 File 或 F
Close 方法 关闭打开的 TextStream 文件。 object.Close object 应为 TextStream 对象的名称。 说明 下面例子举例说明如何使用 Close 方
BuildPath 方法 向现有路径后添加名称。 object.BuildPath(path, name) 参数 object 必选项。应为 FileSystemObject 对象的名称
GetFolder 方法 返回与指定的路径中某文件夹相应的 Folder 对象。 object.GetFolder(folderspec) 参数 object 必选项。应为 FileSy
GetFileName 方法 返回指定路径(不是指定驱动器路径部分)的最后一个文件或文件夹。 object.GetFileName(pathspec) 参数 object 必选项。应为
GetFile 方法 返回与指定路径中某文件相应的 File 对象。 object.GetFile(filespec) 参数 object 必选项。应为 FileSystemObject
GetExtensionName 方法 返回字符串,该字符串包含路径最后一个组成部分的扩展名。 object.GetExtensionName(path) 参数 object 必选项。应
GetDriveName 方法 返回包含指定路径中驱动器名的字符串。 object.GetDriveName(path) 参数 object 必选项。应为 FileSystemObjec
GetDrive 方法 返回与指定的路径中驱动器相对应的 Drive 对象。 object.GetDrive drivespec 参数 object 必选项。应为 FileSystemO
GetBaseName 方法 返回字符串,其中包含文件的基本名 (不带扩展名), 或者提供的路径说明中的文件夹。 object.GetBaseName(path) 参数 object 必
GetAbsolutePathName 方法 从提供的指定路径中返回完整且含义明确的路径。 object.GetAbsolutePathName(pathspec) 参数 object
FolderExists 方法 如果指定的文件夹存在,则返回 True;否则返回 False。 object.FolderExists(folderspec) 参数 object 必选项
FileExists 方法 如果指定的文件存在返回 True;否则返回 False。 object.FileExists(filespec) 参数 object 必选项。应为 FileS
我是一名优秀的程序员,十分优秀!