Surpass Day7——Java this关键字


1.2.1 实例方法的访问(不带static的方法)

public class helloworld {

public static void main(String[] args) {

// 创建Customer对象

Customer c1 = new Customer();

c1.name = “zhangshan”;

// c1购物

c1.shopping();

// 创建Customer对象

Customer c2 = new Customer();

c2.name = “lisi”;

// c2购物

c2.shopping();

}

}

class Customer {

// 姓名

String name;

// 构造方法

public Customer() {

}

//实例方法

public void shopping(){

//当张三在购物的时候输出"张三在购物"

//当李四在购物的时候输出"李四在购物"

System.out.println(this.name + “在购物”);

}

}

1.2.2 实例变量的访问(不带static的变量)

public class helloworld {

public static void main(String[] args) {

//访问"当前对象"的num属性

//System.out.println(num); //编译错误(因为num是一个实例变量,需要"引用."的方式访问)

//如何访问num?

//创建一个对象

ThisTest tt = new ThisTest();

System.out.println(tt.num);

}

}

class ThisTest{

//实例变量

int num = 10;

}

1.3 静态方法


1)访问的时候采用 类名.方法名,不能用this;

2)静态方法不能"直接"访问实例变量和实例方法;(实例变量和实例方法都需要对象的存在);

3)静态方法中没有this,也就是说"当前对象"不存在,自然也无法访问当前对象的实例变量和实例方法(this代表的是当前正在执行这个动作的对象);

4)静态方法,既可以采用类名的方式访问,也可以采用应用的方式访问,但是即使采用引用的方式访问,实际上执行的时候和引用指向的对象无关,还是默认成“类名.”的方式访问,且会产生警告,但不会产生空指针异常;

1.4 特例:实例方法中实例方法的访问


实例方法和静态方法的访问 比较

public class ThisTest{

public static void main(String[] args){

//ThisTest.doSome();

//编译错误(实例方法必须先创建对象,通过引用.的方式访问)如下:

ThisTest tt = new ThisTest();

tt.run();

doSome();//带有static的方法,可以通过类名.的方式直接访问

}

public static void doSome(){

System.out.println(“do some!”);

}

public void doOther(){

System.out.println(“do other”);

}

//特例:实例方法中实例方法的访问

public void run(){

System.out.println(“run execute!”);

doOther();//this.doOther

//这里可以直接写“doOther();”由于是通过run()来访问到doOther的,此时this就是访问run()时的对象

}

}

1.4 实例方法和实例变量的定义和访问的比较


不带有static关键字的方法被称为"实例方法";

不带有static关键字的变量被称为"实例变量";

public class ThisTest{

public static void main(String[] args){

//编译错误

/*System.out.println(name);

doSome();

System.out.println(this.name);

this.doSome();*/

//编译通过

ThisTest tt = new ThisTest();

System.out.println(tt.name);

tt.doSome();

}

//实例变量

String name;

//实例方法

public void doSome(){

System.out.println(“do some!”);

}

}

1.5 this的作用领域


1)可以使用在实例方法中,代表当前对象【语法格式:this】

2)可以使用在构造方法中,通过当前的构造方法调用其他的构造方法【语法格式:this(实参)】;

3)this()这种语法只能出现在构造方法的第一行;

使用在构造方法中,通过当前的构造方法调用其他的构造方法的代码如下:

public class helloworld {

public static void main(String[] args) {

Date d = new Date();

System.out.println(d.getYear() + “-” +  d.getMonth() + “-” + d.getDay());

Date a = new Date(2021,1,3);

System.out.println(a.getYear() + “-” +  d.getMonth() + “-” + d.getDay());

}

}

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;

}

public Date() {

//需求:当程序员调用以下无参数的构造方法的时候,默认创建日期为1970.1.1

/*this.year = 1970;

this.month = 1;

this.day = 1;*/

//以上代码可以通过调用另一个构造方法来完成

//但前提是不能通过创新新的对象。以下代码表示创新了一个新的对象

//new Date(1970.1.1);

//需要采用以下的语法来完成构造方法的调用

this(1970,1,1);

}

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;

}

}

1.6 两种方法调用 以及 实例变量的访问 的完整/省略写法


public class Test{

//没有static的变量

int i = 10;

//带有static的方法

public static void doSome() {

System.out.println(“do some!”);

}

//没有static的方法

public void doOther() {

System.out.println(“do other!”);

}

//带有static的方法

public static void method1() {

//调用doSOme(带有static的方法)

//完整方法的调用

Test.doSome();

//省略方法的调用

doSome();

//调用doOther(没有static的方法)

//完整方法的调用

Test t = new Test();

t.doOther();

//省略方法的调用

//无

//访问i(实例参数)

//完整方法的调用

System.out.println(t.i);

//省略方法的调用

//无

}

//没有static的方法

public void method2() {

//调用doSOme(带有static的方法)

//完整方法的调用

Test.doSome();

//省略方法的调用

doSome();//this.doSome;

//调用doOther(没有static的方法)

//完整方法的调用

this.doOther();//调用的doOther中无static,且当前方法中也无static,当前方法有this,所以直接引用.即this.doOther()

//省略方法的调用

doOther();

//访问i(实例参数)

//完整方法的调用

System.out.println(this.i);

//省略方法的调用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值