品牌网站升级,wordpress 后台主题跟前台一致,wordpress阿里云插件,丽水网站开发公司文章目录final修饰符1. final不能用来修饰构造方法2. final修饰变量的一些注意问题2.1 final修饰成员变量2.2 final修饰引用类型2.2.1 演示代码中lombok链式编程介绍final修饰符
final具有“不可改变”的含义#xff0c;它可以修饰非抽象类、非抽象成员方法和变量。
用final…
文章目录final修饰符1. final不能用来修饰构造方法2. final修饰变量的一些注意问题2.1 final修饰成员变量2.2 final修饰引用类型2.2.1 演示代码中lombok链式编程介绍final修饰符
final具有“不可改变”的含义它可以修饰非抽象类、非抽象成员方法和变量。
用final修饰的类不能被继承没有子类用final修饰的方法不能被子类的方法覆盖Override用final修饰的变量表示常量只能被赋值一次
1. final不能用来修饰构造方法
final不能用来修饰构造方法因为“方法覆盖Override”仅适用于类的成员方法而不适用于类的构造方法父类的构造方法和子类的构造方法之间不存在覆盖Override关系所以用final修饰构造方法是无意义的。
2. final修饰变量的一些注意问题
2.1 final修饰成员变量 final修饰成员变量必须显示初始化否则会产生编译错误 final修饰的实例变量可以在定义变量时候或者在构造方法中进行初始化
public class TestFinal1 {final int i1;final int j;public TestFinal1(int j) {this.j j;}
}public class TestFinal1 {final int i;final int j;public TestFinal1(int i, int j) {this.i i;this.j j;}
}final修饰的静态变量可以再变量定义时进行初始化或者在静态代码块中初始化
public class TestFinal1 {static final int i1;static final int j;static {j2;}
}2.2 final修饰引用类型 final修饰引用类型变量那么该变量只能始终引用一个对象但是对象的内容可以改变 Data
Accessors(chain true)
public class Person {private String name;private int age;
}public class TestFinal {public static void main(String[] args) {ListPerson personList new ArrayListPerson(){{add(new Person().setAge(25).setName(张三));add(new Person().setAge(28).setName(李四));add(new Person().setAge(34).setName(王五));}};ListPerson newPersonList new TestFinal().method(personList);System.out.println(newPersonList);}public ListPerson method(final ListPerson personList){personList.add(new Person().setAge(29).setName(朱六));return personList;}
}输出结果
[Person(name张三, age25), Person(name李四, age28), Person(name王五, age34), Person(name朱六, age29)]2.2.1 演示代码中lombok链式编程介绍
在实体上面添加注解Accessors(chain true)如
Data
Accessors(chain true)
public class Person {private String name;private int age;
}那么就可以针对该实体进行如下链式编程操作
new Person().setAge(25).setName(张三)