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

查看:24
本文介绍了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 获得类似 new 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!"); }
}

你不能这样做,因为AbstractClassfinalMethod()的实现被标记为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天全站免登陆