栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

this 和 static 详解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

this 和 static 详解

static:
    1、static翻译为"静态"
    2、所有的static关键字修饰的都是类相关的,类级别的
    3、所有static修饰的,都采用"类名."的方式访问
    4、static修饰的变量:静态变量
    5、static修饰的方法:静态方法

变量的分类:
    变量根据声明的位置进行划分:
       在方法体当中声明的变量叫做:局部变量
       在方法体外中声明的变量叫做:成员变量

    成员变量又可以分为:
       实例变量
       静态变量

class VarTest{
	// 以下实例的,都是对象相关的,访问时采用"引用."的方式访问,需要先new对象
	// 实力相关的,必须先有对象,才能访问,可能会出现空指针异常
	// 成员变量中的实例变量
	int i;
	// 实例方法
	public void m1(){		
	}
	
	// 成员变量中的静态变量
	static int k;
	// 静态方法
	public static void m2(){
		
	}
}

 什么时候变量声明为实例的,什么时候声明为静态的?
    如果这个类型的所有对象的某个属性值都是一样的,
    不建议定义为实例变量,浪费内存空间,建议定义为
    类级别特征,定义为静态变量,在方法区中只保留一
    份,节省内存开销。

    一个对象一份的是实例变量
    多个对象一份的是静态变量

public class staticTest02
{
	public static void main(String[] args){
		 Chinese c1 =new Chinese("123456","junker","中国");
		 Chinese c2 =new Chinese("00000","jun","中国");
		 System.out.println(c1.id);
		 System.out.println(c1.name);
		 System.out.println(c1.country);
		 System.out.println(c2.id);
		 System.out.println(c2.name);
		 System.out.println(c2.country);		
}
}
// 定义一个类:中国人

class Chinese{
	// 身份证号
	// 每个人的身份证号不同,所以身份证号应该是实例变量,一个对象一份
	String id;
	// 姓名
	// 姓名也是一个人一个姓名,姓名也应该为实例变量
	String name;
	// 国籍
	// 对于"中国人"这个类来说,国籍都是"中国",不会随着对象的改变而改变
	// 显然国籍并不是对象级别的特征
	// 国籍属于整个类的特征
	String country;
	
	// 无参数
	public Chinese(){
		
	}

	// 有参数
	public Chinese(String s1,String s2,String s3){
		id =s1;
		name =s2;
		country =s3;
	}
}

当变量 country不为static静态的时候的内存图如下所示:

当变量country为static静态变量时的代码演练和内存图如下所示:

代码演练2:(为static静态变量,应该使用"类名."的方式访问)

public class staticTest02
{
	public static void main(String[] args){
		
		// 访问中国人国籍
		// 国籍为static静态变量,应该使用"类名."的方式访问
		 Chinese c1 =new Chinese("123456","junker");
		 System.out.println(Chinese.country);
		 
		 // 报错[id,name是实例变量,应该先new一个对象 通过"引用."的方式访问]
		 //System,out.println(Chinese.id);
		 
		 System.out.println(c1.id);
		 System.out.println(c1.name);
		 
		 Chinese c2 =new Chinese("00000","jun");
		 System.out.println(c2.id);	
		 System.out.println(c2.name);
		 System.out.println(Chinese.country);
		 		
}
}
// 定义一个类:中国人

class Chinese{
	// 身份证号
	// 每个人的身份证号不同,所以身份证号应该是实例变量,一个对象一份
	String id;
	// 姓名
	// 姓名也是一个人一个姓名,姓名也应该为实例变量
	String name;
	// 国籍
	// 重点五颗星:加有static的变量叫做静态变量
	// 静态变量在类加载时初始化,不需要new对象,静态变量的空间就开出来了
	// 静态变量存储在方法区
	// 静态变量一般会给一个值
	static String country ="中国";
	
	// 无参数
	public Chinese(){
		
	}

	// 有参数
	public Chinese(String s1,String s2){
		id =s1;
		name =s2;
	}
}

 代码演练3:

实例的:一定需要使用"引用."来访问
静态的:建议使用"类名."来访问,但使用"引用."也可以 ,如果使用"引用."来访问会让别人产生困惑:程序员以为是实例的呢

public class staticTest02
{
	public static void main(String[] args){
		 Chinese c1 =new Chinese("123456","junker");
		 
		 // 报错[id,name是实例变量,应该先new一个对象 通过"引用."的方式访问]
		 //System,out.println(Chinese.id);
		 
		 System.out.println(c1.id);
		 System.out.println(c1.name);
	 // 访问中国人国籍
	// 国籍为static静态变量,应该使用"类名."的方式访问
		 System.out.println(Chinese.country);
		 
		 Chinese c2 =new Chinese("00000","jun");
		
		 System.out.println(c2.id);	
		 System.out.println(c2.name);
		 
		 // 使用"引用."的方式访问依然通过 
		 System.out.println(c2.country);	// 中国
		 		
}
}
// 定义一个类:中国人

class Chinese{
	String id;
	
	String name;

	static String country ="中国";
	
	// 无参数
	public Chinese(){
		
	}

	// 有参数
	public Chinese(String s1,String s2){
		id =s1;
		name =s2;
	}
}
空引用访问静态不会空指针异常
public class staticTest02
{
	public static void main(String[] args){
		 Chinese c1 =new Chinese("123456","junker");
		 System.out.println(c1.id);
		 System.out.println(c1.name);
		 System.out.println(Chinese.country);
		 
		 Chinese c2 =new Chinese("00000","jun");	
		 System.out.println(c2.id);	
		 System.out.println(c2.name);
		 
		 // c2 是空引用
		 c2 =null;

		 // 不会出现空指针异常
		 // 因为静态变量不需要对象的存在
		 // 实际上以下的代码运行的时候,还是:System.out.println(Chinese.country);
		 System.out.println(c2.country);// 依然通过	

		 // 出现空指针异常 因为name为实例变量
		 // System.out.println(c2.name);
		 		
}
}
// 定义一个类:中国人

class Chinese{
	String id;
	
	String name;

	static String country ="中国";
	
	// 无参数
	public Chinese(){
		
	}

	// 有参数
	public Chinese(String s1,String s2){
		id =s1;
		name =s2;
	}
}
补充 【掌握】
public class staticTest03
{
	public static void main(String[] args){
		User u =new User();
		int i =u.getId();
		System.out.println(i);
	}
}


class User
{	
	// 实例变量又称为对象变量
	private int id; // id为对象级别的变量
	
	
	public int getId(){
		return id;
	}

}

静态方法时变量也要是静态的:

一、静态代码块 

    1、使用static关键字可以定义:静态代码块

    2、什么是静态代码块,语法是什么?
       static{
        java语句;
        ...
       }

    3、static静态代码块在什么时候执行呢?
       特点: 类加载的时候执行,并且只执行一次   
    
    4、注意:静态代码块在类加载时执行,并且在main方法执行之前执行

    5、静态代码块一般是按照自上而下的顺序执行

    6、静态代码块有啥作用,有什么用?

     具体的业务:
         项目经理说:所有的我们编写的程序中,只要是类加载了,请记录一下类加载的日志信息
         (在哪年哪月哪日几时几分,哪个类加载到JVM当中了) 就需要用到静态代码块

代码演练: 

public class staticTest04
{	
	// 静态代码块
	static{
		System.out.println("A");
	}
	// 一个类中可以编写多个静态代码块
	static{
		System.out.println("B");
	}
	static{
		System.out.println("C");
	}

	// 程序入口
	public static void main(String[] args){
		System.out.println("come on~");
		}

	// 再编写一个静态代码块
	static{
		System.out.println("D");
	}
}

运行结果:(结论:静态代码块在类加载的时候执行)

二、代码执行顺序    (结论:静态代码块1 和 静态代码块2 有先后顺序、静态代码块和静态变量也有先后顺序)

代码演示:

public class staticTest05
{	
	// 静态变量在什么时候初始化?	类加载时
	// 静态变量储存在哪里?	方法区
	static int i =100;
	
	// 静态代码块在什么时候执行?	类加载的时候
	static{
		System.out.println("i:"+i);
	}
	
	
	//程序入口
	public static void main(String[] args){
		System.out.println("main begin~");
	}	
}
三、this的内存结构

   1、this 是一个关键字,全部小写
    
    2、this 是什么,在内存方面是怎样的?
         一个对象一个this。
         this是一个变量,是一个引用,this保存当前对象的内存地址,指向自身,
         所以,严格意义上来说,this代表的就是"当前对象"
         
         this储存在堆内存当中的对象内部
    
    3、this只能使用在实例方法中,谁调用这个实例方法,this就是谁
         所以this代表的是:当前对象
    
    4、this 大部分情况下是可以省略的

    5、为什么this不能使用在静态方法中???
        this代表当前对象,静态方法当中不存在对象

代码演练:[掌握] 

public class ThisTest01
{	
	public static void main(String[] args){
	// new一个对象
	Customer c1 =new Customer("junker");
	Customer c2 =new Customer("jun");
	c1.shopping();
	c2.shopping();
	
	}
}




// 顾客类
class Customer
{
	// 属性
	// 实例变量(必须采用"引用."的方式访问)
	String name;

	// 构造方法
	public Customer(){
		
	}
	public Customer(String s){
		name =s;
	}

	// 顾客购物的方法
	// 实例方法
	public void shopping(){
		// 这里的this是谁呢? this是当前对象
		// c1调用shopping();,this就是c1
		// c2调用shopping();,this就是c2
		System.out.println(name+"正在购物");
		// 疑问: String name是实例变量(必须采用"引用."的方式访问)
		// 为什么这里的(name+"正在购物");不是(引用.name+"正在购物")呢 
		// 此程序中为什么不是(c1.name+"正在购物");呢?
		// [注意:new出来的引用c1 是在ThisTest01方法体 这个是Customer类体当中 ];

		// 解答:其实(name+"正在购物");当中隐藏了当前对象this (详看内存图)
		//System.out.println(this.name+"正在购物");
	}
}

扩展:


public class ThisTest02{	

	int i =100;	// 实例变量是对象级别的 必须先new对象才能使用
	static String k; 
	public static void main(String[] args){
		// 错误: 无法从静态上下文中引用非静态
		//System.out.println(this.i);
		//System.out.println(i);	// 同样报错 因为i是实例变量 而main方法是静态的
		
		// 如果真的想在main方法中访问实例变量 那么需要手动new一个对象
		ThisTest02 t =new ThisTest02();
		System.out.println(t.i);
		// 访问静态变量 "类名."的方式访问
		System.out.println(ThisTest02.k);
	}
}
this在哪些情况下不能省略:

public class ThisTest06
{
	public static void main(String[] args){
		// 无参
		Student s1 =new Student();
		s1.setName("jun");
		s1.setNo(11111);
		System.out.println(s1.getNo());
		System.out.println(s1.getName());
		// 有参
		Student s2 =new Student(22222,"junker");
		System.out.println("学号:"+s2.getNo());
		System.out.println("姓名"+s2.getName());

		
	}
}

// 分析以下代码哪些地方写的不好?
// 形参描述的不太清楚
class Student
{	
	private int no;
	private String name;
	
	// 构造器
	public Student(){}
	public Student(int no,String name){
		this.no =no;
		this.name =name;
	}
	
	

	// 构造方法
	public int getNo(){
		return no;
	}
	public void setNo(int no){	// 形参n表达的意思不太明显 有的不知道是no
		this.no =no;
	}

	public String getName(){
		return name;
	}
	public void setName(String name){
		this.name =name;
	}
}

运行结果:

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

新语法:通过当前的构造方法去调用另一个本类的构造方法,可以使用以下语法格式:
         this(实际参数列表);
         通过一个构造方法1去调用构造方法2,可以做到代码复用
         注意:"构造方法1"和"构造方法2"都是在同一类当中

 需求:
    1、定义一个日期类,可以表示年月日信息。
    2、需求中要求:
     如果调用无参数构造方法,默认创建的日期为:1970年1月1日。
     当然,除了调用无参数构造方法外,也可以调用有参数构造方法来创建日期对象。

public class ThisTest07
{
	public static void main(String[] args){
		// 无参数
		Date d1 =new Date();
		d1.doSome();
		// 有参数
		Date d2 =new Date(1949,3,6);
		d2.doSome();
	}
}

class Date
{
	private int year;
	private int month;
	private int day;
	
	
	// 无参数构造器
	public Date(){
		
		this(1970,1,1);
	}
	// 有参数构造器
	public Date(int year,int month,int day){
		this.year =year;
		this.month =month;
		this.day =day;
	}
	// 设立关卡
	// 读取数据
	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 setDay(int day){
		this.day =day;
	}
	
	public void doSome(){
		
		//System.out.println(year); // 本类当中封装无效 1970(有缺省构造器传进1970)
		// 没有构造器的时候结果输出int型为0 String-> null

		System.out.println(year+"年"+month+"月"+day+"日");
	}
}

 运行结果:

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/749052.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号