实现Java简单继承

面向对象练习-简单继承 

一、完成教师类的创建

说明:

id 代表身份证号

name 表示姓名

birth 表示出生日期

title 表示职称(讲师,副教授,教授等)

二、完成学生类的创建

说明:

major 代表专业

三、编写主类测试

四、使用继承的方法来实现类之间的关系设计。

代码实现:

 1 class Person{
 2     String id,name,birth;
 3     public Person() {
 4     }
 5     public Person(String id,String name,String birth) {
 6         this.id=id;
 7         this.name=name;
 8         this.birth=birth;
 9     }
10     public void setId(String id) {
11         this.id = id;
12     }
13     public void setName(String name) {
14         this.name = name;
15     }
16     public void setBirth(String birth) {
17         this.birth = birth;
18     }
19     public String getBirth() {
20         return birth;
21     }
22     public String getId() {
23         return id;
24     }
25     public String getName() {
26         return name;
27     }
28     public void showInfo() {
29         System.out.printf("name is: %s  ,id is: %s  ,birth is: %s,",name,id,birth);
30     }
31 }
32 class Student extends Person{
33     String major;
34     public Student() {
35         
36     }
37     public Student(String id,String name,String birth,String major) {
38         super(id, name, birth);
39         this.major=major;
40     }
41     public void setMajor(String major) {
42         this.major = major;
43     }
44     public String getMajor() {
45         return major;
46     }
47     @Override
48     public void showInfo() {
49         super.showInfo();
50         System.out.println("major is : "+major);
51     }
52 }
53 class Teacher extends Person{
54     String title;
55     public Teacher() {
56     }
57     public Teacher(String id,String name,String birth,String title) {
58         super(id, name, birth);
59         this.title=title;
60     }
61     public void setTitle(String title) {
62         this.title = title;
63     }
64     public String getTitle() {
65         return title;
66     }
67     @Override
68     public void showInfo() {
69         super.showInfo();
70         System.out.println("title is : "+title);
71     }
72 }
73 public class Main{
74 
75     public static void main(String[] args) {
76         Student stu=new Student("1000","张大","1998.01.23","Computer");
77         Student stu1=new Student();
78         stu1.setName("张三");
79         stu1.setBirth("1998.04.01");
80         stu1.setId("1001");
81         stu1.setMajor("Computer");
82         
83         Teacher teacher1=new Teacher("ls1001","李大","1977.01.01","Professor");
84         Teacher teacher=new Teacher();
85         teacher.setName("李四");
86         teacher.setBirth("1974.05.01");
87         teacher.setId("ls1002");
88         teacher.setTitle("Professor");
89         
90         stu.showInfo();
91         stu1.showInfo();
92         teacher1.showInfo();
93         teacher.showInfo();
94     }
95 
96 }

 

转载于:https://www.cnblogs.com/LJHAHA/p/10148340.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值