Java使用instanceof检查不同包中具有相同名称的类

时间:2022-09-08 16:54:28

I have two files with same name in different packages, in certain function i need to check the argument passed is of the instanceof which class it is.

我在不同的包中有两个同名的文件,在某些函数中,我需要检查传递的参数是它的类的实例。

Eg.

if(input instanceof x.y.test) {
      //do something
    } else if(input instanceof x.y.z test) {
      //do something
    }

But in many places I read that using instanceof is not a good practice. What kinda of alternate I can apply here.

但在很多地方我都知道使用instanceof不是一个好习惯。我可以在这里申请什么样的候补。

Note: One file is of .groovy and other one is .java file, but both representing the same domain

注意:一个文件是.groovy,另一个是.java文件,但都表示相同的域

1 个解决方案

#1


It's a bit general claim, to say "instanceof is a bad practice". It depends on the requirements, technical limitations, personal taste etc.

这是一个普遍的主张,说“实例是一种不好的做法”。这取决于要求,技术限制,个人品味等。

However, it is a point worth considering. Sometimes it means someone missed an opportunity to use polymorphism. For example, if your code says

但是,这是一个值得考虑的问题。有时它意味着有人错过了使用多态的机会。例如,如果你的代码说的话

if (input instanceof x.y.test){
    greeting = "hello from xy";
}
else if (input instanceof x.y.z.test){
   greeting = "hello from xyz";
}

Then it might be nicer if each 'test' had a method 'greet()':

如果每个'test'都有一个'greet()'方法,那么它可能会更好:

greeting = input.greet();
// where class x.y.test  has public String greet(){return "hello from xy";}
// and class x.y.z.test  has public String greet(){return "hello from xyz";}

I say might, because it has a strong advantage: people can now add lots of other 'test' implementations, and your main code will accept them seamlessly without having to add 'if-else'.
However, it might also have disadvantages: if you feel it's wrong for your 'test' class to be responsible for the greeting calculation; or if it's a 3rd party outside your control. That's why I'm against generalizations.

我说可能,因为它具有很强的优势:人们现在可以添加许多其他“测试”实现,并且您的主代码将无缝地接受它们而无需添加“if-else”。但是,它也可能有缺点:如果你认为你的'测试'课程负责问候计算是错误的;或者如果它是你控制之外的第三方。这就是我反对概括的原因。

There are more elaborate solutions - e.g. lookup the 'visitor' design pattern.

有更复杂的解决方案 - 例如查找“访客”设计模式。

#1


It's a bit general claim, to say "instanceof is a bad practice". It depends on the requirements, technical limitations, personal taste etc.

这是一个普遍的主张,说“实例是一种不好的做法”。这取决于要求,技术限制,个人品味等。

However, it is a point worth considering. Sometimes it means someone missed an opportunity to use polymorphism. For example, if your code says

但是,这是一个值得考虑的问题。有时它意味着有人错过了使用多态的机会。例如,如果你的代码说的话

if (input instanceof x.y.test){
    greeting = "hello from xy";
}
else if (input instanceof x.y.z.test){
   greeting = "hello from xyz";
}

Then it might be nicer if each 'test' had a method 'greet()':

如果每个'test'都有一个'greet()'方法,那么它可能会更好:

greeting = input.greet();
// where class x.y.test  has public String greet(){return "hello from xy";}
// and class x.y.z.test  has public String greet(){return "hello from xyz";}

I say might, because it has a strong advantage: people can now add lots of other 'test' implementations, and your main code will accept them seamlessly without having to add 'if-else'.
However, it might also have disadvantages: if you feel it's wrong for your 'test' class to be responsible for the greeting calculation; or if it's a 3rd party outside your control. That's why I'm against generalizations.

我说可能,因为它具有很强的优势:人们现在可以添加许多其他“测试”实现,并且您的主代码将无缝地接受它们而无需添加“if-else”。但是,它也可能有缺点:如果你认为你的'测试'课程负责问候计算是错误的;或者如果它是你控制之外的第三方。这就是我反对概括的原因。

There are more elaborate solutions - e.g. lookup the 'visitor' design pattern.

有更复杂的解决方案 - 例如查找“访客”设计模式。