?? test.java
字號:
// Test.java
// point, circle, cylinder 擴展層次主程序
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
public class Test {
public static void main( String args[] )
{
Point point = new Point( 7, 11 );
Circle circle = new Circle( 3.5, 22, 8 );
Cylinder cylinder = new Cylinder( 10, 3.3, 10, 10 );
Shape arrayOfShapes[];
arrayOfShapes = new Shape[ 3 ];
// Point 子類 在arrayOfShapes[0]實現
arrayOfShapes[ 0 ] = point;
// Circle子類 在arrayOfShapes[1]實現
arrayOfShapes[ 1 ] = circle;
// Cylinder子類 在arrayOfShapes[2]實現
arrayOfShapes[ 2 ] = cylinder;
String output =
point.getName() + ": " + point.toString() + "\n" +
circle.getName() + ": " + circle.toString() + "\n" +
cylinder.getName() + ": " + cylinder.toString();
DecimalFormat precision2 = new DecimalFormat( "0.00" );
// 循環輸出下轉形狀并打印名字
// 各實現的面積、體積
for ( int i = 0; i < arrayOfShapes.length; i++ ) {
output += "\n\n" +
arrayOfShapes[ i ].getName() + ": " +
arrayOfShapes[ i ].toString() +
"\nArea = " +
precision2.format( arrayOfShapes[ i ].area() ) +
"\nVolume = " +
precision2.format( arrayOfShapes[ i ].volume() );
}
JOptionPane.showMessageDialog( null, output,
"Demonstrating Polymorphism",
JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 );
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -