无法解决传递具体类时的方法

huangapple go评论77阅读模式
英文:

Cannot resolve method when passing a concrete class

问题

为什么这是无效的语法?
我认为多态性会在这个例子中发生。
形状只能在运行时知道,应该分派给正确的方法。
为什么这行不通?

public class Test {

    public interface Shape {

    }

    static class Circle implements Shape {

    }

    static class Square implements Shape {

    }

    public void foo(Shape shape) {
        Shape s = new Circle();
        bar(s);
    }

    public void bar(Circle c) {

    }

    public void bar(Square s) {

    }
}
英文:

Why is this an invalid syntax?
I thought polymorphism will occur in this example.
The shape could be known only in runtime and it should be dispatched to the correct method.
Why won't this work?

public class Test {

    public interface Shape {

    }

    static class Circle implements Shape {

    }

    static class Square implements Shape {

    }


    public void foo(Shape shape) {
        Shape s = new Circle();
        bar(s);
    }

    public void bar(Circle c) {

    }

    public void bar(Square s) {

    }
}

答案1

得分: 2

你应该使用 Circle 引用而不是 Shape:

public void foo(Shape shape) {
    Circle s = new Circle();
    bar(s);
}

在方法 foo 中,变量 s 是一个 Shape 类。而你的 bar 方法只接受 Circle 或 Square 之一。

如果你创建一个接收 Shape 参数的方法,你的代码就能工作:

public void bar(Shape c) {

}

但因为这个方法不存在,编译器会抛出异常。

对于上面的方法,你可以传递 Shape、Circle 和 Square 的引用。但这样你将会为所有形状只有一个方法。

如果你只在运行时知道类的话,你需要进行一些类型转换:

public void foo(Shape shape) {
   Shape s = new Circle();

   if(Circle.class.isInstance(s)){
      bar(Circle.class.cast(s));
   } else if (Square.class.isInstance(s)){
      bar(Square.class.cast(s));
   }
}
英文:

You should be using Circle reference instead of Shape:

public void foo(Shape shape) {
    Circle s = new Circle();
    bar(s);
}

In method foo variable s is a Shape class. And your bar methods expect only one of two : Circle or Square.

Your code would work if you create a method that receives a Shape

public void bar(Shape c) {

}

But because it is missing, the compiler throws an exception.

To above method you could pass references of: Shape, Circle and Square. But then you will end up with a single method for all shapes.

If you know the class only in the runtime, then you need to do some casting:

public void foo(Shape shape) {
   Shape s = new Circle();

   if(Circle.class.isInstance(s)){
      bar(Circle.class.cast(s));
   } else if (Square.class.isInstance(s)){
      bar(Square.class.cast(s));
   }
}

答案2

得分: 0

是的,你说得对,多态性将会发生,但是是在运行时。在编译时它只知道声明的类型。所以方法参数中应该是相同的类型。

你可以像下面这样修改方法:

public void bar(Shape s) {

  if(s instanceof Circle){

  }else if(s instanceof Square) {

  } 
}
英文:

Yes you are correct polymorphism will occur but at run-time. At compile time it only knows the declaration type. So there should be the same type in the method argument.

You can change the method like below:

public void bar(Shape s) {

  if(s instanceof Circle){

  }else if(s instanceof Square) {

  } 
}

huangapple
  • 本文由 发表于 2020年8月31日 13:10:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63665110.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定