Java基础编程题目 您所在的位置:网站首页 用Java编写一个自我介绍 Java基础编程题目

Java基础编程题目

2024-07-11 00:33| 来源: 网络整理| 查看: 265

定义一个Person类,含姓名、性别、年龄、年龄等字段;继承Person类设计Teacher类,增加职称、部门等字段;继承Person类设计Student类,增加学号、入学时间、专业等字段。定义各类的构造方法和toString()方法,并分别创建对象进行测试。

public class Persons { public static void main(String[] args) { Teacher A = new Teacher("张三", "男", 35, "教授", "教学"); Student B = new Student("李四", "男", 20, 123, 201809, "computer"); System.out.println(A.toString()); System.out.println(B.toString()); } } class Person { private String name; private String sex; private int age; 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 getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } } class Teacher extends Person { String zhicheng; String bumen; public Teacher(String name, String sex, int age, String zhicheng, String bumen) { this.setName(name); this.setSex(sex); this.setAge(age); this.zhicheng = zhicheng; this.bumen = bumen; } public String toString() { return "姓名:" + this.getName() + ",性别:" + this.getSex() + ",年龄:" + this.getAge() + ",职称:" + this.zhicheng + ",部门:" + this.bumen; } } class Student extends Person { int num; int time; String zhuanye; public Student(String name, String sex, int age, int num, int time, String zhuanye) { this.setName(name); this.setSex(sex); this.setAge(age); this.num = num; this.time = time; this.zhuanye = zhuanye; } public String toString() { return "姓名:" + this.getName() + ",性别:" + this.getSex() + ",年龄:" + this.getAge() + ",学号:" + this.num + ",入学时间:" + this.time + ",专业:" + this.zhuanye; } }


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有