使用基类引用的派生类对象重载方法 - java

发布于 2024-12-04 22:45:30 字数 1210 浏览 0 评论 0原文

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 技术交流群。

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

发布评论

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

评论(4

抹茶夏天i‖ 2024-12-11 22:45:30

问题是您的方法采用 childAchildB 对象作为参数,并且您给它一个 base 对象,而不是

更改方法签名以采用像这样将基类作为参数可以解决问题,但是您会失去多态性

function(base a,base b){}

,您可以做的是将变量 ab 更改为

childA a = new childA();
childB b = new childB();

Maybe you should see the method如果您想坚持使用 base 而不是 childA 或,则重写而不是重载孩子 B.

您在基类中定义一个方法

someMethod(){
  //do something
}

,然后在子类中重写它,就像

@override
someMethod(){
  //do something specific to childA
}

当您这样做

base a = new childA();

并调用

a.doSomething();

它时将调用 childA 中的重写方法

The problem is that your method takes a childA or childB object as argument and you give it a base object instead

change the method signature to take the base class as argument like so would fix the problem but you lose the polymorphism

function(base a,base b){}

what you can do instead is change the variables a and b to

childA a = new childA();
childB b = new childB();

Maybe 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

someMethod(){
  //do something
}

and then override it in your child classes like

@override
someMethod(){
  //do something specific to childA
}

then when you do

base a = new childA();

and call

a.doSomething();

it will call the overrided method in childA

风吹雪碎 2024-12-11 22:45:30

以下对我有用:

class user9
{
    static class base
    {
    }

    static class childA extends base
    {
    }

    static class childB extends base
    {
    }

    static void function ( childA a , childB b )
    {
    }

    static void function ( childA a1  , childA a2 )
    {
    }

    public static void main ( String [ ] args )
    {
        childA a = new childA ( ) ;
        childB b = new childB (  ) ;
        function ( a , b ) ;
        function ( a , a ) ;
    }
}

The following worked for me:

class user9
{
    static class base
    {
    }

    static class childA extends base
    {
    }

    static class childB extends base
    {
    }

    static void function ( childA a , childB b )
    {
    }

    static void function ( childA a1  , childA a2 )
    {
    }

    public static void main ( String [ ] args )
    {
        childA a = new childA ( ) ;
        childB b = new childB (  ) ;
        function ( a , b ) ;
        function ( a , a ) ;
    }
}
〆凄凉。 2024-12-11 22:45:30
abstract class Base{};

class ChildA extends Base{};
class ChildB extends Base{};

public class JavaTest {

    public static void function( ChildA a, ChildA a2 ) {
        //do something
    }

    public static void function( ChildA a, ChildB b ) {
        //do something else
    }

    public static void function( Base a, Base a2 ) {
        //do something
    }

    public static void main(String[] args) {
        function( new ChildA(), new ChildA() );
        function( new ChildA(), new ChildB() );
        function( new ChildB(), new ChildA() ); //Uses function(Base, Base)
    }
}

下面的示例代码使用您指定的 2 个重载以及 @Ben 建议的泛型重载。正如我的评论中提到的,如果您想使用特定的 ChildA/B 函数,则在使用通用重载时必须向下转换。

abstract class Base{};

class ChildA extends Base{};
class ChildB extends Base{};

public class JavaTest {

    public static void function( ChildA a, ChildA a2 ) {
        //do something
    }

    public static void function( ChildA a, ChildB b ) {
        //do something else
    }

    public static void function( Base a, Base a2 ) {
        //do something
    }

    public static void main(String[] args) {
        function( new ChildA(), new ChildA() );
        function( new ChildA(), new ChildB() );
        function( new ChildB(), new ChildA() ); //Uses function(Base, Base)
    }
}

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.

淡淡的优雅 2024-12-11 22:45:30

您可能对访问者模式感兴趣。

You may be interested in the Visitor pattern.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文