| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 780 人关注过本帖
标题:新手,明天考试了
只看楼主 加入收藏
whys
Rank: 1
等 级:新手上路
帖 子:50
专家分:8
注 册:2009-10-27
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
新手,明天考试了
刚学这方面的,很多基本的都不会,对这个题目没信心,晚上10点来核对答案,我不是不动手,现在正在做着,只是为保明天考试能通过,麻烦各位给出下面的题目正确答案,谢谢

编程:
1.    编写Person类,该类包含:
四个受私有属性:人的姓名name、年龄age、地址address和电话tel。
一个构造器方法:用于初始化name、 age和address属性。
两个公有成员方法:分别实现人基本信息的输出和设置电话。
2.编写学生类Student继承于Person类
  增加成员变量学校school、学号id
  重写父类输出方法,将学生所有信息输出。
  实例化一个对象,并输出其信息。
3.扩展:编写教师类Teacher继承于Person类
  增加成员变量学校school、编号id、职称。
搜索更多相关主题的帖子: 考试 
2010-05-12 16:51
whys
Rank: 1
等 级:新手上路
帖 子:50
专家分:8
注 册:2009-10-27
收藏
得分:0 


程序代码:
class Person {
   String name;
   int age;
   String address;
   int tel;
}
class Student extends Person{
    String school;
    int id;
}
class Teacher extends Person{
    String school;
    int id;
    String career;
}
public class test{
    public static void main(String args[]){
        Student student=new Student();
        student.name="张三";
        student.age=20;
        student.address="北京";
        student.tel=888168;
        student.school="清华";
        student.id=88;
        System.out.println(student.name);
        System.out.println(student.age);
        System.out.println(student.address);
        System.out.println(student.tel);
        System.out.println(student.school);
        System.out.println(student.id);
        Teacher teacher=new Teacher();
        teacher.name="李四";
        teacher.age=29;
        teacher.address="北京";
        teacher.tel=123456;
        teacher.school="清华";
        teacher.id=22;
        teacher.career="教师";
        System.out.println(teacher.name);
        System.out.println(teacher.age);
        System.out.println(teacher.address);
        System.out.println(teacher.tel);
        System.out.println(teacher.school);
        System.out.println(teacher.id);
        System.out.println(teacher.career);
    }
}

 
2010-05-13 00:26
lampeter123
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:54
帖 子:2508
专家分:6424
注 册:2009-1-30
收藏
得分:20 
程序代码:
class Person {
    private String name;
    private int age;
    private String address;
    private int tel;
   
    public Person() {

    }
   
    public Person(String name, int age, String address, int tel) {
        this.name = name;
        this.age = age;
        this.address = address;
        this.tel = tel;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getTel() {
        return tel;
    }
   
    public void setTel(int tel) {
        this.tel = tel;
    }

    @Override
    public String toString() {
        return "姓名:"+ name + " 年龄:" + age + " 地址:" + address + " 电话:" + tel;
    }
   
   
   
}

class Student extends Person {
   
    private String school;
    private int id;
   
    public Student() {
        super();
    }
   
    public Student(String name, int age, String address, int tel,
            String school, int id) {
        super(name, age, address, tel);
        this.school = school;
        this.id = id;
    }

    public String getSchool() {
        return school;
    }

    public void setSchool(String school) {
        this.school = school;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return super.toString() + " 学校:" + school + " id:" + id;
    }
   
   
}

class Teacher extends Person {
    private String school;
    private int id;
    private String career;
   
    public Teacher() {
        super();
    }
   
    public Teacher(String name, int age, String address, int tel,
            String school, int id, String career) {
        super(name, age, address, tel);
        this.school = school;
        this.id = id;
        this.career = career;
    }
    public String getSchool() {
        return school;
    }
    public void setSchool(String school) {
        this.school = school;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getCareer() {
        return career;
    }
    public void setCareer(String career) {
        this.career = career;
    }

    @Override
    public String toString() {
        return super.toString() +  " 学校:" + school + " id:" + id + " 职位:" + career;
    }
   
   
}

public class Test {
    public static void main(String args[]) {

        Student student = new Student("张三",20,"北京",888168,"清华",88);
        System.out.println(student);
        Teacher teacher = new Teacher("李四",29,"北京",123456,"清华",22,"教师");
        System.out.println(teacher);
       
    }
}
帮你修改了代码,按JAVABEAN规范写的,祝你考试成功

你的优秀和我的人生无关!!!!
    
    我要过的,是属于我自己的生活~~~
2010-05-13 08:41
skypz
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2010-6-13
收藏
得分:0 
怎么感觉我也做个这个题啊?

2010-06-13 18:27
快速回复:新手,明天考试了
数据加载中...
 
   



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

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