Java封装——this、static关键字

本文详细讲解了Java中this关键字的作用,包括在方法内部引用对象属性、解决方法内形参与成员变量同名问题,以及在构造方法中的应用。同时介绍了static关键字,包括静态方法的特性、静态变量和静态代码块的用法。通过实例演示加深理解。
摘要由CSDN通过智能技术生成

一、this关键字作用

第3和最后一个用的最多

  • 在类的方法中,使用this代表的是调用此方法的对象的引用
//类中使用this,就调用了对象的name变量
void testThis(){
	System.out.println(this.name);
}

Student stu3 = new Student(18,"xiaowei",99.9);
//stu3.introduce();
stu3.testThis();
/*
xiaowei
*/
  • this可以看做是一个变量,它的值是当前对象的引用
void testThis(){
	Student stutmp = null;  //没有指向的变量
	stutmp = this;			//把当前对象的变量赋给stutmp
	System.out.println(stutmp.name);//直接使用
	System.out.println(this.name);
}

Student stu3 = new Student(18,"xiaowei",99.9);
//stu3.introduce();
stu3.testThis();
/*
xiaowei
xiaowei
*/
  • 使用this可以处理方法中的成员变量和形参同名问题
Student(int age, String name, double score){
	System.out.println("构造方法三");
	this.age = age;		//成员变量和形参同名age = age;
	this.name = name;
	this.score = score;
}

Student stu3 = new Student(18,"xiaowei",99.9);
stu3.introduce();
/*
age=18,name=xiaowei,score=99.9
*/
  • 当在方法内需要用到调用到该方法的对象时,就可以this
    和第一个一样
  • 在类的构造方法中可以调用this(参数列表)来调用该类的指定构造方法
Student(){
	System.out.println("构造方法一");
}
Student(int newage){
	System.out.println("构造方法二");
	age = newage;
}
Student(int age, String name, double score){
	this();//只能调用一个,使用这个就不能使用shis(18);
	System.out.println("构造方法三");
	this.age = age;
	this.name = name;
	this.score = score;
}

Student stu3 = new Student(18,"xiaowei",99.9);

/*
构造方法一
构造方法三
*/

二、static关键字

static关键字,注意事项
1、静态方法中只能访问外部的静态成员
2、静态方法中不能出现this关键字(先于对象之前,this是对象的引用)

	static void test(){
		System.err.println(name);		//静态方法只能访问静态成员
		System.out.println(this.age); //静态方法中不能出现this关键字
	}
  • 用来修饰类成员,修饰成员变量称为类变量(静态变量)
int age;
String name;
double score;
static int data;	//类变量

Student stu3 = new Student(18,"xiaowei",99.9);
//Student.score = 10;
Student.data = 10;	//类变量,可以通过类.变量名 访问变量
stu3.data = 10;		//当然也可以使用对象.变量名 访问
  • 修饰方法叫,类方法(静态方法)
public class Test {
	//不加static的访问
	public static void main(String[] args) {
		Test s1 = new Test();
		System.out.println("ret = " + s1.add(1,3));
	}
	//加static 的访问方法
	public static void main(String[] args) {
		System.out.println("ret = " + add(1,3));
	}
	//加上static  main就可以直接访问
	static int add(int a, int b){
		return a+b;
	}
}

/*
ret = 4
*/
  • 当类被加载的时候就会被加载,优先于对象的存在
    可以不依赖于对象进行访问(类变量,类方法)
  • 用来修饰语句块——称为静态代码块,先于构造方法执行,只会执行一次,用来对静态成员做初始化
//静态代码块,与构造方法类似,都是初始化变量,只是这个是初始化静态变量
static{
	System.out.println("静态代码块");
	data = 110;
}
  • 调用的时候可以直接通过类名.成员来访问
    类名.成员

三、代码演示

this完整代码

class Student{//class 相当于c语言的struct
	int age;
	String name;
	double score;
	
	void testThis(){
		Student stutmp = null;
		stutmp = this;
		System.out.println(stutmp.name);
		System.out.println(this.name);
	}
	
	Student(){
		System.out.println("构造方法一");
	}
	Student(int newage){
		System.out.println("构造方法二");
		age = newage;
	}
	Student(int age, String name, double score){
		this();
		System.out.println("构造方法三");
		this.age = age;
		this.name = name;
		this.score = score;
	}


	void introduce(){
		System.out.println("age="+ age +",name=" + name + ",score=" + score);
	}
}
public class Test {
	public static void main(String[] args) {
		Student stu3 = new Student(18,"xiaowei",99.9);
		stu3.introduce();
		stu3.testThis();
	}
}

/*
构造方法一
构造方法三
age=18,name=xiaowei,score=99.9
xiaowei
xiaowei
*/

static完整代码

class Student{//class 相当于c语言的struct
	int age;
	String name;
	double score;
	static int data;
	
	Student(int age, String name, double score){
		System.out.println("构造方法三");
		this.age = age;
		this.name = name;
		this.score = score;
	}

	static{
		System.out.println("静态代码块");
		data = 110;
	}
	
//	static void test(){
//		System.err.println(name);		//静态方法只能访问静态成员
//		System.out.println(this.age); //静态方法中不能出现this关键字
//	}

	void introduce(){
		System.out.println("age="+ age +",name=" + name + ",score=" + score);
	}
}
public class Test {
	public static void main(String[] args) {
		Student stu2 = new Student(18,"xiaowei",99.9);
		Student stu3 = new Student(18,"xiaowei",99.9);
		Student.data = 10;
		
		System.out.println("ret = " + add(1,3));
	}
	
	static int add(int a, int b){
		return a+b;
	}
}

师承上官可编程 —— 陈立臣

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dz小伟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值