我在網上找到了以下代碼,但不知道如何在 IDE 上運行它,因為我是 Java 新手,請您幫忙。
class Figure {
void display() { System.out.print("Figure "); }
}
class Rectangle extends Figure {
void display() { System.out.print("Rectangle "); }
void display(String s){ System.out.print(s); }
}
class Box extends Figure {
void display() { System.out.print("Box "); }
void display(String s){ System.out.print(s); }
}
Figure f = new Figure();
Rectangle r = new Rectangle();
Box b = new Box();
f = r;
((Figure) f).display();
f = (Figure) b;
f.display();
我嘗試創建一個類并復制粘貼代碼,但無法產生任何輸出。超級卡住,我還是個新手。手工做我認為它應該輸出矩形框。
uj5u.com熱心網友回復:
圖.java:
class Figure {
void display() { System.out.print("Figure "); }
}
矩形.java:
class Rectangle extends Figure {
void display() { System.out.print("Rectangle "); }
void display(String s){ System.out.print(s); }
}
盒子.java:
class Box extends Figure {
void display() { System.out.print("Box "); }
void display(String s){ System.out.print(s); }
}
其余代碼應該在main方法中,您可以將main方法放入上述任何一個 .java 檔案中。我放進去了Figure.java,請你試試。

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/346830.html
