第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Java - 從方法返回不同的類(lèi)并使用類(lèi)的特定函數(shù)

Java - 從方法返回不同的類(lèi)并使用類(lèi)的特定函數(shù)

慕尼黑8549860 2023-09-06 16:38:44
我有 2 節(jié)課。例如;矩形和圓形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() {    }}我在課外有這個(gè)方法;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());}但我想在沒(méi)有類(lèi)轉(zhuǎn)換的情況下在一行上完成它,例如:public void PrintArea(Object object) {    System.out.print(object.getArea());}是否可以?謝謝...(圓形和矩形只是示例。我在大項(xiàng)目中有不同且詳細(xì)的類(lèi)。而且我無(wú)法更改這個(gè)類(lèi)結(jié)構(gòu))
查看完整描述

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


查看完整回答
反對(duì) 回復(fù) 2023-09-06
?
繁星淼淼

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

}


查看完整回答
反對(duì) 回復(fù) 2023-09-06
?
幕布斯6054654

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


    }


查看完整回答
反對(duì) 回復(fù) 2023-09-06
?
慕斯王

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í)使用它。


查看完整回答
反對(duì) 回復(fù) 2023-09-06
  • 4 回答
  • 0 關(guān)注
  • 178 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)