⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chap4-11.txt

📁 清华大学出版社经典教材系列
💻 TXT
字号:
// 程序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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -