java吧 关注:1,297,435贴子:12,832,419
  • 0回复贴,共1

java类继承学习

只看楼主收藏回复

class Circle {
private double radius;
public Circle() {
this.radius = 0;
}
public Circle(double radius) {
this.radius = radius;
}
public double getArea() {
return Math.PI * Math.pow(radius, 2);
}
public double getPerimenter() {
return Math.PI * 2 * radius;
}
public void show() {
System.out.println("圆的半径" + radius);
System.out.println("圆的周长" + getPerimenter());
System.out.println("圆的面积" + getArea());
}
}
class Cylinder extends Circle {
private double hight;
public Cylinder(double r, double h) {
super(r);
this.hight = h;
}
public double getVolume() {
return getArea() * hight;
}
public void showVolume() {
System.out.println("圆柱的体积" + getVolume());
}
}
public class H1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Cylinder b = new Cylinder(1, 1);
b.show();
b.showVolume();
}
}


IP属地:辽宁1楼2015-05-19 21:42回复