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