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

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

问题描述

希望任何人都可以提供帮助,我正在学习 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,只知道它是someem> 类型的 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 没有定义 areadoSomething 就不会编译.我们必须声明一个接受 TriangledoSomething 和另一个接受 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天全站免登陆