📄 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 + -