使用基类引用的派生类对象重载方法 - java
class base{}
class childA extends base{}
class childB extends base{}
我有两个函数(重载) 像这样:
function(childA,childA){}
function(childA,childB){}
//main program
base a = new childA();
base b = new childB();
function(a,b);
function(a,a); //problem
函数调用显然不会编译。
但是有没有一种方法可以获得相同的效果,而不会使代码过于复杂或每次调用函数时都进行类型检查。
注意:重载函数与类无关。这些类只是数据结构,我宁愿其中没有任何相互依赖的代码。
附言。我浏览了很多涵盖类似问题的主题,但它们似乎没有解决上述问题。抱歉,如果我错过了一些东西(新手,第一篇文章等:))。
编辑:
似乎我的例子有点模糊,我只是想总体理解这个概念,而不仅仅是解决当前问题。看起来很奇怪,上面的代码不起作用,如果起作用的话,这将是一个强大的功能。
好吧,另一个例子,这几乎就是我想要做的。
class Shape{}
class Rectangle extends Shape{
//rectangle data
}
class Circle extends Shape{
//circle data
}
重载函数(另一个类的成员)
boolean checkIntersection(Rectangle r, Circle c){}
boolean checkIntersection(Circle c, Circle c){}
//主程序
Vector<Shape> shapes = new Vector<Shape>();
shapes.add(new Rectangle());
shapes.add(new Circle());
shapes.add(new Circle());
checkIntersection(shapes.get(0),shapes.get(1));
checkIntersection(shapes.get(1),shapes.get(2));
class base{}
class childA extends base{}
class childB extends base{}
I have two functions (overloaded)
like this:
function(childA,childA){}
function(childA,childB){}
//main program
base a = new childA();
base b = new childB();
function(a,b);
function(a,a); //problem
The function calls won't compile obviously.
But is there a way to get the same effect without complicating the code too much or type checking each time the functions are called.
Note: The overloaded functions are independent of the classes. The classes are just data structures, I would rather not have any interdependent code in them.
PS. I went through quite a few topics covering similar problems, but they don't seem to address the problems mentioned above. Sorry if I missed something, (newbie, first post etc :)).
Edit :
Seems my example was a bit vague, I just wanted to understand the concept in general instead of just a solution to the immediate problem. Seems strange that the above code doesn't work, would have been a powerful feature if it did.
Ok another example, this is pretty much what I'm trying to do.
class Shape{}
class Rectangle extends Shape{
//rectangle data
}
class Circle extends Shape{
//circle data
}
Overloaded functions (members of another class)
boolean checkIntersection(Rectangle r, Circle c){}
boolean checkIntersection(Circle c, Circle c){}
//main program
Vector<Shape> shapes = new Vector<Shape>();
shapes.add(new Rectangle());
shapes.add(new Circle());
shapes.add(new Circle());
checkIntersection(shapes.get(0),shapes.get(1));
checkIntersection(shapes.get(1),shapes.get(2));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题是您的方法采用
childA
或childB
对象作为参数,并且您给它一个base
对象,而不是更改方法签名以采用像这样将基类作为参数可以解决问题,但是您会失去多态性
,您可以做的是将变量
a
和b
更改为Maybe you should see the method如果您想坚持使用 base 而不是 childA 或,则重写而不是重载孩子 B.
您在基类中定义一个方法
,然后在子类中重写它,就像
当您这样做
并调用
它时将调用 childA 中的重写方法
The problem is that your method takes a
childA
orchildB
object as argument and you give it abase
object insteadchange the method signature to take the base class as argument like so would fix the problem but you lose the polymorphism
what you can do instead is change the variables
a
andb
toMaybe you should have a look at method override instead of overload if you want to hold onto using base instead of childA or childB.
you define a method in base
and then override it in your child classes like
then when you do
and call
it will call the overrided method in childA
以下对我有用:
The following worked for me:
下面的示例代码使用您指定的 2 个重载以及 @Ben 建议的泛型重载。正如我的评论中提到的,如果您想使用特定的 ChildA/B 函数,则在使用通用重载时必须向下转换。
Here's sample code that uses the 2 overloads you specify, and the generic-fied overload @Ben suggested. As mentioned in my comment, you have to cast down when using the generic overload if you want to use specific ChildA/B functions.
您可能对访问者模式感兴趣。
You may be interested in the Visitor pattern.