
- Groovy 教程
- Groovy - 首頁
- Groovy - 概述
- Groovy - 環境
- Groovy - 基本語法
- Groovy - 資料型別
- Groovy - 變數
- Groovy - 運算子
- Groovy - 迴圈
- Groovy - 決策
- Groovy - 方法
- Groovy - 檔案 I/O
- Groovy - 可選值
- Groovy - 數字
- Groovy - 字串
- Groovy - 範圍
- Groovy - 列表
- Groovy - 對映
- Groovy - 日期和時間
- Groovy - 正則表示式
- Groovy - 異常處理
- Groovy - 面向物件
- Groovy - 泛型
- Groovy - 特性
- Groovy - 閉包
- Groovy - 註解
- Groovy - XML
- Groovy - JMX
- Groovy - JSON
- Groovy - DSLS
- Groovy - 資料庫
- Groovy - 構建器
- Groovy - 命令列
- Groovy - 單元測試
- Groovy - 模板引擎
- Groovy - 元物件程式設計
- Groovy 有用資源
- Groovy - 快速指南
- Groovy - 有用資源
- Groovy - 討論
Groovy - 特性
特性是語言的結構構造,允許:
- 行為的組合。
- 介面的執行時實現。
- 與靜態型別檢查/編譯的相容性
它們可以被視為包含預設實現和狀態的介面。使用 trait 關鍵字定義特性。
下面給出一個特性的示例:
trait Marks { void DisplayMarks() { println("Display Marks"); } }
然後可以使用 implement 關鍵字以與介面類似的方式實現特性。
class Example { static void main(String[] args) { Student st = new Student(); st.StudentID = 1; st.Marks1 = 10; println(st.DisplayMarks()); } } trait Marks { void DisplayMarks() { println("Display Marks"); } } class Student implements Marks { int StudentID int Marks1; }
實現介面
特性可以實現介面,在這種情況下,介面使用 implements 關鍵字宣告。
下面給出一個特性實現介面的示例。在下面的示例中,可以注意到以下關鍵點。
定義了一個名為 Total 的介面,其中包含 DisplayTotal 方法。
特性 Marks 實現 Total 介面,因此需要為 DisplayTotal 方法提供實現。
class Example { static void main(String[] args) { Student st = new Student(); st.StudentID = 1; st.Marks1 = 10; println(st.DisplayMarks()); println(st.DisplayTotal()); } } interface Total { void DisplayTotal() } trait Marks implements Total { void DisplayMarks() { println("Display Marks"); } void DisplayTotal() { println("Display Total"); } } class Student implements Marks { int StudentID int Marks1; }
上述程式的輸出將是:
Display Marks Display Total
屬性
特性可以定義屬性。下面給出一個包含屬性的特性的示例。
在以下示例中,型別為整數的 Marks1 是一個屬性。
class Example { static void main(String[] args) { Student st = new Student(); st.StudentID = 1; println(st.DisplayMarks()); println(st.DisplayTotal()); } interface Total { void DisplayTotal() } trait Marks implements Total { int Marks1; void DisplayMarks() { this.Marks1 = 10; println(this.Marks1); } void DisplayTotal() { println("Display Total"); } } class Student implements Marks { int StudentID } }
上述程式的輸出將是:
10 Display Total
行為組合
特性可用於以受控的方式實現多重繼承,避免菱形問題。在以下程式碼示例中,我們定義了兩個特性 - **Marks** 和 **Total**。我們的 Student 類實現了這兩個特性。由於 student 類擴充套件了這兩個特性,因此它能夠訪問這兩個方法 - **DisplayMarks** 和 **DisplayTotal**。
class Example { static void main(String[] args) { Student st = new Student(); st.StudentID = 1; println(st.DisplayMarks()); println(st.DisplayTotal()); } } trait Marks { void DisplayMarks() { println("Marks1"); } } trait Total { void DisplayTotal() { println("Total"); } } class Student implements Marks,Total { int StudentID }
上述程式的輸出將是:
Total Marks1
擴充套件特性
特性可以擴充套件另一個特性,在這種情況下,必須使用 **extends** 關鍵字。在以下程式碼示例中,我們使用 Marks 特性擴充套件了 Total 特性。
class Example { static void main(String[] args) { Student st = new Student(); st.StudentID = 1; println(st.DisplayMarks()); } } trait Marks { void DisplayMarks() { println("Marks1"); } } trait Total extends Marks { void DisplayMarks() { println("Total"); } } class Student implements Total { int StudentID }
上述程式的輸出將是:
Total
廣告