//仅作为学习记录
/*
this 指向内幕的对象
*/
class PersonDemo
{
public static void main(String[] args)
{
Person p = new Person("lisi",20);
}
}
class Person
{
private String name;
private int age;
Person(String name , int age){
this.name = name;//this 关键字用法
this.age = age;//用点号 不是C++中的箭头
//System.out.println("A: name :" + name +" , Age:" + age);
System.out.println("A: name :" + this.name +" , Age:" + this.age);//这句和上句是一样的
}
}
class PersonDemo
{
public static void main(String []args){
Person p1 = new Person("p1",20);
Person p2 = new Person("p2", 23);
if(p1.compare(p2))
System.out.println("p1>p2");
else
System.out.println("p1<=p2");
}
}
class Person
{
private String name;
private int age;
Person(String name,int age){
this.name = name;
this.age = age;
}
//比较二者的年纪
public boolean compare(Person p){
return this.age == p.age;
}
}
/*
构造函数间调用只能使用this 关键字
比如一个程序中有多个可以用于初始化的构造函数 但是只对外提供一个 此时就需要this
*/
class PersonDemo
{
public static void main(String []args){
Person p = new Person(" zhangsan",20);
p.show();
}
}
class Person
{
private String name;
private int age;
Person(String name){
this.name = name;
}
Person(String name,int age){
//this.name = name;
this(name);//相当于对内幕对象进行赋值
this.age = age;
}
void show(){
System.out.println("Name = "+ this.name + "; age=" + this.age);
}
}