1、this是一个引用,this是一个变量,this变量中保存了内存地址指向了自身,this存储在JVM堆内存Java对象内部。
2、创建N个java对象,每一个对象都有this,也就是说有N个不同的this
3、this可以出现在“实例方法”当中,this指向当前正在执行这个动作的对象。(this代表当前对象)
4、this在多数情况下都可以省略不写
5、"this."什么时候不能省略?
当实例变量和局部变量同名的时候,用来区分实例变量和局部变量的时候,“this.”不能省略。
6、this不能在带有static的方法中使用
1)没有static关键字的方法被称为“实例方法”
2)没有static关键字的变量被称为“实例变量”
3)当一个行为/动作执行的过程当中是需要对象参与的,那么这个方法一定要定义为“实例方法”,不要带static关键字
7、this可以用在哪里?
1)可以使用在实例方法当中,代表当前对象【语法格式:this.】
2)可以使用在构造方法当中,通过当前的构造方法调用其他的构造方法【语法格式:this(实参);】
3)this()这种语法只能出现在构造函数的第一行。
【代码一】
public class Customer {
//姓名
String name;//实例变量:必须采用“引用.”的方式访问
//构造方法
public Customer(){
}
//不带有static关键字的一个方法
//顾客购物的行为
//每一个顾客购物最终的结果是不一样的
//所以购物这个行为是属于对象级别的行为
//由于每一个对象在执行购物这个动作的时候最终结果不同,所以购物这个动作必须有对象的参与
public void shopping(){
//由于name是一个实例变量,所以这个name访问的时候一定访问的是当前对象的name
//所以多数情况下“this.”是可以省略的
System.out.println(name + "在购物");
//完整写法
//System.out.println(this.name+"在购物");
}
//带有static
public static void doSome(){
//这个执行过程中没有“当前对象”,因为带有static的方法是通过类名的方式访问的
//或者说这个“上下文”当中没有当前对象,自然也不存在this(this代表的是当前正在执行这个动作的对象)
//以下程序为什么编译报错呢?
//doSome方法调用不是对象去调用,是一个类名去调用,执行过程中没有“当前对象”
//name是一个“实例变量”,以下代码的含义是:访问当前对象的name,没有当前对象,自然也不能访问当前对象的name
//System.out.println(name);
//static的方法调用不需要对象,直接使用类名,所以执行过程中没有当前对象
//System.out.println(this);
}
public static void doOther() {
//假设想访问name这个实例变量的话应该怎么做?
// System.out.println(name);//编译报错
//可以采用以下方案,但是以下方案,绝对不是访问的是当前对象的name
//创建对象
Customer c = new Customer();
System.out.println(c.name);//这里访问的name是c引用指向对象的name
}
}
public class CustomerTest {
public static void main(String[] args) {
//创建Customer对象
Customer c1 = new Customer();
c1.name = "张三";
//c1购物
c1.shopping();
//创建第二个Customer对象
Customer c2 = new Customer();
c2.name = "李四";
//c2购物
c2.shopping();
//调用doSome方法(修饰符列表上有static)
//采用“类名.”的方式访问,显然这个方法在执行的时候不需要对象的参与
Customer.doSome();
//调用doOther
Customer.doOther();
}
}
【代码二】
public class ThisTest {
//实例变量(引用.的方式访问)
int num = 10;
//带有static的方法
//JVM负责调用main方法,JVM是怎么调用的?
//ThisTest.main(String[])
public static void main(String[] args) {
//没有当前对象this
//以下代码什么意思?
//访问“当前对象”的num属性
//System.out.println(num);//编译错误
//System.out.println(this.num);//编译错误
//想访问num怎么办?
ThisTest tt = new ThisTest();
System.out.println(tt.num);
}
}
【代码三】
/**
* 最终结论:
* 在带有static的方法当中不能“直接”访问实例变量和实例方法
* 因为实例变量和实例方法都需要对象的存在
* 而static的方法当中是没有this的,也就是说当前对象是不存在的
* 自然也就无法访问当前对象的实例变量和实例方法
*/
public class ThisTest {
//带有static的方法
//主方法
public static void main(String[] args) {
//调用doSome方法
ThisTest.doSome();
//调用doSome方法
doSome();
//调用doOther方法
//【编译报错】
//ThisTest.doOther();//实例方法必须先创建对象,通过引用.的方式访问
//doOther()是实例方法
//实例方法调用必须有对象的存在
//以下代码表示的含义:调用当前对象的doOther方法
//但是由于main方法中没有this,所以以下方法不能调用
//doOther();
//this.doOther();//编译报错
ThisTest tt = new ThisTest();
tt.doOther();
tt.run();
}
//带有static的方法
public static void doSome(){
System.out.println("do some!");
}
//实例方法
public void doOther(){
//this表示当前对象
System.out.println("do other!");
}
//run是实例方法,调用run方法的一定是有对象存在的
//一定是先创建了一个对象才能调用run方法
public void run(){
//在大括号当中的代码执行的过程当中一定是存在“当前对象”的
//也就是说这里一定是有“this”的
System.out.println("run execute!");
//doOther是一个实例方法,实例方法调用必须有对象的存在
//以下代码表示的含义就是:调用当前对象的doOther方法
doOther();
//this.doOther();//完整写法
}
}
【代码四】
public class ThisTest {
//实例变量
String name;
//实例方法
public void doSome(){
System.out.println("do some!");
}
//带有static的方法
//主方法
public static void main(String[] args) {
//这里没有this
//编译错误
/*
System.out.println(name);
doSome();
System.out.println(this.name);
this.doSome();
*/
//通过
ThisTest tt = new ThisTest();
System.out.println(tt.name);
tt.doSome();
}
}
【代码五】
/**
* "this."什么时候不能省略?
* 当实例变量和局部变量同名的时候,用来区分实例变量和局部变量的时候,“this.”不能省略。
*
*/
//用户类
public class User {
//属性
private int id;
private String name;
//构造函数
public User(){
}
public User(int id,String name){
this.id = id;
this.name = name;
}
//setter and getter
public int getId() {
return id;
}
public void setId(int id) {
//此时this不能省略,用来区分实例变量和局部变量
//id = id
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class UserTest {
public static void main(String[] args) {
User u1 = new User(1234,"张三");
System.out.println(u1.getId());
System.out.println(u1.getName());
//想修改id
u1.setId(2345);
System.out.println(u1.getId());
}
}
【代码六】
/**
* 自定义的日期类型
*
* this可以用在哪里?
* 1、可以使用在实例方法当中,代表当前对象【语法格式:this.】
* 2、可以使用在构造方法当中,通过当前的构造方法调用其他的构造方法【语法格式:this(实参);】
*
* 【重点】
* this()这种语法只能出现在构造函数的第一行。
*/
public class Date {
//属性
private int year;
private int month;
private int day;
//构造方法
public Date(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
this.printDate();
}
/**
* 需求:当程序员调用以下无参数的构造方法的时候,默认创建的日期是1970-1-1
*/
public Date() {
//System.out.println();//编译报错
/*
this.year = 1970;
this.month = 1;
this.day = 1;
this.printDate();
*/
//以上代码可以通过调用另一个构造方法来完成
//但前提是不能创建新的对象,以下代码表示创建了一个全新的对象
//new Date(1970,1,1);
//需要采用以下语法来完成构造方法的调用
//这种方式不会创建新的java对象,但同时又可以达到调用其他的构造方法
this(1970,1,1);
}
//setter and getter
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 printDate(){
System.out.println("系统默认时间为:" + this.year+"-"+this.month+"-"+this.day);
}
}
public class DateTest {
public static void main(String[] args) {
Date date1 = new Date();
Date date = new Date(2021,3,23);
}
}
【代码七】
public class ThisTest {
//带有static的方法
public static void method1(){
//调用doSome
//使用完整方式调用
ThisTest.doSome();
//使用省略方式调用
doSome();
//调用doOther
//使用完整方式调用
ThisTest tt = new ThisTest();
tt.doOther();
//使用省略方式调用
//访问i
//完整方式访问
System.out.println(tt.i);
//省略方式访问
}
//没有static的方法
public void method2(){
//调用doSome
//使用完整方式调用
ThisTest.doSome();
//使用省略方式调用
doSome();
//调用doOther
//使用完整方式调用
this.doOther();
//使用省略方式调用
doOther();
//访问i
//完整方式访问
System.out.println(this.i);
//省略方式访问
System.out.println(i);
}
//主方法
public static void main(String[] args) {
//要求在这里编写程序调用method1
//使用完整方式调用
ThisTest.method1();
//使用省略方式调用
method1();
//要求在这里编写程序调用method2
//使用完整方式调用
ThisTest tt = new ThisTest();
tt.method2();
//使用省略方式调用
//访问i
//完整方式访问
System.out.println(tt.i);
//省略方式访问
}
//没有static的变量
int i;
//带有static的方法
public static void doSome(){
System.out.println("do some!");
}
//没有static的方法
public void doOther(){
System.out.println("do other!");
}
}
【代码八】
public class ThisTest {
//主方法
public static void main(String[] args) {
ThisTest.doSome();
doSome();
ThisTest tt = new ThisTest();
tt.doSome();
//引用是空
tt = null;
//带有static的方法,其实可以采用类名的方式访问,也可以采用引用的方式访问
//但是即使采用引用的方式去访问,实际上执行的时候和引用指向的对象无关
//但是建议带有static的方法还是使用“类名.”的方式访问
tt.doSome();//这里不会出现空指针异常
}
//没有static的变量
int i;
//带有static的方法
public static void doSome(){
System.out.println("do some!");
}
//没有static的方法
public void doOther(){
System.out.println("do other!");
}
}