4 回答

TA貢獻1827條經驗 獲得超9個贊
您可以使用多態性在運行時調用正確實例的方法。為此,您可以創建一個Shape具有該printArea()方法的接口。該方法將由具體類實現Rectangle,并Circle定義其特定行為:
interface Shape {
void printArea();
}
class Rectangle implements Shape {
private int width;
private int height;
public Rectangle(int width, int height) {
this.width = width;
this.height = height;
}
public int getArea() {
return width * height;
}
public void printArea() {
System.out.println(this.getArea());
}
}
class Circle implements Shape {
private int radius;
public Circle(int radius) {
this.radius = radius;
}
public int getArea() {
return radius * radius * 3;
}
public void printArea() {
System.out.println(this.getArea());
}
}
然后會有一個調度程序類Printer,它接受一個形狀對象并調用printArea()它的方法:
class Printer {
public void printArea(Shape shape) {
shape.printArea();
}
}
然后你可以這樣做來使用 Java 中的動態方法分派概念:
public static void main(String[] args) {
Shape rectangle = new Rectangle(10, 20);
rectangle.printArea();
Shape circle = new Circle(20);
circle.printArea();
Printer printer = new Printer();
printer.printArea(circle);
printer.printArea(rectangle);
}
編輯
如果您無法在類中添加新方法,那么您可以在Java 8 引入的接口default中添加方法:Shape
interface Shape {
int getArea();
default void printArea(){
System.out.println(this.getArea());
}
}
現在假設您需要一個Square實現,并且已定義getArea,那么您不再需要printArea()在類中定義方法Square:
class Square implements Shape{
private int side;
public Square(int side) {
this.side = side;
}
public int getArea() {
return side * side;
}
}
這樣,當您Shape在現有類中實現新接口時,就不會破壞現有的實現。

TA貢獻1775條經驗 獲得超11個贊
您需要編寫一個帶有 getArea() 方法的接口。并且您應該讓 Rectangle 和 Circle 實現該接口。
public interface Shape {
int getArea();
}
public class Rectangle implements Shape {
private int width;
private int height;
public Rectangle(int width,int height) {
this.width = width;
this.height = height;
}
@Override
public int getArea() {
return width * height;
}
}
public class Circle implements Shape {
private int radius;
public Circle(int radius) {
this.radius = radius;
}
@Override
public int getArea() {
return radius * radius * 3;
}
}
和..
public void PrintArea(Object object) {
if(object instanceof Shape)
System.out.print((Shape)object.getArea());
}
//or..
public void PrintArea(Shape object) {
object.getArea();
}

TA貢獻1876條經驗 獲得超7個贊
您可以在 printArea 方法中使用反射,但不推薦這樣做。使用接口或從抽象基類擴展
一種方法如下。
public void PrintArea(Object object) throws Exception {
Method method = object.getClass().getMethod("getArea");
System.out.println(method.invoke(object));
}

TA貢獻1864條經驗 獲得超2個贊
假設您有兩個類:矩形和圓形。
這兩個類,你不能改變,所以你不能讓它們實現一個接口。
但是,您可以在本地擴展它們,并讓這些子類實現接口。
public interface Shape {
int getArea();
}
并且,您創建兩個新類:
public class MyRectangle extends Rectangle implements Shape {
}
public class MyCircle extends Rectangle implements Shape {
}
由于這些類繼承了其父類的成員,并且這些類已經提供了 getArea() 的實現,因此您所需要做的就是這些。
然后,將 printArea 方法更改為:
public void printArea(Shape myShape) {
System.out.println(myShape.getArea());
}
為了使其工作,您需要使用實例化新類而不是舊類,或者需要添加映射器,這可能類似于:
public static Shape createMyShapes(Object o) throws TechnicalException {
if ( !(o instanceof Circle || o instanceof Rectangle) )
throw new TechnicalException("Wrong type");
if ( o instanceof Circle )
return new MyCircle(o);
return new MyRectangle(o);
}
并在必要時使用它。
添加回答
舉報