IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

二十二、Groovy 特征

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

文章永久连接:https://tech.souyunku.com/?p=8509

特征是语言的结构构造,允许 –

  • 行为的组成。
  • 接口的运行时实现。
  • 与静态类型检查/编译的兼容性

它们可以被看作是承载默认实现和状态的接口。使用trait关键字定义trait。

下面给出了一个特征的例子:

trait Marks {
   void DisplayMarks() {
      println("Display Marks");
   } 
}

然后可以使用implement关键字以类似于接口的方式实现trait。

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;
}

实现接口

Traits可以实现接口,在这种情况下,使用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

属性

特征可以定义属性。下面给出了具有属性的trait的示例。

在以下示例中,integer类型的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类实现了两个特征。由于学生类扩展了这两个特征,它能够访问这两种方法 – 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 trait扩展了Total trait。

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

干货推荐


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(90) 打赏



未经允许不得转载:搜云库技术团队 » 二十二、Groovy 特征

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367