我正在尝试编写一个方法来返回与它作为参数获取的类匹配的所有对象:
public class Scenario extends View {
...
private Actor[] actors = new Actor[1024];
...
public Actor[] getActors(Class<?> cls) {
//Count actors corresponding to class cls
int cnt = 0;
for (int i = 0; i<actorsCount; i++)
if (actors[i] instanceof cls) cnt++;
//Build a new array;
Actor[] clsActors = new Actor[cnt];
//Fill it
for (int j = 0, k=0; j<cnt; k++)
if (actors[k] instanceof cls)
clsActors[j++] = actors[k];
return clsActors;
}
}
但是,我收到一个错误:"- 不兼容的操作数类型 boolean 和 Class<capture#1-of ? extends Scenario>"
'Actor' 由我的 Sprite 扩展,比如 Bird、Hero 等。 例如,这个想法是在给定时间获取场景中所有鸟类的列表以进行某些计算。
知道这里发生了什么吗?如何测试给定对象是否是给定类的实例?
最佳答案
import java.util.Arrays;
public class Main
{
static class Actor {}
static class Frog extends Actor {@Override public String toString() {return "I'm a frog";}}
static class Lizard extends Actor {@Override public String toString() {return "I'm a lizard";}}
private static Actor[] actors;
public static Actor[] getActors(Class<?> cls) {
//Count actors corresponding to class cls
int cnt = 0;
for (int i = 0; i<actors.length; i++)
if (cls.isInstance(actors[i])) cnt++;
//Build a new array;
Actor[] clsActors = new Actor[cnt];
//Fill it
for (int j = 0, k=0; j<cnt; k++)
if (cls.isInstance(actors[k]))
clsActors[j++] = actors[k];
return clsActors;
}
public static void main(String[] args)
{
actors = new Actor[] {new Frog(), new Lizard()};
System.out.println(Arrays.toString(getActors(Frog.class)));
}
}
输出:
[I'm a frog]
编辑:使用列表的更优雅的 getActors() 版本:
public static Actor[] getActors(Class<?> cls) {
LinkedList<Actor> chosenActors = new LinkedList<Actor>();
for(Actor actor: actors) if(cls.isInstance(actor)) chosenActors.add(actor);
return chosenActors.toArray(new Actor[0]);
}
关于java - instanceof Class<?> 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13308102/
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere
我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"
我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request
两者都可以defsetup(options={})options.reverse_merge:size=>25,:velocity=>10end和defsetup(options={}){:size=>25,:velocity=>10}.merge(options)end在方法的参数中分配默认值。问题是:哪个更好?您更愿意使用哪一个?在性能、代码可读性或其他方面有什么不同吗?编辑:我无意中添加了bang(!)...并不是要询问nobang方法与bang方法之间的区别 最佳答案 我倾向于使用reverse_merge方法:option
我有一个只接受一个参数的方法:defmy_method(number)end如果使用number调用方法,我该如何引发错误??通常,我如何定义方法参数的条件?比如我想在调用的时候报错:my_method(1) 最佳答案 您可以添加guard在函数的开头,如果参数无效则引发异常。例如:defmy_method(number)failArgumentError,"Inputshouldbegreaterthanorequalto2"ifnumbereputse.messageend#=>Inputshouldbegreaterthano
我使用Nokogiri(Rubygem)css搜索寻找某些在我的html里面。看起来Nokogiri的css搜索不喜欢正则表达式。我想切换到Nokogiri的xpath搜索,因为这似乎支持搜索字符串中的正则表达式。如何在xpath搜索中实现下面提到的(伪)css搜索?require'rubygems'require'nokogiri'value=Nokogiri::HTML.parse(ABBlaCD3"HTML_END#my_blockisgivenmy_bl="1"#my_eqcorrespondstothisregexmy_eq="\/[0-9]+\/"#FIXMEThefoll