知行社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2602|回复: 0
收起左侧

各位帮忙改进下代码

[复制链接]
terkcov 发表于 2012-1-7 02:35 | 显示全部楼层 |阅读模式
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;
    }
}

QQ|小黑屋|手机版|知行技术社区 ( 湘ICP备11020288号-1 )

GMT+8, 2020-9-27 13:53 , Processed in 0.013946 second(s), 9 queries , Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表