Java:抽象类方法和接口

Java: Abstract classes methods and interface

下面的程序计算实例化的任意数量的形状的总面积。问题是不知道怎么输出接口的draw()方法

这里是主要的class:

public class MyClass1{

public static void main(String[] args) {


    Shape[] shapes= new Shape[3];


    shapes[0]= new Circle(20.0);
    shapes[1]= new Rectangle(25, 63);
    shapes[2]= new Circle(25);

   double total=0;

  for(int i=0; i<shapes.length; i++){

    total+= shapes[i].area();

}

System.out.printf("Total: %.1f", total);

  }

 }

超class形

 abstract class Shape {

  abstract double area();

}

接口Drawable

public interface Drawable {
    public abstract void draw();

}

子class圆

public class Circle extends Shape implements Drawable {

    double radius;

    Circle(double aRadius){

    radius= aRadius;
}


   double area(){

       return Math.PI*radius*radius;
}

    public void draw(){
       System.out.println("This is a circle");
  }

}

子class矩形

  public class Rectangle extends Shape implements Drawable {

   double width, height;

   public Rectangle(double aWidth, double aHeight){

    width= aWidth;
    height= aHeight;

 }

    double area(){

       return width*height;
    }

   public void draw(){

       System.out.println("This is a rectangle.");
    }
  }

我假设要打印出 draw() 方法,代码应该是这样的:

Shape obj= new Rectangle();
Shape obj1= new Circle();

obj.draw();
obj1.draw();

但是没有成功。想知道打印 draw 方法的正确方法以及一些解释,因为我是 Java 的新手。 谢谢。

draw 方法不可用,因为您的数组中有 Shape,而不是 Drawable。理论上你可以有 UndrawableShape 扩展 Shape 但不实现 Drawable,所以编译器不会让你调用可能不存在的 draw() 方法。

要调用draw(),您可以执行以下操作:

不是让每个具体的子class实现Drawable,而是让Shapeclass实现DrawableShapeabstract,所以它不必自己实现 draw(); subclasses 将需要实现该方法。

问题是您将 Shape 类型用作对象。 Java 将它们视为形状,而不是矩形或圆形,并且形状 class 没有 draw() 方法实现。您可以执行 Rectangle obj1 = new Rectangle()Shape obj1 = new Rectangle 然后稍后将其转换为 ((Rectangle) obj1).draw() (括号是因为转换是低优先级操作,您必须先转换才能访问方法和字段是 class具体)。

这里我要定义一个Shapeclass来展示Inheritance(其他的shape如Triangle,Circle和Rectangle是如何继承Shapeclass的)记住Shapeclass是抽象的,例如三角形是具体的 class,三角形 class 是形状 class 的子项,而形状是父项 class。我使用 Java 程序来展示这个。

package inheritancePackage;

/* PARENT CLASS – abstract class */
public abstract class Shape {
    protected double area;

    public abstract void draw();

    public abstract double getArea();


}

package inheritancePackage;

/* CHILD CLASS – concrete class, Circle */

public class Circle extends Shape{
    private double radius;

    public void draw() {
        System.out.println("I am drawing a circle");
    }

    public Circle(){
        radius = 0.0;
    }

    public Circle(double radius) {
        this.radius = radius;
    }

    //getter and setter
    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

    @Override
    public double getArea() {
        return calculateArea();
    }

    //Area of circle
    private double calculateArea() {
        return radius * radius * Math.PI;
    }

    public String toString() {
        return "The radius of the circle is: " + radius + ", and the area is: "
                + getArea();
    }
}

package inheritancePackage;

/* CHILD CLASS – concrete class, Triangle */

public class Triangle extends Shape {
    private double base, high;

    public void draw() {
        System.out.println("I am drawing  a Triangle");
    }

    public Triangle(){
        base = 0.0;
        high = 0.0;
    }

    public Triangle(double base, double high) {
        this.base = base;
        this.high = high;
    }

    //getters and setters
    public double getBase(){
        return base;    
    }

    public void setBase(double base){
        this.base = base;

    }

    public double getHigh(){
        return high;    
    }

    public void setHigh(double high){
        this.high = high;

    }

    @Override
    public double getArea() {
        return calculateArea();
    }

    //Area of Triangle
    private double calculateArea(){
        area = (base * high) / 2;
        return area;
    }

    public String toString() {
        return "The base of the triangle is: " + base + " and the high is: " + high + ", and the area is: "
                + getArea();
    }

}

package inheritancePackage;

/* CHILD CLASS – concrete class, Rectangle */

public class Rectangle extends Shape {
    private double length, width;

    public void draw() {
        System.out.println("I am drawing  a Rectangle");
    }

    Rectangle(){
        length= 0.0;
        width = 0.0;        
    }

    Rectangle(double length, double width){
        this.length =length;
        this.width = width;
    }

//getters and setters

    public double getLenght() {
        return length;
    }

    public void setLength(double length) {
        this.length = length;
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    @Override
    public double getArea() {
        return calculateArea();
    }

    //Area of Rectangle
    private double calculateArea(){
        return area = width * length;
    }

    public String toString(){
        return "The width of the rectangle is: " + width + " and the length is: " + length + ", "
                + "and the area is: " + getArea();
    }

}

package inheritancePackage;

/* CHILD CLASS – concrete class, Star */

public class Star extends Shape {
    public void draw() {
        System.out.println("I am drawing  a Star");
        System.out.println("No area has been calculated for this kind of shape");
    }

    @Override
    public double getArea() {
        // TODO Auto-generated method stub
        return 0;
    }
}


package inheritancePackage;

/* This class is used to test and pass value through the respective constructors*/

public class TestShape {

    public static void main(String [] args) {

        Circle circle = new Circle(5);
        Rectangle rectangle = new Rectangle(10, 5);
        Triangle triangle = new Triangle(8.0,5.0);
        Star star = new Star();

        circle.draw();
        System.out.println(circle.toString()); 

        rectangle.draw();
        System.out.println(rectangle.toString());

        triangle.draw();
        System.out.println(triangle.toString());

        star.draw();

    }

}