public class Person {
public String name;
public char sex;
public Person father;
public Person mother;
public static final String twoBlanks=" ";
public static String tab="" ;
public Person(String name,char sex){
this.name=name;
this.sex=sex;
}
public void setMother(Person mother){
this.mother=mother;
}
public void setFather(Person father){
this.father=father;
}
public String toString(){
String s=new String(name+"("+sex+")") ;
s+="\n" ;
if(father!=null){
tab+=twoBlanks;
s+=tab +"father:" +father;
* tab=tab.substring(2);
}
if(mother!=null) {
tab+=twoBlanks;
s+=tab +"mother" +mother;
* tab=tab.substring(2);
}
return s;
}
}public class TestPerson {
public static void main(String args[] ) {
Person sx=new Person("Steven" ,'M' ) ;
Person bx=new Person("Bob" ,'M' ) ;
Person mc=new Person("Mary" ,'F' ) ;
Person sc=new Person("Smith" ,'M' ) ;
Person kw=new Person("Kate" ,'F' ) ;
Person lq=new Person("Lucy" ,'F' ) ;
Person lw=new Person("Lee" ,'M' ) ;
sx.setFather(bx) ;
sx.setMother(mc);
bx.setFather(sc);
bx.setMother(kw);
mc.setMother(lq);
kw.setFather(lw);
System.out.println(sx) ;
}
}
以上是输出家族中的关系的代码
输出如下
Steven(M)
father:Bob(M)
father:Smith(M)
motherKate(F)
father:Lee(M)
motherMary(F)
motherLucy(F)
问题是*行应该如何理解呢?
执行过*行后不是tab等于空字符了吗?
那又何来的 像输出那样的格式呢?
比较费解,望各位耐心解答,先谢谢了!
public String name;
public char sex;
public Person father;
public Person mother;
public static final String twoBlanks=" ";
public static String tab="" ;
public Person(String name,char sex){
this.name=name;
this.sex=sex;
}
public void setMother(Person mother){
this.mother=mother;
}
public void setFather(Person father){
this.father=father;
}
public String toString(){
String s=new String(name+"("+sex+")") ;
s+="\n" ;
if(father!=null){
tab+=twoBlanks;
s+=tab +"father:" +father;
* tab=tab.substring(2);
}
if(mother!=null) {
tab+=twoBlanks;
s+=tab +"mother" +mother;
* tab=tab.substring(2);
}
return s;
}
}public class TestPerson {
public static void main(String args[] ) {
Person sx=new Person("Steven" ,'M' ) ;
Person bx=new Person("Bob" ,'M' ) ;
Person mc=new Person("Mary" ,'F' ) ;
Person sc=new Person("Smith" ,'M' ) ;
Person kw=new Person("Kate" ,'F' ) ;
Person lq=new Person("Lucy" ,'F' ) ;
Person lw=new Person("Lee" ,'M' ) ;
sx.setFather(bx) ;
sx.setMother(mc);
bx.setFather(sc);
bx.setMother(kw);
mc.setMother(lq);
kw.setFather(lw);
System.out.println(sx) ;
}
}
以上是输出家族中的关系的代码
输出如下
Steven(M)
father:Bob(M)
father:Smith(M)
motherKate(F)
father:Lee(M)
motherMary(F)
motherLucy(F)
问题是*行应该如何理解呢?
执行过*行后不是tab等于空字符了吗?
那又何来的 像输出那样的格式呢?
比较费解,望各位耐心解答,先谢谢了!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货