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

查看:37
本文介绍了理解 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类中显式定义一个方法为抽象的目的是什么?
  • 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).

不过不用太担心.

抽象类 vs 接口

抽象类允许您定义基本功能,而留下未定义的部分.接口不允许您实现任何东西.除了在每种情况下真正改变的部分之外,您可以对所有内容进行编程.所以当你需要的时候,你继承并实现缺失的部分.

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 的方法.没关系.如果您想让它不可修改,您可以将其声明为 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 以另一种更有效的方式实现它可能是有意义的(想象一下,比较成本很高).但是在某些情况下,您想要提供已经实现的基本功能(然后是抽象类比接口更好的时候),或者当您想要强制实现某些功能时(然后是 final 关键字显示有用的时候).

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天全站免登陆