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

二十一、Groovy 泛型

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

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

在定义类,接口和方法时,泛型使能类型(类和接口)作为参数。与在方法声明中使用的更熟悉的形式参数非常类似,类型参数提供了一种方法,可以为不同的输入重复使用相同的代码。区别在于形式参数的输入是值,而类型参数的输入是类型。

集合的通用

可以对集合类(如List类)进行一般化,以便只有该类型的集合在应用程序中被接受。下面显示了一般化ArrayList的示例。以下语句的作用是它只接受类型为string的列表项 –

List<String> list = new ArrayList<String>();

在下面的代码示例中,我们将执行以下操作:

  • 创建一个只包含字符串的通用ArrayList集合。
  • 向列表中添加3个字符串。
  • 对于列表中的每个项目,打印字符串的值。
class Example {
   static void main(String[] args) {
      // Creating a generic List collection
      List<String> list = new ArrayList<String>();
      list.add("First String");
      list.add("Second String");
      list.add("Third String");

      for(String str : list) {
         println(str);
      }
   } 
}

上述程序的输出将是 –

First String 
Second String 
Third String

泛型类

整个类也可以泛化。这使得类更灵活地接受任何类型,并相应地与这些类型工作。让我们来看一个例子,说明我们如何做到这一点。

在以下程序中,我们执行以下步骤 –

  • 我们正在创建一个名为ListType的类。注意放置在类定义前面的关键字。这告诉编译器这个类可以接受任何类型。因此,当我们声明这个类的一个对象时,我们可以在声明期间指定一个类型,并且该类型将在占位符
  • 泛型类有简单的getter和setter方法来处理类中定义的成员变量。
  • 在主程序中,注意我们能够声明ListType类的对象,但是不同类型的对象。第一个类型是Integer类型,第二个类型是String类型。
class Example {
   static void main(String[] args) {
      // Creating a generic List collection 
      ListType<String> lststr = new ListType<>();
      lststr.set("First String");
      println(lststr.get()); 

      ListType<Integer> lstint = new ListType<>();
      lstint.set(1);
      println(lstint.get());
   }
} 

public class ListType<T> {
   private T localt;

   public T get() {
      return this.localt;
   }

   public void set(T plocal) {
      this.localt = plocal;
   } 
}

上述程序的输出将是 –

First String 
1

干货推荐


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



未经允许不得转载:搜云库技术团队 » 二十一、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