接口和抽象类的理解,Java8做了啥修改?

问题:

谈谈接口和抽象类有什么全部?

知识点补充

  1. Java 8 新增了函数式编程的支持,所以又增加了一个类定义,即所谓的functional interface,简单来说只有一个方法的接口,用FunctionnalInterface annotation来标记。如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@FunctionalInterface
public interface Runnable {
/**
* When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object's
* <code>run</code> method to be called in that separately executing
* thread.
* <p>
* The general contract of the method <code>run</code> is that it may
* take any action whatsoever.
*
* @see java.lang.Thread#run()
*/
public abstract void run();
}

Java8以后,接口中方法也是可以实现的。如Collections中代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* Returns a sequential {@code Stream} with this collection as its source.
*
* <p>This method should be overridden when the {@link #spliterator()}
* method cannot return a spliterator that is {@code IMMUTABLE},
* {@code CONCURRENT}, or <em>late-binding</em>. (See {@link #spliterator()}
* for details.)
*
* @implSpec
* The default implementation creates a sequential {@code Stream} from the
* collection's {@code Spliterator}.
*
* @return a sequential {@code Stream} over the elements in this collection
* @since 1.8
*/
default Stream<E> stream() {
return StreamSupport.stream(spliterator(), false);
}
/**
* Returns a possibly parallel {@code Stream} with this collection as its
* source. It is allowable for this method to return a sequential stream.
*
* <p>This method should be overridden when the {@link #spliterator()}
* method cannot return a spliterator that is {@code IMMUTABLE},
* {@code CONCURRENT}, or <em>late-binding</em>. (See {@link #spliterator()}
* for details.)
*
* @implSpec
* The default implementation creates a parallel {@code Stream} from the
* collection's {@code Spliterator}.
*
* @return a possibly parallel {@code Stream} over the elements in this
* collection
* @since 1.8
*/
default Stream<E> parallelStream() {
return StreamSupport.stream(spliterator(), true);
}
  1. 面向对象通用设计原则(S.0.L.I.D原则)
  • 单一职责,类或者对象只有单一职责
  • 开闭原则,设计要对扩展开发,对修改关闭
  • 里氏替换,凡是可以用父类或者基类的地方,都可以用子类替换。可参考:面向对象设计 里氏替换原则(LSP)
  • 接口分离,对行为进行解耦
  • 依赖反转,实体应该依赖于抽象而不是实现。

    回答问题:

    接口和抽象类是Java面向对象设计的两个基础机制。
    接口是对行为的抽象,它是抽象方法的集合,利用接口可以达到API定义和实现分离的目的。接口、不能实例化;不能包含任何非常量成员,任何field都是隐含着public static final的意义;同时,没有非静态方法的实现,也就是说要么是抽象方法,要么是静态方法。Java标准类库中,定义了非常多的接口,比如java.util.List。
    抽象类是不能实例化的类,用abstract关键字修饰class,其目的是代码重用,除了不能实例化,形式上和一般的Java类并没有太大区别,可以有一个或者多个抽象方法,也可以没有抽象方法。抽象类大多用户抽取相关Java类的共用方法或者是共同成员变量,然后通过继承的方式达到代码重用的目的。Java标准类库中,比如collection框架,很多通用部分被抽取成为抽象类,例如java.util.AbstractList。
    Java类实现interface使用implements关键词,继承abstract class是使用extends关键词,例如ArrayList:
1
2
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable

参考:

声明:此为原创,转载请联系作者


作者:微信公众号添加公众号-遛狗的程序员 ,或者可以扫描以下二维码关注相关技术文章。

qrcode_for_gh_1ba0785324d6_430.jpg

当然喜爱技术,乐于分享的你也可以可以添加作者微信号:

WXCD.jpeg

文章目录
  1. 1. 问题:
  2. 2. 知识点补充
  3. 3. 回答问题:
|