chap4-11.txt

来自「清华大学出版社经典教材系列」· 文本 代码 · 共 30 行

TXT
30
字号
// 程序4-11
class point{  // 默认情况下父类是Object类
    int x, y;  

    point( ){ this(0,0);}  
    point(int x, int y){  this.x=x; this.y=y;   }    
    double area( ){ return 0;}  // point类定义了area方法
}

class circle extends point{     // circle类的父类是point类
    int radius;
   
    circle(int r, int x, int y){  super(x, y);    radius=r; }      
    double area( ){  		// 子类覆盖了父类的area方法 
        return   Math.PI*radius*radius ;  
    } 
}

public class dynamicalCall {
    public static void main(String args[ ]) {
        point p[ ]={new point(2,2), new circle(1,1,1) };	// 注意这一行
        
        for(int i=0;i<p.length;i++){            
            System.out.print("类名: "+p[i].getClass( ).getName( ));
            System.out.print("  父类: "+p[i].getClass( ).getsuperclass( ));
            System.out.println("  面积: "+ p[i].area( ));
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?