gpt4 book ai didi

java - 使用集合接口(interface)名称作为变量名称的一部分是一个好习惯吗?

转载 作者:太空宇宙 更新时间:2023-11-04 12:50:04 24 4
gpt4 key购买 nike

我知道使用Hungarian notation体贴是一种不好的做法。

但是,当我们使用集合时,使用集合接口(interface)名称作为变量名称的一部分是一个不好的做法吗?

  • 示例 1:

     List<Student> listStudents; 
    //or
    List<Student> studentsList;

    对比

     List<Student> students;
  • 示例 2:

    Map<Integer, Student> mapStudents; 
    //or
    Map<Integer, Student> studentsMap;

    对比

    Map<Integer, Student> students;
  • 示例 3:

       Set<Student> setStudents;  
    //or
    Set<Student> studentsSet

    对比

       Set<Student> students;

最佳答案

如果您更仔细地选择名称,您通常会发现,正确描述集合做什么而不是它是什么会容易得多。

  • 示例 1:

     List<Student> listStudents; **NO**

    对比

     List<Student> allStudents; **YES**
  • 示例 2:

    Map<Integer, Student> studentsByID; **YES**   

    对比

    Map<Integer, Student> students; **NO**
  • 示例 3:

       Set<Student> specialNeedsStudents;  **YES**

    对比

       Set<Student> students; **NO**

关于java - 使用集合接口(interface)名称作为变量名称的一部分是一个好习惯吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35917982/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com