Java 篇
抽象類
定義
- 抽象類是使用關(guān)鍵字
abstract聲明的類。抽象類與普通類的最大區(qū)別是抽象類不能被實(shí)例化。 - 抽象類可以擁有沒有方法體的抽象方法也可以擁有普通方法,抽象方法使用
abstract聲明。 - 普通類可以使用關(guān)鍵字
extends繼承普通類,繼承時(shí)必須重寫所有抽象方法。 - 普通類可以繼承抽象類,抽象類可以繼承抽象類。
使用
定義一個(gè)抽象類
public abstract class View {
protected int width;
protected int height;
public abstract String getDescription();
}
繼承抽象類
public class Button extends View {
private String text;
public Button(String text, int width, int height) {
this.text = text;
this.width = width;
this.height = height;
}
@Override
public String getDescription() {
return text;
}
}
接口
定義
- 接口使用關(guān)鍵字
interface聲明,接口中聲明的所有普通方法都為public abstract,所有屬性都為public static final常量。 - 類使用關(guān)鍵字
implements實(shí)現(xiàn)接口 - 一個(gè)接口可以繼承其它接口
- Java 8 開始支持函數(shù)式接口和接口默認(rèn)方法
使用
定義一個(gè)接口
public interface OnClickListener {
void onClick();
String getDescription();
}
實(shí)現(xiàn)接口
public class Button extends View implements OnClickListener {
private String text;
public Button(String text, int width, int height) {
this.text = text;
this.width = width;
this.height = height;
}
@Override
public void onClick() {
System.out.println("Click Button " + text);
}
@Override
public String getDescription() {
return text;
}
}
函數(shù)式接口
函數(shù)式接口有且只有一個(gè)抽象方法,是實(shí)現(xiàn) Lambda 表達(dá)式的手段,具體見方法與閉包章節(jié)。
interface Convert<F, T> {
T convert(F from);
}
接口默認(rèn)方法
Java 1.8 新增功能,以前版本的接口只能有抽象方法,現(xiàn)在則可以為接口提供默認(rèn)方法,但是默認(rèn)方法無法在 Lambda 表達(dá)式內(nèi)部被訪問
interface Calculator {
int add(int x, int y);
// 接口默認(rèn)方法
default int addOne(int x) {
return x + 1;
}
}
Groovy 篇
抽象類
Groovy 中抽象類的使用類似 Java
定義一個(gè)抽象類
abstract class View {
protected def width
protected def height
abstract def getDescription()
}
繼承抽象類
class Button extends View {
private def text
@Override
def getDescription() {
return text
}
}
接口
Groovy 中接口使用類似 Java,但是沒有 Java 1.8 那些特性
定義一個(gè)接口
interface OnClickListener {
def onClick()
def getDescription()
}
實(shí)現(xiàn)接口
class Button extends View implements OnClickListener {
private def text
@Override
def onClick() {
println("Click Button $text")
}
@Override
def getDescription() {
return text
}
}
使用閉包實(shí)現(xiàn)接口和抽象類
Groovy 中可以通過定義一個(gè)包含需要實(shí)現(xiàn)的類或接口的方法的閉包來實(shí)現(xiàn)抽象類或接口。
例:
def textview = [getDescription: { -> }] as View
textview.metaClass.getDescription = { -> "This is a TextView." }
println(textview.getDescription())
以上例子通過定義了一個(gè)包含 getDescription() 方法閉包來實(shí)現(xiàn)抽象類 View。
同樣該方法也適用于接口
def listener = [onClick: { -> println("Trigger click event.") }] as OnClickListener
listener.onClick()
Scala 篇
抽象類
定義
- Scala 中抽象類的使用類似 Java,但是聲明抽象方法時(shí)無需加上
abstract關(guān)鍵字。 - 實(shí)現(xiàn)抽象方法時(shí)可以不加關(guān)鍵字
override,重新普通方法時(shí)則必須加override
使用
定義一個(gè)抽象類
abstract class View(val width: Int, val height: Int) {
def getDescription(): String
}
繼承抽象類
其中 getDescription() 實(shí)現(xiàn)了 View 中的抽象方法,所以可以不加 override
,而 toString 則重寫了普通方法,所以必須加 override。
class Button(val text: String, width: Int, override val height: Int) extends View(width, height) {
def getDescription(): String = text
override def toString: String = getDescription()
}
子類繼承父類時(shí),子類構(gòu)造方法中定義的參數(shù)如果和父類相同無需添加
val或var修飾符(上述例子中的width),否則的話相當(dāng)于覆蓋(上述例子中的height)。
接口
Scala 中沒有接口的概念,但是有更加強(qiáng)大的 Trait,在后面的章節(jié)會具體說。
屬性重寫規(guī)則
Scala 中不但可以重寫方法,也可以重寫屬性,屬性重寫需要遵守以下規(guī)則:
-
def可以重寫def -
val可以重寫val或無參的def -
var只能重寫抽象的var
class Person(val name: String) {
val valValue = "PersonVal"
var varValue = "PersonVar"
def defValue1 = "PersonDef"
def defValue2 = "PersonDef2"
}
class Employee(name: String, salary: Int) extends Person(name) {
override val valValue: String = "val Override val"
// Wrong!!
// override var x: String = "var Override var"
override val defValue1: String = "val Override def"
override def defValue2: String = "def Override def"
}
def employee = new Employee("Jane", 30)
println(employee.valValue) // val Override val
println(employee.varValue) // PersonVar
println(employee.defValue1) // val Override def
println(employee.defValue2) // def Override def
Protected
- Scala 中
protected修飾的成員可以被子類訪問,但是不能被其它位置的類(包括同包的其它類)訪問。而 Java 則是可以被同包及子類訪問。 - 如果要讓同包的其它類訪問,需要使用包修飾符
protected[packageName]。 -
protected[this]可以將訪問權(quán)限限定在當(dāng)前對象
//限定子類
protected def info = name
//限定子類 + 同包的類
protected[_16_inherit] def info2 = name
Kotlin 篇
Open Class
定義
- 與其它三門語言都不一樣,Kotlin 中默認(rèn)所有 Class 都是 final 的,即不能被任何類繼承。
- 如果希望一個(gè)類能夠被繼承,需要將該類聲明為
open。同樣,Kotlin 中所有方法也為 final,如果希望方法能被重寫,也必須聲明方法為open,且重寫的方法必須聲明override關(guān)鍵字。 - Kotlin 中使用符號
:來繼承類或?qū)崿F(xiàn)接口
使用
聲明一個(gè) open class
open class Person(name: String) {
open fun foo() {
}
open fun bar() {
}
fun foo2(){
}
}
繼承該 open class
class Employee(name: String) : Person(name) {
constructor(name: String, age: Int) : this(name) {
}
override fun foo() {
super.foo()
}
final override fun bar() {
super.bar()
}
override fun toString(): String {
return super.toString() + javaClass.name
}
}
抽象類
Kotlin 中抽象類的聲明類似 Java。但是抽象類和其包含抽象方法默認(rèn)為 open,而普通方法則仍然默認(rèn)為 final。
定義一個(gè)抽象類
abstract class View(val width: Int, val height: Int) {
abstract fun getDescription(): String
open fun onClick() {
println("Click event of View")
}
}
繼承抽象類
class Button(val text: String, width: Int, height: Int) : View(width, height) {
override fun getDescription(): String {
return text
}
override fun onClick() {
}
}
接口
- Kotlin 中接口和接口中的所有成員都默認(rèn)為
open。 - Kotlin 中接口除了有抽象方法也可以有普通方法和抽象屬性
定義一個(gè)接口
interface OnClickListener {
// 抽象屬性
val prop: Int
// 普通方法
fun onClick() {
println("Click event of OnClickListener")
}
// 抽象方法
fun getDescription(): String
}
實(shí)現(xiàn)接口
class Button(val text: String, width: Int, height: Int) : View(width, height), OnClickListener {
override fun onClick() {
super<View>.onClick()
super<OnClickListener>.onClick()
}
override val prop: Int
get() = width * height
override fun getDescription(): String {
return text
}
}
以上屬性 prop 實(shí)現(xiàn)了 OnClickListener 接口的抽象屬性。而在 onClick() 方法中可以通過 super<接口或類名>.方法名 來指定調(diào)用的父類方法的具體源頭。
Summary
- Kotlin 使用符號
:而非extends繼承類和接口 - Scala 沒有接口的概念
- Kotlin 默認(rèn)普通方法和類都是
final的 - Groovy 中可以通過閉包繼承類或接口
文章源碼見 https://github.com/SidneyXu/JGSK 倉庫的 _19_inherit 小節(jié)