abstract class Graph {
// 将图形周长、面积等信息输出到屏幕
abstract void disp();
}
public class Circle extends Graph {
private double radius;// 半径
public Circle() {
this.radius = 0;
}
public Circle(double radius) {
this.radius = radius;
}
@Override
void disp() {
System.out.println("圆的半径是:" + radius);
System.out.println("圆的周长是:" + getZhouChang());
System.out.println("圆的面积是:" + getMianJi());
}
private double getZhouChang() {
return this.radius * 3.14 * 2;
}
private double getMianJi() {
return this.radius * this.radius * 3.14;
}
/**
* @param args
*/
public static void main(String[] args) {
new Circle().disp();
}
}
父类:
public class Graph {
public void disp(){}
}
子类圆 继承父类
public class Circle extends Graph {
private double radius;
public Circle(){}
public Circle(double r){
this.radius=r;
}
public void disp(){
System.out.println("周长为:"+2*3.14*radius+"面积为:"+3.14*(radius*radius)+"半径为:"+radius);
}
测试方法:
public static void main(String[] args) {
System.out.println("请输入半径:");
Scanner in= new Scanner(System.in);
double r=in.nextDouble();
Graph g=new Circle(r);
g.disp();
}
}
class Graph {
}
public class Circle extends Graph {
private double radius;
public Circle() {
radius=0;
}
public Circle(double r) {
radius = r;
}
public void disp() {
System.out.print("半径 : " + radius);
System.out.print("周长 : " + 2*3.14*radius);
System.out.print("面积 : " + 3.14*radius*radius);
}
}