当前位置: 代码迷 >> Java相关 >> 各位帮忙改进下代码
  详细解决方案

各位帮忙改进下代码

热度:263   发布时间:2011-10-11 09:16:13.0
各位帮忙改进下代码
程序代码:

public class Demo_9 {
    public static void main(String[] args) {
        Teacher teacher = new Teacher(11111,"张三", "男", 25, "讲师");
        teacher.display();
        Student student = new Student(9922, "李四", "男", 21, "公寓");
        student.display();
    }

}

class Person{
    protected String name;
    protected String sex;
    protected int age;
}

class Teacher extends Person{
    protected int tnumbers;
    protected String title;
    public Teacher(int tnumbers, String name, String sex, int age, String title){
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.tnumbers = tnumbers;
        this.title = title;
    }
    public void display(){
        javax.swing.JOptionPane.showMessageDialog(null,"  职工号  " +tnumbers+ " 名字 " +name
                    + "  性别  " +sex
                    + "  年龄  " +age
                    + "  职称  " +title);
    }
}

class Student extends Person{
    protected int snumbers;
    protected String address;
    public Student(int snumbers, String name, String sex, int age, String address){
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.snumbers = snumbers;
        this.address = address;
    }
    public void display(){
        javax.swing.JOptionPane.showMessageDialog(null," 学号   " +snumbers
                    + " 名字   " +name
                    + "  性别  " +sex
                    + "  年龄  " +age
                    + "  家庭住址  " +address);
    }
}
就是我想把那个display也搞个继承,各位看看是否行得通
搜索更多相关的解决方案: 公寓  讲师  teacher  color  

----------------解决方案--------------------------------------------------------
把方法写到person里去

----------------解决方案--------------------------------------------------------
package Pack2;

public class Person {
    protected String Name;
    protected String Sex;
    protected int Age;
    public Person(String name,String sex,int age)
    {
        this.Name=name;
        this.Sex=sex;
        this.Age=age;
    }

}

package Pack2;

public class Student extends Person{
    protected int Snumbers;
    protected String Address;
    public Student(String name,String sex,int age,int snumbers,String address)
    {
        super(name,sex,age);
        this.Snumbers=snumbers;
        this.Address=address;
    }
    public void display(){
        javax.swing.JOptionPane.showMessageDialog(null," 学号   " +this.Snumbers
                    + " 名字   " +this.Name
                    + "  性别  " +this.Sex
                    + "  年龄  " +this.Age
                    + "  家庭住址  " +this.Address);

    }
}

package Pack2;

public class Test {
    public static void main(String[] agrs)
    {
        Student stu=new Student("张三","男",22,0000111,"中国");
        stu.display();
    }

}
也许这样更好一点吧!!仁者见仁,智者见智了!!仅作参考!!


----------------解决方案--------------------------------------------------------
你的继承没有写的完全,我改了一下,不对之处请不要见怪。

程序代码:
public class Demo_9 {
    public static void main(String[] args) {
        Teacher teacher = new Teacher(11111,"张三", "男", 25, "讲师");
        teacher.display();
        Student student = new Student(9922, "李四", "男", 21, "公寓");
        student.display();
    }

}

abstract class  Person{
    protected String name;
    protected String sex;
    protected int age;
    protected String personInfo;
    abstract String getPersonInfo();
    public void display(){
        javax.swing.JOptionPane.showMessageDialog(null,personInfo);
    }
}

class Teacher extends Person{
    protected int tnumbers;
    protected String title;
   

    public Teacher(int tnumbers, String name, String sex, int age, String title){
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.tnumbers = tnumbers;
        this.title = title;
        personInfo=getPersonInfo();
    }
    public String getPersonInfo(){
        return
          "  职工号  " +tnumbers+ " 名字 " +name
        + "  性别  " +sex
        + "  年龄  " +age
        + "  职称  " +title;
    }
   

}

class Student extends Person{
    protected int snumbers;
    protected String address;
    public Student(int snumbers, String name, String sex, int age, String address){
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.snumbers = snumbers;
        this.address = address;
        personInfo=getPersonInfo();
    }
    public String getPersonInfo(){
        return
          " 学号   " +snumbers
        + " 名字   " +name
        + "  性别  " +sex
        + "  年龄  " +age
        + "  家庭住址  " +address;
    }
}


----------------解决方案--------------------------------------------------------
回复 4楼 小恕
谢谢叻,我当时的目的就是想把display写到person里去,但是刚学实在不知道怎么弄,谢谢叻!
----------------解决方案--------------------------------------------------------
  相关解决方案