class Person{
private String person_name ;
private int person_age;
private Car car;
private Person child ;
public Person(String person_name,int person_age){
this.person_name = person_name ;
this.person_age = person_age ;
}
public void setChild(Person child){
this.child = child ;
}
public Person getChild(){
return child ;
}
public void setCar(Car car){
this.car = car ;
}
public Car getCar(){
return this.car ;
}
public void fun(Person temp){
temp.person_name = "Jiangchanghong" ;
temp.person_age = 23 ;
}
public String getPerson_name(){
return this.person_name ;
}
public int getPerson_age(){
return this.person_age ;
}
}
class Car{
private String car_name ;
private String car_color;
private Person person;
public Car(String car_name,String car_color){
this.car_name = car_name ;
this.car_color = car_color ;
}
public void setPerson(Person person){
this.person = person ;
}
public Person getPerson(){
return this.person ;
}
public void fun(Car temp){
temp.car_name = "Nissan" ;
temp.car_color = "black" ;
}
public String getCar_name(){
return this.car_name ;
}
public String getCar_color(){
return this.car_color ;
}
}
public class function{
public static void main(String args[]){
Person person = new Person("123",123);
System.out.println(person.getPerson_name()+"、"+person.getPerson_age());
Car car = new Car("456","456");
System.out.println(car.getCar_name()+"、"+car.getCar_color());
System.out.println("我是分隔符---------------------------------");
person.fun(person);
System.out.println(person.getPerson_name()+"、"+person.getPerson_age());
car.fun(car);
System.out.println(car.getCar_name()+"、"+car.getCar_color());
person.setCar(car);
System.out.println(person.getPerson_name()+"拥有"+person.getCar().getCar_name());
car.setPerson(person);
System.out.println(car.getCar_name()+"属于"+car.getPerson().getPerson_name());
System.out.println("我是分隔符---------------------------------");
Person child = new Person("Wuyilan",23) ;
System.out.println(person.getPerson_name()+"、"+person.getPerson_age());
Car car_child = new Car("Beatles","White") ;
System.out.println(car.getCar_name()+"、"+car.getCar_color());
person.setChild(child);
System.out.println(person.getChild().getPerson_name()+":"+person.getChild().getPerson_age());
child.setCar(car);
System.out.println(child.getCar().getCar_name()+":"+child.getCar().getCar_color());
child.setCar(car_child);
System.out.println(child.getCar().getCar_name()+":"+child.getCar().getCar_color());
car.setPerson(child);
// 这里该怎么表示,运行结果报空指针System.out.println(car.getPerson().getChild().getPerson_name()+":"+car.getPerson().getChild().getPerson_age());
car_child.setPerson(child);
// 这里该怎么表示,运行结果报空指针System.out.println(car_child.getPerson().getChild().getPerson_name()+":"+car_child.getPerson().getChild().getPerson_age());
}
}; |