我想知道为什么这是一个有效的覆盖:
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/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/
我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..
我想了解Ruby方法methods()是如何工作的。我尝试使用“ruby方法”在Google上搜索,但这不是我需要的。我也看过ruby-doc.org,但我没有找到这种方法。你能详细解释一下它是如何工作的或者给我一个链接吗?更新我用methods()方法做了实验,得到了这样的结果:'labrat'代码classFirstdeffirst_instance_mymethodenddefself.first_class_mymethodendendclassSecond使用类#returnsavailablemethodslistforclassandancestorsputsSeco
我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除