1. 先看一段代码,并分析问题
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
public class this01 { //编写一个main方法 public static void main(string[] args) { dog dog1 = new dog( "大壮" , 3 ); //dog1调用了 info()方法 dog1.info(); } } class dog{ //类 string name; int age; // public dog(string dname, int dage){//构造器 // name = dname; // age = dage; // } //如果我们构造器的形参,能够直接写成属性名,就更好了 //但是出现了一个问题,根据变量的作用域原则 //构造器的name 是局部变量,而不是属性 //构造器的age 是局部变量,而不是属性 //==> 引出this关键字来解决 public dog(string name, int age){ //构造器 //this.name 就是当前对象的属性name this .name = name; //this.age 就是当前对象的属性age this .age = age; } public void info(){ //成员方法,输出属性x信息 system.out.println(name + "\t" + age + "\t" ); } } |
2. 深入理解 this
为了进一步理解 this,我们再看一个案例 (this01.java)
使用hashcode()
看看对象的情况
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
public class this01 { //编写一个main方法 public static void main(string[] args) { dog dog1 = new dog( "大壮" , 3 ); system.out.println( "dog1的hashcode=" + dog1.hashcode()); //dog1调用了 info()方法 dog1.info(); system.out.println( "============" ); dog dog2 = new dog( "大黄" , 2 ); system.out.println( "dog2的hashcode=" + dog2.hashcode()); dog2.info(); } } class dog{ //类 string name; int age; public dog(string name, int age){ //构造器 //this.name 就是当前对象的属性name this .name = name; //this.age 就是当前对象的属性age this .age = age; system.out.println( "this.hashcode=" + this .hashcode()); } public void info(){ //成员方法,输出属性x信息 system.out.println( "this.hashcode=" + this .hashcode()); system.out.println(name + "\t" + age + "\t" ); } } |
3. this 的注意事项和使用细节
thisdetail.java
- this 关键字可以用来访问本类的属性、方法、构造器
- this 用于区分当前类的属性和局部变量
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public class thisdetail { public static void main(string[] args) { t t = new t(); t.f3(); } } class t{ string name = "兮动人" ; int num = 10 ; //this关键字可以用来访问本类的属性 public void f3(){ string name = "smith" ; //传统方式 system.out.println( "name=" + name + " num=" + num); //smith 100 //也可以使用this访问属性 system.out.println( "name=" + this .name + " num=" + this .num); //jack 100 } } |
访问成员方法的语法:this.方法名(参数列表);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
public class thisdetail { public static void main(string[] args) { t t1 = new t(); t.f2(); } } class t { public void f1(){ system.out.println( "f1()方法..." ); } public void f2(){ system.out.println( "f2()方法..." ); //调用本类的 f1 //第一种方式 f1(); //第二种方式 this .f1(); } } |
访问构造器语法:this(参数列表);
注意只能在构造器中使用(即只能在构造器中访问另外一个构造器, 必须放在第一条语句)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
public class thisdetail { public static void main(string[] args) { t t2 = new t(); } } class t{ /* 细节: 访问构造器语法:this(参数列表); 注意只能在构造器中使用(即只能在构造器中访问另外一个构造器) 注意: 访问构造器语法:this(参数列表); 必须放置第一条语句 */ public t(){ //这里去访问 t(string name,int age)构造器,必须放在第一行 this ( "jack" , 23 ); system.out.println( "t()构造器" ); } public t(string name, int age){ system.out.println( "t(string name,int age)构造器" ); } } |
this 不能在类定义的外部使用,只能在类定义的方法中使用。
4. this 的案例
testperson.java
定义 person 类,里面有 name、age 属性,并提供 compareto 比较方法,用于判断是否和另一个人相等,提供测试类 testperson 用于测试, 名字和年龄完全一样,就返回 true, 否则返回 false
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
public class testperson { //编写一个main方法 public static void main(string[] args) { person p1 = new person( "mary" , 20 ); person p2 = new person( "mary" , 20 ); system.out.println( "p1和p2比较的结果=" + p1.compareto(p2)); } } /* 定义person类,里面有name、age属性,并提供compareto比较方法, 用于判断是否和另一个人相等,提供测试类testperson用于测试, 名字和年龄完全一样,就返回true, 否则返回false */ class person { string name; int age; //构造器 public person(string name, int age) { this .name = name; this .age = age; } //compareto比较方法 public boolean compareto(person p) { //名字和年龄完全一样 // if(this.name.equals(p.name) && this.age == p.age) { // return true; // } else { // return false; // } return this .name.equals(p.name) && this .age == p.age; } } |
把名字或年龄改成其他不同数据
到此这篇关于java this关键字的使用详解的文章就介绍到这了,更多相关java this内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://xdr630.blog.csdn.net/article/details/120940483