简体   繁体   English

对象的区域 - 抽象类 - Java

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

I am learning Java using the book Java: The Complete Reference . 我正在使用Java:The Complete Reference这本书学习Java。 Currently I am working on the topic of abstract classes. 目前我正在研究抽象类的主题。

Please Note: There are similar questions on stackoverflow. 请注意:stackoverflow上有类似的问题。 I searched them but I couldn't understand the concept. 我搜查了他们,但我无法理解这个概念。

If I run the below program, it produces the correct output, but I didn't understand the concept. 如果我运行以下程序,它会产生正确的输出,但我不明白这个概念。

What is the need of reference variable of an Abstract class here. 这里对Abstract类的引用变量有什么需求。 I can get the output without the reference variable of an abstract class. 我可以在没有抽象类的引用变量的情况下获得输出。

First I ran the below program and got the desired output. 首先,我运行以下程序并获得所需的输出。

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

And I tried the below code without creating/using abstract class reference. 我尝试了下面的代码,而没有创建/使用抽象类引用。

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

It also gave the same output as the first program. 它也提供了与第一个程序相同的输出。

Can anyone please explain what is the need of calling "area method" using abstract class reference. 任何人都可以解释使用抽象类引用调用“区域方法”的需要。

It's meant simply as a demonstration that even though you declared the variable as the abstract type, you can assign an instance of a concrete subclass to it and get the overriden behavior from the subclass. 它只是作为一个演示 ,即使您将变量声明为抽象类型,您也可以为其分配一个具体子类的实例,并从子类中获取重写行为。

Practical use example would be if you needed a collection of them: 实际使用示例是,如果您需要它们的集合

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

Or if you want to pass any subclass of Figure to a method that used the 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));

In Abstract classes you can define abstract as well as non abstract methods. 在Abstract类中,您可以定义抽象方法和非抽象方法。 However the 1st concrete subclass of the Abstract class must implement those abstract methods. 但是,Abstract类的第一个具体子类必须实现那些抽象方法。 You cannot create instance of Abstract classes and they must be subclassed to some concrete class. 您不能创建抽象类的实例,并且必须将它们子类化为某个具体类。

Also note JLS states if abstract classes have all abstract method it is better to use interface. 另请注意,JLS声明如果抽象类具有所有抽象方法,则最好使用接口。

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

Concept is same as inheritance. 概念与继承相同。 We use abstract classes to avoid duplicate. 我们使用抽象类来避免重复。

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.

Abstract class is used as a reference because you can take advantage of polymorphism here. 抽象类用作参考,因为您可以在此处利用多态性 If you call area() on the reference variable at runtime it will call the corresponding implementation of Traingle or Rectangle based on the actual instance type. 如果在运行时调用引用变量上的area() ,它将根据实际实例类型调用相应的TraingleRectangle实现。

Hey here you're using a concept of UPCASTING which is also known as Parent reference to the child object . 嘿,你在这里使用UPCAST​​ING的概念,也称为子对象的父引用 And the above code program which u have written is performing UPCASTING . 您编写的上述代码程序正在执行UPCAST​​ING Let us look what is UPCASTING . 让我们来看看什么是UPCAST​​ING

Upcasting is a mechanism of using parent class reference to refer the child class objects . 向上转换是一种使用父类引用来引用子类对象的机制

Whenever you use Upcasting you can access only the parents class members ( both variables and methods) and the overridden methods of parent class . 每当使用Upcasting时,您只能访问父类成员 (包括变量和方法)和父类重写方法

In your example the method area() has been overidden in the child classes Rectangle and Triangle so they can be accessed using parent reference figref . 在您的示例中,方法area()已在子类RectangleTriangle中被覆盖,因此可以使用父引用figref访问它们。

One of the advantage of UPCASTING is we can achieve Dynamic Method Dispatch or Dynamic Polymorphism which is very much necessary in writing complex applications having complex class hierarchies. UPCAST​​ING的一个优点是我们可以实现动态方法调度动态多态 ,这在编写具有复杂类层次结构的复杂应用程序时非常必要。

Since u mentioned you're using Complete reference Check out the section Dynamic Method Dispatch which comes after method overriding . 因为你提到你正在使用完整参考检查方法覆盖之后的动态方法调度部分。

Hope this answer Helps :) 希望这个答案有助于:)

Yes you can get the same answer But it is always preferred to use abstract classes or intefaces to call any api. 是的,你可以得到相同的答案但是总是首选使用抽象类或接口来调用任何api。 area() is an api which is overridden in Rectangle or Triangle. area()是一个在Rectangle或Triangle中重写的api。

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

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