亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Java - 從方法返回不同的類并使用類的特定函數

Java - 從方法返回不同的類并使用類的特定函數

慕尼黑8549860 2023-09-06 16:38:44
我有 2 節課。例如;矩形和圓形public class Rectangle {    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 class Circle {    private int radius;    public Circle(int radius) {        this.radius = radius;    }    public int getArea() {    }}我在課外有這個方法;public void PrintArea(Object object) {    if(object instanceof Circle)        System.out.print(((Circle)object).getArea());    else if(object instanceof Rectangle)        System.out.print(((Rectangle)object).getArea());}但我想在沒有類轉換的情況下在一行上完成它,例如:public void PrintArea(Object object) {    System.out.print(object.getArea());}是否可以?謝謝...(圓形和矩形只是示例。我在大項目中有不同且詳細的類。而且我無法更改這個類結構)
查看完整描述

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在現有類中實現新接口時,就不會破壞現有的實現。


查看完整回答
反對 回復 2023-09-06
?
繁星淼淼

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();

}


查看完整回答
反對 回復 2023-09-06
?
幕布斯6054654

TA貢獻1876條經驗 獲得超7個贊

您可以在 printArea 方法中使用反射,但不推薦這樣做。使用接口或從抽象基類擴展


一種方法如下。


public void PrintArea(Object object) throws Exception {

        Method method = object.getClass().getMethod("getArea");

        System.out.println(method.invoke(object));


    }


查看完整回答
反對 回復 2023-09-06
?
慕斯王

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);

}

并在必要時使用它。


查看完整回答
反對 回復 2023-09-06
  • 4 回答
  • 0 關注
  • 208 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號