Java:抽象类方法和接口

时间:2015-06-15 16:35:33

标签: java interface abstract

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

这是主要课程:

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);

  }

 }

超类Shape

 abstract class Shape {

  abstract double area();

}

界面Drawable

public interface Drawable {
    public abstract void draw();

}

子类圈

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");
  }

}

子类Rectangle

  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的新手。 感谢。

3 个答案:

答案 0 :(得分:5)

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

要致电draw(),您可以执行以下操作:

不要让每个具体的子类实现Drawable,而是让Shape类实现DrawableShapeabstract,因此无需自行实施draw();子类需要实现该方法。

答案 1 :(得分:0)

问题是您使用Shape类型作为对象。 Java将其视为Shapes,而不是Rectangle或Circles,而Shape类没有draw()方法实现。您可以执行Rectangle obj1 = new Rectangle()Shape obj1 = new Rectangle,然后将其转换为((Rectangle) obj1).draw()(括号是因为强制转换是低优先级操作,您必须先强制​​转换才能访问类的方法和字段特异性的)。

答案 2 :(得分:0)

这里我将定义一个Shape类来显示继承(其他形状如Triangle,Circle和Rectangle如何从Shape类继承)记住Shape类是抽象的,而Triangle例如是一个具体的类,Triangle类是一个Shape类和Shape的子类是父类。我用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();

    }

}