Java入门——day45

1.人与学生

  • 设计一个类people,有保护数据成员:age(年龄,整型)name(姓名,string),行为成员:两个构造函数(一个默认,另一个有参数);void setValue(int m, string str)agename赋值;void display()输出agename

  • 设计一个学生类student,公有继承类people,有私有成员:ID(学号,整型),行为成员:两个构造函数(一个默认,另一个有参数);void setID(int m)ID赋值;void displayID()输出ID

  • main函数定义学生对象,给对象初始化赋值或调用setValue()setID()赋值,并输出学生的信息。

 1 //People类
 2 public class People {
 3     protected int age;      //年龄
 4     protected String name;  //姓名
 5     public People() {
 6     }
 7     public People(int age, String name) {
 8         this.age = age;
 9         this.name = name;
10     }
11     public void setValue(int m,String str) {
12         this.age=m;
13         this.name=str;
14     }
15     public void display() {
16         System.out.println("age:"+age);
17         System.out.println("name:"+name);
18     }
19 }
 1 //Student类
 2 public class Student extends People{
 3     private int ID;  //学号
 4     public Student() {
 5         super();
 6     }
 7     public Student(int age,String name,int ID) {
 8         super(age, name);
 9         this.ID=ID;
10     }
11     public void setID(int m) {
12         this.ID=m;
13     }
14     public void displayID() {
15         System.out.println("ID:"+ID);
16     }
17     public static void main(String[] args) {
18         Student s=new Student(18,"李华",2020);
19         s.display();
20         s.displayID();
21     }
22 }


 2.多继承

 学生具有姓名,班级,学号等属性,有上课等行为;教师具有工号,工资等属性,有教课等行为;助教既是学生,又是老师,具有学生和老师的双重属性。请用类的多继承机制实现上述问题。

 1 //学生类
 2 public class Student {
 3     private String name;  //姓名
 4     private int room;     //班级
 5     private int ID;       //学号
 6     public Student() {
 7     }
 8     public Student(String name,int room,int ID) {
 9         this.name=name;
10         this.room=room;
11         this.ID=ID;
12     }
13     public void setValue(String name,int room,int ID) {
14         this.name=name;
15         this.room=room;
16         this.ID=ID;
17     }
18     public void display1() {
19         System.out.println("姓名:"+name);
20         System.out.println("班级:"+room);
21         System.out.println("学号:"+ID);
22         System.out.println("上课");
23     }
24 }
 1 //教师类
 2 public class Teacher {
 3     private int num;  //工号
 4     private int pay;  //工资
 5     public Teacher() {
 6     }
 7     public Teacher(int num,int pay) {
 8         this.num=num;
 9         this.pay=pay;
10     }
11     public void setValue(int num,int pay) {
12         this.num=num;
13         this.pay=pay;
14     }
15     public void display2() {
16         System.out.println("工号:"+num);
17         System.out.println("工资:"+pay);
18         System.out.println("教课");
19     }
20 }
 1 //助教类
 2 public class Assistant {
 3     private class MyStudent extends Student{
 4     }
 5     private class MyTeacher extends Teacher{
 6     }
 7     private MyStudent s=new MyStudent();
 8     private MyTeacher t=new MyTeacher();
 9     public void output1() {
10         s.setValue("Wang",1907,20194024);
11         s.display1();
12     }
13     private void output2() {
14         t.setValue(305,6000);
15         t.display2();
16     }
17     public static void main(String[] args) {
18         Assistant a=new Assistant();
19         a.output1();
20         a.output2();
21     }
22 }

 注意:Java类与类之间只可以单继承,要想实现多继承可以有三种方法:多层继承、内部类和接口。

原文地址:https://www.cnblogs.com/znjy/p/13529008.html