简体   繁体   English

Java - Eclipse问题中的多个包

[英]Java - Multiple Package In Eclipse Issue

I have the following set of packages within my source folder. 我的源文件夹中有以下一组包。 The packages are shapes,model,views. 包是形状,模型,视图。

Say I have a class file in my model folder that has the following piece of code: 假设我的模型文件夹中有一个类文件,其中包含以下代码:

  shapes.interfaceforshapes[][] temp = model.get2dshapearray();


if(temp[x][y].getClass().isInstance(shapes.cTriangle)){

            }

Please note in the above code temp[x][y] will return a class that interfaces my shapeInterface and all classes within the shapes folder interface this. 请注意,在上面的代码中, temp[x][y]将返回一个接口我的shapeInterface类和shapes文件夹接口中的所有类。

Am I doing the correct thing to say "Is the class within my array of type cTriangle"? 我是否正确地说“我的数组cTriangle中的类是什么”?

I currently get the error: 我目前收到错误:

shapes.cTriangle cannot be resolved to a variable shapes.cTriangle无法解析为变量

but I don't want to match a variable, I want to test it agaisnt the class cTriangle within my package shape. 但我不想匹配变量,我想测试它在我的包形状中的类cTriangle

Thanks 谢谢

如果要检查对象是否是某个类的实例,请使用instanceOf运算符,而isInstance方法需要类的实例。

  if( temp[x][y] instanceOf shapes.cTriangle) {//dosomething }

That is not how isInstance(Object) works. 这不是isInstance(Object)工作原理。 You have to call it on a class and pass in the object you want to match. 您必须在类上调用它并传入要匹配的对象。 You would do: 你会这样做:

shapes.cTriangle.class.isInstance(temp[x][y]);

assuming cTriangle is a class and temp[x][y] returns an object and you want to check if that object is of type cTriangle . 假设cTriangle是一个类, temp[x][y]返回一个对象,并且您想要检查该对象是否为cTriangle类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM