Java中的抽象类 [英] Abstract class in Java

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

问题描述

Java中的抽象类是什么?

What is an "abstract class" in Java?

推荐答案

抽象类是一个无法实例化的类。通过创建可以实例化的继承子类来使用抽象类。抽象类为继承子类做了一些事情:

An abstract class is a class which cannot be instantiated. An abstract class is used by creating an inheriting subclass that can be instantiated. An abstract class does a few things for the inheriting subclass:


  1. 定义继承子类可以使用的方法。

  2. 定义继承子类必须实现的抽象方法。

  3. 提供一个通用接口,允许子类与所有其他子类互换。

以下是一个例子:

abstract public class AbstractClass
{
    abstract public void abstractMethod();
    public void implementedMethod() { System.out.print("implementedMethod()"); }
    final public void finalMethod() { System.out.print("finalMethod()"); }
}

请注意,abstractMethod()没有任何方法体。因此,您无法执行以下操作:

Notice that "abstractMethod()" doesn't have any method body. Because of this, you can't do the following:

public class ImplementingClass extends AbstractClass
{
    // ERROR!
}

没有方法可以实现 abstractMethod()!因此,JVM无法知道当它获得类似新的ImplementingClass()。abstractMethod()时应该做什么。

There's no method that implements abstractMethod()! So there's no way for the JVM to know what it's supposed to do when it gets something like new ImplementingClass().abstractMethod().

这是一个正确的 ImplementingClass

public class ImplementingClass extends AbstractClass
{
    public void abstractMethod() { System.out.print("abstractMethod()"); }
}

请注意,您不必定义 implementedMethod() finalMethod()。它们已经由 AbstractClass 定义。

Notice that you don't have to define implementedMethod() or finalMethod(). They were already defined by AbstractClass.

这是另一个正确的 ImplementingClass

public class ImplementingClass extends AbstractClass
{
    public void abstractMethod() { System.out.print("abstractMethod()"); }
    public void implementedMethod() { System.out.print("Overridden!"); }
}

在这种情况下,您已覆盖 implementedMethod( )

In this case, you have overridden implementedMethod().

但是,由于 final 关键字,以下是不可能的。

However, because of the final keyword, the following is not possible.

public class ImplementingClass extends AbstractClass
{
    public void abstractMethod() { System.out.print("abstractMethod()"); }
    public void implementedMethod() { System.out.print("Overridden!"); }
    public void finalMethod() { System.out.print("ERROR!"); }
}

你不能这样做因为 in AbstractClass 被标记为 finalMethod()的最终实现:永远不会允许其他任何实现。

You can't do this because the implementation of finalMethod() in AbstractClass is marked as the final implementation of finalMethod(): no other implementations will be allowed, ever.

现在你可以两次实现一个抽象类:

Now you can also implement an abstract class twice:

public class ImplementingClass extends AbstractClass
{
    public void abstractMethod() { System.out.print("abstractMethod()"); }
    public void implementedMethod() { System.out.print("Overridden!"); }
}

// In a separate file.
public class SecondImplementingClass extends AbstractClass
{
    public void abstractMethod() { System.out.print("second abstractMethod()"); }
}

现在可以在某处编写另一种方法。

Now somewhere you could write another method.

public tryItOut()
{
    ImplementingClass a = new ImplementingClass();
    AbstractClass b = new ImplementingClass();

    a.abstractMethod();    // prints "abstractMethod()"
    a.implementedMethod(); // prints "Overridden!"     <-- same
    a.finalMethod();       // prints "finalMethod()"

    b.abstractMethod();    // prints "abstractMethod()"
    b.implementedMethod(); // prints "Overridden!"     <-- same
    b.finalMethod();       // prints "finalMethod()"

    SecondImplementingClass c = new SecondImplementingClass();
    AbstractClass d = new SecondImplementingClass();

    c.abstractMethod();    // prints "second abstractMethod()"
    c.implementedMethod(); // prints "implementedMethod()"
    c.finalMethod();       // prints "finalMethod()"

    d.abstractMethod();    // prints "second abstractMethod()"
    d.implementedMethod(); // prints "implementedMethod()"
    d.finalMethod();       // prints "finalMethod()"
}

请注意,即使我们声明 b 一个 AbstractClass 类型,显示Overriden!。这是因为我们实例化的对象实际上是一个 ImplementingClass ,当然会覆盖其 implementedMethod()。 (您可能已经看到这被称为多态。)

Notice that even though we declared b an AbstractClass type, it displays "Overriden!". This is because the object we instantiated was actually an ImplementingClass, whose implementedMethod() is of course overridden. (You may have seen this referred to as polymorphism.)

如果我们希望访问特定于某个子类的成员,我们必须首先转发到该子类:

If we wish to access a member specific to a particular subclass, we must cast down to that subclass first:

// Say ImplementingClass also contains uniqueMethod()
// To access it, we use a cast to tell the runtime which type the object is
AbstractClass b = new ImplementingClass();
((ImplementingClass)b).uniqueMethod();

最后,您无法执行以下操作:

Lastly, you cannot do the following:

public class ImplementingClass extends AbstractClass, SomeOtherAbstractClass
{
    ... // implementation
}

一次只能扩展一个类。如果需要扩展多个类,则必须是接口。你可以这样做:

Only one class can be extended at a time. If you need to extend multiple classes, they have to be interfaces. You can do this:

public class ImplementingClass extends AbstractClass implements InterfaceA, InterfaceB
{
    ... // implementation
}

这是一个示例界面:

interface InterfaceA
{
    void interfaceMethod();
}

这基本上与以下相同:

abstract public class InterfaceA
{
    abstract public void interfaceMethod();
}

唯一的区别是第二种方式不让编译器知道它实际上是一个界面。如果您希望人们只实现您的界面而不是其他人,那么这将非常有用。但是,作为一般的初学者经验法则,如果你的抽象类只有抽象方法,你应该把它变成一个接口。

The only difference is that the second way doesn't let the compiler know that it's actually an interface. This can be useful if you want people to only implement your interface and no others. However, as a general beginner rule of thumb, if your abstract class only has abstract methods, you should probably make it an interface.

以下是非法的:

interface InterfaceB
{
    void interfaceMethod() { System.out.print("ERROR!"); }
}

您无法在接口中实现方法。这意味着如果实现两个不同的接口,那些接口中的不同方法就不会发生冲突。由于接口中的所有方法都是抽象的,因此必须实现该方法,并且由于您的方法是继承树中唯一的实现,因此编译器知道它必须使用您的方法。

You cannot implement methods in an interface. This means that if you implement two different interfaces, the different methods in those interfaces can't collide. Since all the methods in an interface are abstract, you have to implement the method, and since your method is the only implementation in the inheritance tree, the compiler knows that it has to use your method.

这篇关于Java中的抽象类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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