是的,如果a是抽象类,b.c可以同时继承它,就好比一个父亲有多个孩子,而一个孩子只能有一个父亲。
给你写了一个例子,你看看。- Java抽象类应用示例程序Test.java 如下:
- public abstract class Shapes {
- public int x, y;
- public int width, height;
- public Shapes(int x, int y, int width, int height) {
- this.x = x;
- this.y = y;
- this.width = width;
- this.height = height;
- }
- abstract double getArea();
- abstract double getPerimeter();
- }
-
- public class Circle extends Shapes {
- public double r;
- public double getArea() {
- return (r * r * Math.PI);
- }
- public double getPerimeter() {
- return (2 * Math.PI * r);
- }
- public Circle(int x, int y, int width, int heigh) {
- super(x, y, width, heigh);
- r = (double) width / 2.0;
- }
- }
- public class Square extends Shapes {
- public double getArea() {
- return (width * height);
- }
- public double getPerimeter() {
- return (2 * width + 2 * height);
- }
- public Square(int x, int y, int width, int height) {
- super(x, y, width, height);
- }
- }
- public class Triangle extends Shapes {
- public double c;
- public double getArea() {
- return (0.5 * width * height);
- }
- public double getPerimeter() {
- return (width + height + c);
- }
- public Triangle(int x, int y, int base, int height) {
- super(x, y, base, height);
- c = Math.sqrt(width * width + height * height);
- }
- }
- import java.applet.Applet;
- import java.awt.Graphics;
-
- public class Test extends Applet {
- Square Box = new Square(5, 15, 25, 25);
- Triangle tri = new Triangle(5, 50, 8, 4);
- Circle Oval = new Circle(5, 90, 25, 25);
-
- public void paint(Graphics g) {
- g.drawRect(Box.x, Box.y, Box.width, Box.height);
- g.drawString("Box Area:" + Box.getArea(), 50, 35);
- g.drawString("Box Perimeter:" + Box.getPerimeter(), 50, 55);
- g.drawString("tri Area:" + tri.getArea(), 50, 75);
- g.drawString("tri Perimeter:" + tri.getPerimeter(), 50, 95);
- g.drawOval(Oval.x, Oval.y, Oval.width, Oval.height);
- g.drawString("oval Area:" + Oval.getArea(), 50, 115);
- }
- }
复制代码 |