今天我们一起学习一下7大常见修饰符,包括权限修饰符(4):private,protected,默认,public和状态修饰符(2):static,final以及抽象修饰符(1):abstract。
下面我们分别对常见的类及其组成的修饰进行Demo验证:
1.类:
//类:
//private:Illegal modifier for the class ClassPermissionTest; only public, abstract & final are permitted
//protected:Illegal modifier for the class ClassPermissionTest; only public, abstract & final are permitted
//public 默认 ok
//static:Illegal modifier for the class ClassPermissionTest; only public, abstract & final are permitted
//final ok
//abstract ok
public class ClassPermissionTest {
结论:
默认,public,final,abstract
常用:public
2.成员变量:
//成员变量
public int a = 1;
protected int b = 2;
int c = 3;
private int d = 4;
static int e = 5;
final int f = 6;
结论:
private,默认,protected,public,static,final
常用:private
3.构造方法:
//构造方法-权限修饰符
//static:Illegal modifier for the constructor in type ClassPermissionTest; only public, protected & private are permitted
//final:Illegal modifier for the constructor in type ClassPermissionTest; only public, protected & private are permitted
//abstract:Illegal modifier for the constructor in type ClassPermissionTest; only public, protected & private are permitted
private ClassPermissionTest(){}
ClassPermissionTest(String s){}
protected ClassPermissionTest(int i){}
public ClassPermissionTest(boolean b){}
结论:
private,默认,protected,public
常用:public
4.成员方法:
//成员方法-权限修饰符、static、final、abstract 所有ok
//Abstract methods do not specify a body,父类
//abstract void show();
void show() {}
结论:
private,默认,protected,public,static,final,abstract
常用:public
ps:使用abstract时类必须是抽象类且方法不能有方法体{}
附比较常见的:
public static final int X = 10;
public static/final /abstract void show() {}