-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththisPoint.java
More file actions
51 lines (47 loc) · 994 Bytes
/
Copy paththisPoint.java
File metadata and controls
51 lines (47 loc) · 994 Bytes
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
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
this:用于区分局部变量和成员变量同名的情况
this代表本类对象
哪个对象在调用this所在的函数,this就代表哪个对象
*/
class Person
{
private int age;
private String name;
Person()
{
age = 20;
System.out.println("构造函数");
}
Person(String name)
{
this.name = name;
System.out.println("A:name="+name);
}
Person(String s,int a)
{
name = s;
age = a;
System.out.println("A:name="+name+",age="+age);
}
//this的应用:当定义类中功能时,该函数内部要用到调用该函数的对象时
// 这是用this来表示这个对象
// 本类功能内部使用到了本类对象,都用this表示
public boolean cpmpare(Person p)
{
return this.age == p.age; //p1调用是,this代表p1,p代p2
}
}
class PersonDemo3
{
public static void main(String[] args)
{
Person p1 = new Person(20);
Person p2 = new Person(23);
boolean b = p1.compare(p2);
System.out.println(b);
// Person p = new Person();
// new Person();
// Person p1 = new Person("Jack");
// Person p2 = new Person("forsn");
}
}