?? inheritancetest.java
字號:
// InheritanceTest.java
// 示范"is a" 關聯
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
public class InheritanceTest {
public static void main( String args[] )
{
Point pointRef, p;
Circle circleRef, c;
String output;
p = new Point( 30, 50 );
c = new Circle( 2.7, 120, 89 );
output = "Point p: " + p.toString() +
"\nCircle c: " + c.toString();
// 使用"is a" 關聯到有關的圓
// with a Point reference
pointRef = c; // 圓賦值到點的定義
output += "\n\nCircle c (via pointRef): " +
pointRef.toString();
// 使用繼承 (實例一個父類定義到子類)
// 子類賦值把一個點定義創建圓對象定義
circleRef = (Circle) pointRef;
output += "\n\nCircle c (via circleRef): " +
circleRef.toString();
DecimalFormat precision2 = new DecimalFormat( "0.00" );
output += "\nArea of c (via circleRef): " +
precision2.format( circleRef.area() );
// 將圓定義為點對象后輸出
if ( p instanceof Circle ) {
circleRef = (Circle) p;
output += "\n\ncast successful";
}
else
output += "\n\np does not refer to a Circle";
JOptionPane.showMessageDialog( null, output,
"Demonstrating the \"is a\" relationship",
JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 );
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -