写的不错,可以作为大家的参考:
- class ClassTest
- {
- public static void main(String[] args)
- {
- String[] fWork = {"A","B"};
- String[] mWork = {"C","D"};
- Person f = new Person("红爸",new Person("红爷爷"),new Person("红奶奶"),fWork);
- Person m = new Person("红妈",new Person("红姥姥"),new Person("红姥爷"),mWork);
- Person d = new Person("小红",f,m);
- d.showPerson();
- // System.out.println("Hello World!");
- }
- }
- class Person
- {
- private String name;
- private Person father = null;
- private Person mother = null;
- private String[] work = null;
- Person()
- {
- }
- Person(String name)
- {
- this.name = name;
- }
- Person(String name,String[] work)
- {
- this.name = name;
- this.work = work;
- }
- Person(String name,Person father,Person mother)
- {
- this.name = name;
- this.father = father;
- this.mother = mother;
- }
- Person(String name,Person father,Person mother,String[] work)
- {
- this.name = name;
- this.father = father;
- this.mother = mother;
- this.work = work;
- }
- public void showPerson()
- {
-
- System.out.print("我是"+name);
- if (null != work)
- {
- System.out.print(",我会工作");
- for (int i = 0;i < work.length ;i++ )
- {
- System.out.print(work[i]);
- if (work.length - 1 != i)
- {
- System.out.print("、");
- }
- }
- }
- System.out.println();
- System.out.println();
- if (null != father)
- {
- System.out.println(name + "的爸爸:");
- father.showPerson();
- }
- if (null != mother)
- {
- System.out.println(name + "的妈妈:");
- mother.showPerson();
- }
-
- }
- public void setName(String name)
- {
- this.name = name;
- }
- public String getName()
- {
- return this.name;
- }
- public void setFather(Person father)
- {
- this.father = father;
- }
- public Person getFather()
- {
- return father;
- }
- public void setMother(Person mother)
- {
- this.mother = mother;
- }
- public Person getMother()
- {
- return mother;
- }
- }
复制代码 |