class Person
{
String name ;
int age ;
public Person(String name,int age)
{
this.name = name ;
this.age = age ;
}
public void print()
{
System.out.println("print方法中的:"+this) ;
}
};
public class OODemo17
{
public static void main(String args[])
{
Person p = new Person("张三",30) ;
System.out.println("main方法中的:"+p) ;
p.print() ;
System.out.println("------------------------") ;
Person p1 = new Person("李四",33) ;
System.out.println("main方法中的:"+p1) ;
p1.print() ;
}
};
class Person
{
private String name ;
private int age ;
public Person(String name,int age)
{
this.name = name ;
this.age = age ;
}
public void change(Person p)
{
p.name = "李四" ;
p.age = 50 ;
}
public void print()
{
System.out.println("姓名:"+this.name) ;
System.out.println("年龄:"+this.age) ;
}
};
public class OODemo18
{
public static void main(String args[])
{
Person p = new Person("张三",30) ;
p.change(p) ;
p.print() ;
}
};
class Person
{
private String name ;
private int age ;
public Person(String name,int age)
{
this.name = name ;
this.age = age ;
}
public boolean compare(Person p1)
{
boolean flag = false ;
Person p2 = this ;
if(p1.name.equals(p2.name)&&p1.age==p2.age)
{
flag = true ;
}
return flag ;
}
public void print()
{
System.out.println("姓名:"+this.name) ;
System.out.println("年龄:"+this.age) ;
}
};
public class OODemo19
{
public static void main(String args[])
{
// 假设认为,名字和年龄相等的就是同一个人
Person per1 = new Person("张三",30) ;
Person per2 = new Person("张三",31) ;
System.out.println(per1.compare(per2)?"是同一个人":"不是同一个人") ;
}
};