1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| abstract class shape { protected String name; double s, v;
public abstract String getName(); }
class ball extends shape { private double r;
public String getName() { return ball.super.name; }
ball(double r, String name) { this.r = r; this.name = name; s = 4 * Math.PI * Math.pow(r, 2); v = 4 * Math.PI * Math.pow(r, 3) / 3; }
public double getVolume() { return v; } }
public class Main { public static void main(String[] args) { ball c = new ball(2, "MyBall"); System.out.println(c.getName() + "\t" + c.getVolume()); } }
|