抽象类和方法,为什么呢? [英] Abstract class and methods, Why?

查看:98
本文介绍了抽象类和方法,为什么呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望任何人都可以提供帮助,我正在学习Java,并且与该论坛的其他人一样,我想我也是编程的新手.

Hope anyone can help, i am learning Java and as comparable to anyone else in this forum i guess i am a newbie to programming as well.

我看过一章有关抽象类和方法的文章,但并没有真正完全理解它们的用途以及原因,并认为我会从经验丰富的程序员那里得到解释.

I have come across a chapter on abstract class and methods but don't really fully understand what they are used for and why, and thought i would get an explanation from someone who is an experienced programmer.

在下面的代码示例中,我一直在努力,在本书的帮助下,我不确定的是为什么在类Dims中,当每个子类都使用一个区域时,我是否必须具有抽象的double area()?无论如何,还是从超类中调用area方法,为什么必须要重写一些方法?

Below i have example code i have been working on, with the help from this book, what i am not sure about is why in the class Dims do i have to have abstract double area() when each sub class uses an area method anyway, or is it calling the area method from the super class, why do you have to have methods that override?

    // Using abstract methods and classes
    package Training2;

    abstract class Dims {
    double dim1;
    double dim2;

    Dims(double a, double b) {
        dim1 = a;
        dim2 = b;
    }
    // area is now an abstract method
    abstract double area();
}
class Rectangles extends Dims {
    Rectangles(double a, double b) {
        super(a, b);
    }

    // Override area for Rectangles
    double area() {
        System.out.println("Inside Area for Rectangle.");
        return dim1 * dim2;
    }
} 
class Triangles extends Dims {
    Triangles(double a, double b) {
        super(a, b);
    }

    // Override area for right Triangles
    double area() {
        System.out.println("Inside Area for Triangle.");
        return dim1 * dim2 /2;
    }
}
public class AbstractAreas {
    public static void main(String args[]) {
        // Dims d = new Dims(10, 10); // Illegal now
        Rectangles r = new Rectangles(9, 5);
        Triangles t = new Triangles(10, 8);
        Dims dimref; // This is OK, no object is created

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

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

}

道歉,但我确实需要一些指导.

Apologies for the waffling on but i really need some guidance.

谢谢.

推荐答案

我们这样做是为了说该类具有完整的(ish) API ,但没有完整的实施."除其他外,这意味着您可以执行以下操作:

We do this to say "This class has a complete (ish) API, but it does not have a complete implementation." Among other things, it means that you can do this:

public void doSomething(Dims d) {
    System.out.println("The area is " + d.area());
}

// ...using it somewhere:

doSomething(new Triangle(4.0, 6.0));

doSomething中,即使对对象的引用是Dims而不是Triangle,我们也可以调用area(并且最终会调用Triangle#area),因为它是在Dims API(有时称为合同").只是Dims实现推迟到子类中.这就是为什么您不能创建抽象类的实例的原因. doSomething方法不知道给出的是Triangle还是Rectangle,只是它是某种Dims.

In doSomething, even though the reference we have to the object is a Dims, not a Triangle, we can call area (and we end up calling Triangle#area) because it's defined in the Dims API (sometimes called a "contract"). It's just that Dims defers the implementation to subclasses. Which is why you can't create instances of abstract classes. The doSomething method doesn't have any idea whether what it was given is a Triangle or Rectangle, just that it's some kind of Dims.

如果Dims没有定义area,则doSomething不会编译.我们必须声明一个doSomething接受一个Triangle,另一个声明一个接受Rectangle,依此类推.不能对通用代码中的所有Dims内容进行操作.

Whereas if Dims didn't define area, doSomething wouldn't compile. We'd have to declare a doSomething accepting a Triangle and another one accepting a Rectangle and so on. We couldn't get the benefit of being able to operate on all Dims things in common code.

在Java中,接口和抽象类之间有很多交叉.从根本上讲,您可以将抽象类视为具有部分实现的接口(需要注意的是,一个类可以实现多个接口,但只能从单个抽象类派生).现在,接口可以定义方法的默认"实现,这条线变得更加模糊. (有些人认为接口现在可以具有默认方法,它们是新"抽象类,并且使真正的抽象类作为语言功能已过时,但是

There's a lot of crossover between interfaces and abstract classes in Java. Fundamentally, you can think of an abstract class as an interface with a partial implementation (with the caveat that a class can implement more than one interface, but can only derive from a single abstract class). The line's gotten even blurrier now that interfaces can define "default" implementations of methods. (Some argue that now that interfaces can have default methods, they're the "new" abstract classes and make true abstract classes obsolete as a language feature, but there are still arguments, mostly syntactic, around using abstract classes sometimes.)

这篇关于抽象类和方法,为什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆