4 回答

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超9個(gè)贊
您可以使用多態(tài)性在運(yùn)行時(shí)調(diào)用正確實(shí)例的方法。為此,您可以創(chuàng)建一個(gè)Shape具有該printArea()方法的接口。該方法將由具體類(lèi)實(shí)現(xiàn)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());
}
}
然后會(huì)有一個(gè)調(diào)度程序類(lèi)Printer,它接受一個(gè)形狀對(duì)象并調(diào)用printArea()它的方法:
class Printer {
public void printArea(Shape shape) {
shape.printArea();
}
}
然后你可以這樣做來(lái)使用 Java 中的動(dòng)態(tài)方法分派概念:
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);
}
編輯
如果您無(wú)法在類(lèi)中添加新方法,那么您可以在Java 8 引入的接口default中添加方法:Shape
interface Shape {
int getArea();
default void printArea(){
System.out.println(this.getArea());
}
}
現(xiàn)在假設(shè)您需要一個(gè)Square實(shí)現(xiàn),并且已定義getArea,那么您不再需要printArea()在類(lèi)中定義方法Square:
class Square implements Shape{
private int side;
public Square(int side) {
this.side = side;
}
public int getArea() {
return side * side;
}
}
這樣,當(dāng)您Shape在現(xiàn)有類(lèi)中實(shí)現(xiàn)新接口時(shí),就不會(huì)破壞現(xiàn)有的實(shí)現(xiàn)。

TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超11個(gè)贊
您需要編寫(xiě)一個(gè)帶有 getArea() 方法的接口。并且您應(yīng)該讓 Rectangle 和 Circle 實(shí)現(xiàn)該接口。
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貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超7個(gè)贊
您可以在 printArea 方法中使用反射,但不推薦這樣做。使用接口或從抽象基類(lèi)擴(kuò)展
一種方法如下。
public void PrintArea(Object object) throws Exception {
Method method = object.getClass().getMethod("getArea");
System.out.println(method.invoke(object));
}

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超2個(gè)贊
假設(shè)您有兩個(gè)類(lèi):矩形和圓形。
這兩個(gè)類(lèi),你不能改變,所以你不能讓它們實(shí)現(xiàn)一個(gè)接口。
但是,您可以在本地?cái)U(kuò)展它們,并讓這些子類(lèi)實(shí)現(xiàn)接口。
public interface Shape {
int getArea();
}
并且,您創(chuàng)建兩個(gè)新類(lèi):
public class MyRectangle extends Rectangle implements Shape {
}
public class MyCircle extends Rectangle implements Shape {
}
由于這些類(lèi)繼承了其父類(lèi)的成員,并且這些類(lèi)已經(jīng)提供了 getArea() 的實(shí)現(xiàn),因此您所需要做的就是這些。
然后,將 printArea 方法更改為:
public void printArea(Shape myShape) {
System.out.println(myShape.getArea());
}
為了使其工作,您需要使用實(shí)例化新類(lèi)而不是舊類(lèi),或者需要添加映射器,這可能類(lèi)似于:
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);
}
并在必要時(shí)使用它。
添加回答
舉報(bào)