JavaSE:3min讲解常见的修饰符

今天我们一起学习一下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() {}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值