【Java】开发中通用的方法和准则20条

1. 不要在常量和变量中出现易混淆的字母

包名全小写、类名首字母全大写、常量全部大写并下划线分割、变量采用驼峰命名等,这些是最基本的Java编码规范。

public class TestDemo {
      public static void main(String[] args) {
            long i = 1l;
            System.out.println("i的两倍是:" + (i+i)); //2
      }
}

字母 “l” 作为长整型标志时,务必大写!

2. 不要让常量变成变量

public class TestDemo {
      public static void main(String[] args) {
            System.out.println("这次常量值是:" + Const.RAND_CONST); //  每次运行值不一样
      }
}
interface Const {
      public static final int RAND_CONST = new Random().nextInt();
}

务必让常量的值在运行期保持不变。

3. 三元操作符的类型必须一致

public class TestDemo {
      public static void main(String[] args) {
            int i = 80;
            String s1 = String.valueOf(i < 100 ? 90 : 100);
            String s2 = String.valueOf(i < 100 ? 90 : 100.0);
            System.out.println(s1 + " " + s2); // 90 90.0
            System.out.println("两者是否相等:" + s1.equals(s2)); //  false
      }
}

保证三元操作符中的两个操作数类型一致,即可减少诸多可能错误的发生。

4. 避免带有变长参数的方法重载

public class TestDemo {
      public static void main(String[] args) {
            Client client = new Client();
            // 499元的货物,75折 == 374.25
            client.calPrice(49900, 75); // 简单折扣的价格:374.25
      }
}
class Client{
      public void calPrice(int price, int discount) {
            float knockdownPrice = price * discount / 100.0F;
            System.out.println("简单折扣的价格:" +  formateCurrency(knockdownPrice));
      }
      public void calPrice(int price, int... discounts) {
            float knockdownPrice = price;
            for (int i : discounts) {
                  knockdownPrice = knockdownPrice * i / 100;
            }
            System.out.println("复杂折扣的价格:" +  formateCurrency(knockdownPrice));
      }
      // 格式化货币形式
      private String formateCurrency(float price) {
            return  NumberFormat.getCurrencyInstance().format(price/100);
      }
}

避免带有变长参数的方法重载,以免陷入某些伤脑筋的小陷阱里。

5. 别让 null 值和空值威胁到变长方法

public class TestDemo {
      public static void main(String[] args) {
            TestDemo t = new TestDemo();
            
            String[] ss = null;
            t.m1("china", ss); // 此处可以编译过
            t.m1("aaa", 0);
            t.m1("china", "people");
            t.m1("china", null); // 此处编译不过,为何?
      }
      
      public void m1(String s, Integer... is) {
            System.out.println("111");
      }
      
      public void m1(String s, String... strs) {
            System.out.println("222");
      }
}

null值对于可变长参数来说,需要确保让编译器知道其类型,否则会有编译报错。

6. 重写变长方法也循规蹈矩

public class TestDemo {
      public static void main(String[] args) {
            Base b = new Sub();
            b.fun(100, 50);
            Sub sub = new Sub();
            sub.fun(100, 50); // 报错!!!
      }
}
class Base {
      void fun(int p, int... d) {
      }
}
class Sub extends Base {
      void fun(int p, int[] d) {
            
      }
}

重写的方法参数与父类相同,不仅仅是类型、数量,还需要包括显示形式。

7. 警惕自增的陷阱

public class TestDemo {
      public static void main(String[] args) {
            int count = 0;
            for (int i = 0; i < 10; i++) {
                  count = count++;
            }
            System.out.println("count=" + count); // 0
      }
}

后++的操作,赋值时,会先取值,后运算++。因此避免同时++时赋值的操作。

8. 不要让旧语法困扰你

public class TestDemo {
      public static void main(String[] args) {
            int f = 200;
            saveDefault:save(f); // 编译不报错,输出 200
      }
      static void saveDefault() { }
      static void save(int fee) {
            System.out.println(fee);
      }
}

可读性优先,旧的语法纵然不报错,也得让它随风去吧…

扫描二维码关注公众号,回复: 10849173 查看本文章

未完待续…

发布了320 篇原创文章 · 获赞 311 · 访问量 66万+

猜你喜欢

转载自blog.csdn.net/sinat_36184075/article/details/105083148