Java基础之Static关键字知识点

一 附上Demo,注释包含了 Static 与 this 相关知识点。
package com.dong.four;

public class TestP2 {

	public static void main(String[] args) {
		//测试下short数据类型
		short s1=1;
		//s1=(short) (s1+(short)1);//进行数据类型转换
		
		//s1=s1+1; 如果是这样的写法,会报错 必须要进行强制转换类型,并且对运算后的结果也需要进行转换
		
		//s1 +=1; 这样的计算后的结果是没问题的
		System.out.println(s1);
		
		int a = s1 + 1;//这样也不会报错
		System.out.println(a);

	}

}

二 附上Demo2,注释包含了this关键字相关知识点

package com.dong.four;

public class TestThis {

	private int a ;
	private int b ;
	private int c;
	public TestThis(){
		
	}
	
	public TestThis(int a,int b){
		this.a = a; //此处加上this关键字就是区分成员变量和局部变量 this关键字第一个用处
		this.b = b;
	}
	
	public TestThis(int a,int b,int c){
		/*this.a=a;  此时如果不想这样写 可以按照下面的方式写  this(a,b);
		this.b=b;*/
		this(a,b);//这个就相当于调用上面的构造方法      用处就是构造方法的调用
		this.c =c; //除此之外 this关键字是不能用在 static关键字里面
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}

猜你喜欢

转载自blog.csdn.net/m0_37264382/article/details/80172319