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等于空字符了吗?
那又何来的 像输出那样的格式呢?
比较费解,望各位耐心解答,先谢谢了!
表示截取tab从第3个字符开始到最后的字符串,并重新赋值给tab
并不是空字符串
修改了一下public class Person {
public String name;
public char sex;
public Person father;
public Person mother;
public static String indent = " "; 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() {
return toString(0,"");
}
public String toString(int level, String prefix) {
String s = "";
for(int i=0; i<level; i++) s += indent;
s += prefix+name + "(" + sex + ")\n";
if (father != null) {
s += father.toString(level+1, "father:");
}
if (mother != null) {
s += mother.toString(level+1, "mother:");
}
return s;
}
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) ;
System.out.println("========================");
System.out.println(bx) ;
}
}
运行结果:
Steven(M)
father:Bob(M)
father:Smith(M)
mother:Kate(F)
father:Lee(M)
mother:Mary(F)
mother:Lucy(F)========================
Bob(M)
father:Smith(M)
mother:Kate(F)
father:Lee(M)
father:Bob(M)
father:Smith(M)
mother:Kate(F)
father:Lee(M)
mother:Mary(F)
mother:Lucy(F)========================
Bob(M)
father:Smith(M)
mother:Kate(F)
father:Lee(M)