开发者

What kind of type I define an argument that is of determinate class and implements determinate interface? [closed]

开发者 https://www.devze.com 2023-03-28 02:02 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. 开发者_如何学Go Closed 10 years ago.

What type define an argument to make sure this is of determinate class and implements determinate interface? I'm using <T extends MyBaseClass & MyInterface> for argument type in method definition but my argument not extends MyBaseClass, IS of MyBaseClass type. I want to make sure that the argument that I recive are of MyBaseClass type and implements MyInterface


You can't. You either require the argument to be of a certain concrete type (and this means its subclasses are allowed too) or to implement a certain interface.

With generics you can force an argument to be parameterized with one or more types, such as subtypes are disallowed, but as far I can see it's not pertinent to your question.

You can however, use instanceof in the body as well as doing comparison with arg.getClass().


I'm going to assume that you are asking if you can do something like this --

public <T extends YourBaseClass, YourInterface> void foo(T bar) {

The answer is yes, you can. If you try passing an argument into this method thats YourBaseClass, but does not implement YourInterface, then the code will not compile.

0

精彩评论

暂无评论...
验证码 换一张
取 消