按实际类型使用方法

发布于 2024-12-09 03:39:36 字数 558 浏览 0 评论 0 原文

我了解到我可以使用对象的真实类型来定义使用哪个方法,如下所示:

[...]
Object foo = new String("hello");
[...]
bla(foo);

void bla(String x){
}
void bla(Integer x){
}

现在它应该采用 bla(String x) 方法,但我却收到编译器错误。我知道为什么:因为 foo 的类型当前是 Object。我不确定我现在这样做是否正确,但我是这样理解的,Java将通过真实类型(或特定类型)选择方法,所以如果我将其向下转换为Object,如果没有指定Object方法,它将选择String

或者是在方法 void bla(Object x) 中通过 if(foo instanceof xxx) 确定类型的唯一方法?

PS:不要误解我的意思:我并不意味着我可以重载方法,我的意思是我想根据实际类型(而不是定义的类型)选择方法!

I learned that I can use the real type of a Object to define which Method is used, such like this:

[...]
Object foo = new String("hello");
[...]
bla(foo);

void bla(String x){
}
void bla(Integer x){
}

Now it should take the bla(String x) method, but instead I get compiler error. I know why: because the type of foo is currently Object. I'm not sure if I do it currently right, but I understood it that way, that Java will choose the method by the real type (or specific type), so if I downcast it to Object, it will choose the String instead, if no Object Method is specified.

Or is the only way to determinate the type by if(foo instanceof xxx) in a method void bla(Object x)?

P.S.: dont get me wrong on this: I dont mean that I can overload methods, I mean that I want to choose the method based on the real type (not on the defined one)!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-12-16 03:39:36

现在它应该采用 bla(String x) 方法,但我得到了编译器错误。

对。在运行时调用哪个方法是在编译时选择的,并且编译器(通常)无法判断 foo 在运行时将具有哪种类型。

...所以如果我将其向下转换为对象...

你不能“向下转换为对象”。不过,您可以向下转换为 String 。没错,如果您这样做,

bla((String) foo);

它将调用以 String 作为参数的 bla(如果 foo< 则抛出 ClassCastException /code> 不是 String)。

或者是方法 void bla(Object x) 中的 if(foo instanceof xxx) 确定类型的唯一方法?

[...]

我的意思是我想根据实际类型(而不是定义的类型)选择方法!

这通常称为多重调度,并且是一项不受支持的功能在爪哇。

如果您确实需要,请实现访问者模式。


顺便说一句,您所说的“定义类型”和“实际类型”通常分别称为静态类型运行时类型。也就是说,Objectfoo静态类型,而 String 是它的运行时类型嗯>。

Now it should take the bla(String x) method, but instead i get compiler error.

Right. Which method is to be called at runtime, is chosen at compile-time, and the compiler can't (in general) tell which type foo will have in runtime.

...so if i downcast it to Object...

You can't "downcast to Object". You can downcast to a String though. And that's right, if you do

bla((String) foo);

it will invoke the bla that takes a String as argument (and throw a ClassCastException if foo is not a String).

Or is the only way to determinate the type by if(foo instanceof xxx) in a Method void bla(Object x)?

[...]

i mean that i want to choose the method based on the real type (not on the defined one)!

This is usually referred to as double or multiple dispatch, and is a feature not supported in Java.

Implement the visitor pattern if you really need this.


Btw, what you refer to as "defined type" and "real type" are usually referred to as static type and runtime type respectively. That is, Object is the static type of foo, while String is it's runtime type.

美人如玉 2024-12-16 03:39:36

是的,你必须使用 instanceof 来获取真正的类型。 bla( Object ) 将始终调用 bla( Object o ) 方法。

注意:基于“真实类型”选择方法仅适用于类方法。对象.方法(...)。如果存在的话,将根据“真实类型”选择方法(并向上直到对象)。

Object foo = new String("hello");

// this will return "hello" although toString() 
// from Object only prints the reference
System.out.println( foo.toString() );

Yeah you have to use instanceof to get the realy type. bla( Object ) will always call the bla( Object o ) method.

Note: Chosing method based on the "real type" is only possible for class methods. object.method( ... ). Will chose the method based on the "real type" if one exists (and go up till Object).

Object foo = new String("hello");

// this will return "hello" although toString() 
// from Object only prints the reference
System.out.println( foo.toString() );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文