规范的代码风格

127 阅读1分钟

正确的注释和注释风格

  1. 使用文档注释来注释整个类或整个方法
  2. 如果注释方法中的某一个步骤,使用单行或多行注释

正确的缩进和空白

  1. 使用一次tab操作,实现缩进,默认整体向右边移动,时候用shift+tab整体向左移动
  2. 运算符和 = 两边习惯性各加一个空格。比如:2 + 4 * 5 + 345 - 89
  3. 代码块的书写格式分为:行尾风格,次行风格

代码演示

次行风格

public class Demo 
{
    public static void main(String[] args) 
    {
        // 选择要整理的代码,输入tab键,就是整体向右移动
            System.out.println("hello world");
            System.out.println("hello world");
        // 选择要整理的代码,输入shift + tab键,就是整体向左移动
    System.out.println("hello world");
        // 运算符和 = 两边习惯性各加一个空格
        int num1 = 10;
        int num2 = 30;
        int sum = num1 + num2;
        System.out.println(sum);
    }
}

行尾风格

public class Demo {
    public static void main(String[] args) {
        // 选择要整理的代码,输入tab键,就是整体向右移动
            System.out.println("hello world");
            System.out.println("hello world");
        // 选择要整理的代码,输入shift + tab键,就是整体向左移动
    System.out.println("hello world");
        // 运算符和 = 两边习惯性各加一个空格
        int num1 = 10;
        int num2 = 30;
        int sum = num1 + num2;
        System.out.println(sum);
    }
}