| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 745 人关注过本帖
标题:各位帮忙改进下代码
只看楼主 加入收藏
Heart→M鹏
Rank: 2
等 级:论坛游民
帖 子:80
专家分:94
注 册:2011-5-13
结帖率:83.33%
收藏
已结贴  问题点数:0 回复次数:4 
各位帮忙改进下代码
程序代码:
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 
2011-10-11 09:16
husiwen
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:2
帖 子:227
专家分:1125
注 册:2010-5-23
收藏
得分:1 
把方法写到person里去
2011-10-11 23:05
ninghz
Rank: 2
等 级:论坛游民
帖 子:34
专家分:34
注 册:2011-7-8
收藏
得分:1 
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();
    }

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


程序改变生活
2011-10-11 23:14
小恕
Rank: 1
等 级:新手上路
帖 子:2
专家分:1
注 册:2011-10-13
收藏
得分:1 
你的继承没有写的完全,我改了一下,不对之处请不要见怪。

程序代码:
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;
    }
}

2011-10-13 17:49
Heart→M鹏
Rank: 2
等 级:论坛游民
帖 子:80
专家分:94
注 册:2011-5-13
收藏
得分:0 
回复 4楼 小恕
谢谢叻,我当时的目的就是想把display写到person里去,但是刚学实在不知道怎么弄,谢谢叻!
2011-10-14 11:27
快速回复:各位帮忙改进下代码
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.037720 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved