理解Java中抽象类的目的 [英] Understanding the purpose of Abstract Classes in Java

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

问题描述

假设我有两个类,A和B.A类被定义为抽象,而B扩展了这个抽象类,最后我测试了结果,两个类都是同一个包的一部分。

Suppose I have two Classes, A and B. The A class is defined as abstract, while B extends this abstract class, and finally i test the result and both classes are part of same package.

public abstract class A {

    protected abstract void method1(); 

    protected void method2() { 
        System.out.println("This is Class A's method"); 
    } 
}

public class B extends A {

    @Override
    protected void method1() {
        System.out.println("This is B's implementaiton of A's method");
    } 
}  

现在当我测试它们时:

B b = new B();
b.method1();
b.method2();  

我得到预期的产出:

This is B's implementaiton of A's method
This is Class A's method

问题:


  • @Override 关键字,因为如果省略它,
    仍然可以正常工作。

  • 如果我没有实现抽象方法,我会收到编译错误。那么实现接口有什么不同呢?

  • 另外,我也可以在B中实现 method2()。然后输出更改为B中使用的内容。这是否也会覆盖父类方法?那么在A类中明确定义方法为 abstract 的目的是什么?

  • What is the purpose of @Override keyword, because if I omit it, it still works the same.
  • If I don't implement the abstract method, I get a compilation error. So what is the difference from implementing an interface?
  • Also, I can implement method2() in B as well. Then the output changes to what is use in B. Isn't this also overriding the parent class method? Then what is the purpose of explicitly defining a method as abstract in Class A?

推荐答案

@Override

@Override在Java 5中引入(并在Java 6中进行了一些扩展)。它只是提供信息。它说我想要覆盖已经存在于父类或接口中的东西。

@Override was introduced in Java 5 (and extended a little bit in Java 6). It's only informative. It says "I'm suppose to override something that already exist in parent class or interface.

IDE就像Eclipse一样可以警告你,如果没有这样的父方法(例如如果你错误地写了这个名字)。在这种情况下你的方法不会被调用(因为拼写错误)。

IDE's like Eclipse can warn you in case there's no such parent method (by example if you mispell the name). In that case your method will not be invoked (because of the mispelling).

但是不要过于担心它。

抽象类与接口

抽象类允许您定义基本功能而不定义接口不允许你实现任何东西。你可以编写除了每种情况下真正改变的部分之外的所有部分。所以当你需要它时,你继承并实现缺少的部分。

An abstract class allows you define a basic functionality leaving undefined parts. An interface doesn't allow you to implement anything. You can program everything except the part that really changes in each case. So when you need it, you inherit and implement the missing part.

覆盖两种方法

是的。在Java中,您可以覆盖在父类中声明为final的所有方法。它是好的。如果你想让它不可修改你可以声明它是最终的。例如,如果你想声明一个订购你可以:

Yes. In Java you can override all methods not explicity declared as final in parent class. It's ok. If you want to make it unmodifiable you can declare it final. By example, if you want to declare an ordering you could:

public abstract class Ordering<X>
{
abstract boolean isLower(X a, X b);
abstract boolean isEquals(X a, X b);
   final boolean isGreater(X a, X b) {
      return !isLower(a, b) && !isEquals(a, b);
   }
}

当然可以覆盖isGreater实施它是另一种更有效的方式(想象它比较昂贵)。但是有些情况下你想要提供基本的已经实现的功能(当抽象类比接口更好时),或者当你想强制实现某些实现时(当最终关键字显示有用时)。

Of course it may have sense to override isGreater to implement it another more efficient way (imagine it's costly to compare). But there are scenarios when you want to provide basic already implemented functionality (and then's when abstract classes are better than interfaces) or when you want to force some implementation (then's when final keyword show useful).

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

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