Java对现实世界数据的处理(2)-static和this关键字

一、static

(1)static相关变量和方法

1、所以static关键字修饰的都是类相关的,类级别的。

2、所以static修饰的,都是采用“类名.”的方式访问。

3、static修饰的变量:静态变量

4、static修饰的方法:静态方法

(2)什么时候变量声明为实例的,什么时候声明为静态的?

这个类型的所有对象的某个属性值都是一样的,不建议定义为实例变量,浪费内存空间。

建议定义为类级别特征,定义为静态变量,在方法区中只保留一份,节省内存开销。

(3)加static的变量叫做静态变量,静态变量在类加载的时候初始化,也就是说,静态变量是存放在方法区的。那么局部变量,实例变量,静态变量在JVM内存的分布是什么样子的?

(4)如何访问实例、静态。

实例的,一定是需要"引用."来访问,静态的:建议使用"类名."来访问,但使用"引用."也行(不建议使用)。

(5)关于方法来说什么时候定义为实例方法,什么时候定义为静态方法?

class User{
	private int id;
	
	public static void  setId(int I ){
		Id = I;
	}
	Public static int getId(){
		Return id;
	}

}

这里方法加上了static行吗?

不行,如果加上static说明是类级别的方法,然而id是实例的变量,所以会报错,原理就是每一个对象的id都不一样,但是setId.getId,加上了static,说明是类级别的,类级别的也就是没有创建对象就已经存在了,里面的含有实例的变量,实例变量是对象创建之后才有的,所以会报错。

(6)static在内存中的变量

class Chinese{
    String idCard;
    String name;
    static String country ="中国";
    //无参数
    public Chinese(){
    
    }
    //有参数
    public Chinese(String s1,String s2){
        idCard = s1;
        name = s2;
    }
}
public class StaticTest{
    public static void main(String [] args){
        //访问中国人的国际
        //静态变量应该使用类名.的方式访问
        System.out.println(Chinese.country);
        
        Chinese c1 = new Chinese("12314","张三");
        System.out.println(c1.idCard);
        System.out.println(c1.name);

        Chinese c2 = new Chinese("789","李四");
        System.out.println(c2.idCard);
        System.out.println(c2.name);
        
    }

}

idCard是实例变量,必须先new对象,通过"引用."访问

static内存图如下:

静态代码块 

(a)使用static关键字可以定义:静态代码块

(b)什么是静态代码块,语法是什么?

	Static{
		java语句;
		java语句;
	}

(c)static静态代码块在什么时候执行?

   类加载时执行,并且只执行1次。

(d)注意:静态代码块在类加载时执行,并且在main方法执行之前执行

(e)静态代码块一般都是按照自上而下的顺序执行。

(f)静态代码块有啥作用,有什么用?

    (I)静态代码块不常用。

    (II)静态代码块是SUN公司给java程序员一种特殊的时刻,叫做类加载时机。

      具体业务:

      所以我们编写的程序中,只要类加载了,请记录一下类加载的日志信息(那年那月,那个类加载到了JVM当中了)

写到静态代码块当中。

二、this

(1)this是关键字,小写。

(2)this是什么,在内存方面是怎么样的?

一个对象是一个this,this是一个变量,是一个引用,this保存当前对象的内存地址指向自身,

this就是代表的当前对象,this存储在堆内存当中对象里面。

public class ThisTest
{
	public static void main(String [] args){
		Customer c1 = new Customer();
		c1.name = "张三";
		c1.shopping();
		Customer c2 = new Customer();
		c2.name = "李四";
		c2.shopping();

	}
}
class Customer
{
	String name;
	public void shopping(){
		System.out.println(name+"正在购物");
	}

}

 

内存图: 

 

 (3)this什么时候不能省略

1、this可以使用在实例方法中,不能使用在静态方法中。

2、this关键字大部分情况下可以省略,什么时候不能省略呢?

public class HomeWork
{
	public static void main(String [] args){
		Wife beautiful = new Wife();
		beautiful.name ="hahha";
		Husband handsome = new Husband(111,"赵帅阁",beautiful);
		//handsome.show();
		//能够输出这个“丈夫对象”的妻子的名字。
		//System.out.println(handsome.show());
		System.out.println(handsome.name +"的妻子是"+handsome.wife.name);
	}
}
class Husband
{	
	//身份证号
	int userNumber;
	//姓名
	String name;
	//出生日期
	//UserDate birthDate;
	//妻子
	Wife wife;
	public Husband(){
	
	}
	public Husband(int userNumber,String name,UserDate birthDate,Husband husband){
		userNumber = userNumber;
		name=name;
		birthDate=birthDate;
		husband=husband;
	}
	//展现
	public void show(){

	System.out.println("这个丈夫是"+name+"他的妻子是"+wife);
	
	}
}
class Wife
{
	//身份证号
	int userNumber;
	//姓名
	String name;
	//出生日期
	UserDate birthDate;
	//丈夫
	Husband husband;
	public Wife(){
	
	}
	public Wife(int userNumber,String name,UserDate birthDate,Husband husband){
		userNumber = userNumber;
		name=name;
		birthDate=birthDate;
		husband=husband;
	}

}
class UserDate
{
	int year;
	int month;
	int day;
}

 这时候运行会出现Null值的情况,原因是因为:

变量是遵循了就近原则,userNumber=userNumber,里面的变量,都会认为是int的局部变量。而name还是实例变量,所以就会是null。 

this.表示引用,this的作用就是区分局部变量和实例变量的,所以这时候就不能省,并且可读性高了。 

 (4)this()的用法

1、this除了可以用在实例方法中,还可以用在构造方法中。

2、新语法:通过当前的构造方法区调用另一个本类的构造方法,可以使用一下语法格式:

this(实际参数列表);

通过一个构造方法1去调用构造方法2,可以做到代码复用,但需要注意的是“构造方法1”和“构造方法2”都是在同一个类当中。

注意:this()的调用必须出现在构造方法的第一行。

3、this()的作用就是代码复用。

4、用代码演示this()的作用:

public class ThisTest
{
	public static void main(String [] args){
		Date d1 = new Date();
		d1.show();
		Date d2 = new Date(2020,4,29);
		d2.show();
	}
}
class Date
{
	private int year;
	private int month;
	private int day;
	//设置get和set,可以通过set设置关卡
	public  int getYear(){
		return year;
	}
	public void setYear(int year){
		this.year = year;
	}
	public int getMonth(){
		return month;
	}
	public void setMonth(int month){
		this.month = month;
	}
	public int getDay(){
		return day;
	}
	public void getDay(int day){
		this.day = day;
	}
	//构造函数
	public Date(){
	this.year = 1970;
	this.month =1;
	this.day =1;
	}
	public Date(int year,int month,int day){
	this.year = year ;
	this.month = month;
	this.day = day;
	}
	//写一个方法区展示year,month,day
	public void show(){
		System.out.println(this.year +"年"+this.month+"月"+this.day+"日");
	}
}

 

这里会显的特别的啰嗦,两个构造函数也就重复了。

所以这时会使用this()。

三 总结this

1.1、this是一个关键字

1.2、this可以使用在实例方法中,也可以使用在构造方法中。

1.3、this出现在实例方法中其实代表的是当前对象

1.4、this不能使用在静态方法中。

1.5、this.大部分情况下可以省略,但是用来区分局部变量和实例变量的时候不能省略。

1.6、this()这种语法只能出现在构造方法第一行,表示当前构造方法调用本类其他的构造方法,目的是代码复用。

猜你喜欢

转载自blog.csdn.net/MyxZxd/article/details/105841337