繁体   English   中英

对象的区域 - 抽象类 - Java

[英]Area of an object- Abstract Class - Java

我正在使用Java:The Complete Reference这本书学习Java。 目前我正在研究抽象类的主题。

请注意:stackoverflow上有类似的问题。 我搜查了他们,但我无法理解这个概念。

如果我运行以下程序,它会产生正确的输出,但我不明白这个概念。

这里对Abstract类的引用变量有什么需求。 我可以在没有抽象类的引用变量的情况下获得输出。

首先,我运行以下程序并获得所需的输出。

abstract class Figure {
  double dim1;
  double dim2; 

  Figure(double a, double b) {
    dim1 = a;
    dim2 = b;
  }

  // area is now an an abstract method 
  abstract double area();
}

class Rectangle extends Figure {
  Rectangle(double a, double b) {
    super(a, b);
  }

  // override area for rectangle
  double area() {
    System.out.println("Inside Area for Rectangle.");
    return dim1 * dim2;
  }
}

class Triangle extends Figure {
  Triangle(double a, double b) {
    super(a, b);
  }

  // override area for right triangle
  double area() {
    System.out.println("Inside Area for Triangle.");
    return dim1 * dim2 / 2;
  }
}

class AbstractAreas {
  public static void main(String args[]) {

    Rectangle r = new Rectangle(9, 5);
    Triangle t = new Triangle(10, 8);

    Figure figref; 

    figref = r;
    System.out.println("Area is " + figref.area());

    figref = t;
    System.out.println("Area is " + figref.area());
  }
}

我尝试了下面的代码,而没有创建/使用抽象类引用。

class AbstractAreas {
  public static void main(String args[]) {

    Rectangle r = new Rectangle(9, 5);
    Triangle t = new Triangle(10, 8);

    // Figure figref; 

    // figref = r;
    System.out.println("Area is " + r.area());

    // figref = t;
    System.out.println("Area is " + t.area());
  }
}

它也提供了与第一个程序相同的输出。

任何人都可以解释使用抽象类引用调用“区域方法”的需要。

它只是作为一个演示 ,即使您将变量声明为抽象类型,您也可以为其分配一个具体子类的实例,并从子类中获取重写行为。

实际使用示例是,如果您需要它们的集合

List<Figure> figureList = new ArrayList<Figure>();
figureList.add(new Rectangle(9, 5));
figureList.add(new Triangle(10, 8));

for (Figure f : figureList) {
    System.out.println(f.area());
}

或者,如果要将Figure任何子类传递给使用area()

public void printArea(Figure f) {
    System.out.println("Area is: " + f.area());
}
...
myObject.printArea(new Rectangle(9, 5));
myObject.printArea(new Triangle(10, 8));

在Abstract类中,您可以定义抽象方法和非抽象方法。 但是,Abstract类的第一个具体子类必须实现那些抽象方法。 您不能创建抽象类的实例,并且必须将它们子类化为某个具体类。

另请注意,JLS声明如果抽象类具有所有抽象方法,则最好使用接口。

Can anyone please explain what is the need of calling "area method" using
abstract class reference.

概念与继承相同。 我们使用抽象类来避免重复。

What is the need of reference variable of an Abstract class here. I can get the
output without the reference variable of an abstract class.

抽象类用作参考,因为您可以在此处利用多态性 如果在运行时调用引用变量上的area() ,它将根据实际实例类型调用相应的TraingleRectangle实现。

嘿,你在这里使用UPCAST​​ING的概念,也称为子对象的父引用 您编写的上述代码程序正在执行UPCAST​​ING 让我们来看看什么是UPCAST​​ING

向上转换是一种使用父类引用来引用子类对象的机制

每当使用Upcasting时,您只能访问父类成员 (包括变量和方法)和父类重写方法

在您的示例中,方法area()已在子类RectangleTriangle中被覆盖,因此可以使用父引用figref访问它们。

UPCAST​​ING的一个优点是我们可以实现动态方法调度动态多态 ,这在编写具有复杂类层次结构的复杂应用程序时非常必要。

因为你提到你正在使用完整参考检查方法覆盖之后的动态方法调度部分。

希望这个答案有助于:)

是的,你可以得到相同的答案但是总是首选使用抽象类或接口来调用任何api。 area()是一个在Rectangle或Triangle中重写的api。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM