继承

说明:

假如我们需要创建一个学生类和老师类,然后里面有许多属性,并实现一些日常生活中做的事情(这里仍然通过打印语句来实现)。代码实现如下:

 

  1 package com.hw.static0127;
  2 public class School{
  3     public static void main(String[] args){
  4         Teacher t1 = new Teacher("老师",'女',25,7-8,3000,"生理健康");
  5         Student s1 = new Student("辰宇",'男',18,7-6,2,99);
  6         t1.show1();
  7         t1.eat();
  8         s1.test();
  9         s1.show2();
 10     }
 11 }
 12 
 13 class Teacher{
 14     private String name;
 15     private char sex;
 16     private int age;
 17     private int birthday;
 18     private int salary;
 19     private String course;
 20     
 21     public Teacher(String name,char sex,int age,int birthday,int salary,String course){
 22         this.name = name;
 23         this.sex = sex;
 24         this.age = age;
 25         this.birthday = birthday;
 26         this.salary = salary;
 27         this.course = course;
 28     }
 29     
 30     public void setName(String name){
 31         this.name = name;
 32     }
 33     public String getName(){
 34         return name;
 35     }
 36     public void setSex(char sex){
 37         this.sex = sex;
 38     }
 39     public char getSex(){
 40         return sex;
 41     }
 42     public void setAge(int age){
 43         this.age = age;
 44     }
 45     public int getAge(){
 46         return age;
 47     }
 48     public void setBirthday(int birthday){
 49         this.birthday = birthday;
 50     }
 51     public int getBirthday(){
 52         return birthday;
 53     }
 54     public void eat(){
 55         System.out.println(getName()+"在吃饭");
 56     }
 57     public void show1(){
 58         System.out.println("老师的性别是"+getSex()+",教授的课程是"+getCourse());
 59     }
 60     
 61     public void setSalary(int salary){
 62         this.salary = salary;
 63     }
 64     public int getSalary(){
 65         return salary;
 66     }
 67     public void setCourse(String course){
 68         this.course = course;
 69     }
 70     public String getCourse(){
 71         return course;
 72     }
 73 }
 74 
 75 class Student{
 76     private String name;
 77     private char sex;
 78     private int age;
 79     private int birthday;
 80     private int rankNum;
 81     private double score;
 82     public Student(String name,char sex,int age,int birthday,int rankNum,double score){
 83         this.name = name;
 84         this.sex = sex;
 85         this.age = age;
 86         this.birthday = birthday;
 87         this.rankNum = rankNum;
 88         this.score = score;
 89     }
 90     
 91     public void setName(String name){
 92         this.name = name;
 93     }
 94     public String getName(){
 95         return name;
 96     }
 97     public void setSex(char sex){
 98         this.sex = sex;
 99     }
100     public char getSex(){
101         return sex;
102     }
103     public void setAge(int age){
104         this.age = age;
105     }
106     public int getAge(){
107         return age;
108     }
109     public void setBirthday(int birthday){
110         this.birthday = birthday;
111     }
112     public int getBirthday(){
113         return birthday;
114     }
115     public void setRankNum(int rankNum){
116         this.rankNum = rankNum;
117     }
118     public int getRankNum(){
119         return rankNum;
120     }
121     public void setScore(double score){
122         this.score = score;
123     }
124     public double getScore(){
125         return score;
126     }
127     public void test(){
128         System.out.println(getName()+"正在考试");
129     }
130     public void show2(){
131         System.out.println("学生的名字是"+getName()+",性别是"+getSex()+",排名是"+getRankNum());
132     }
133 }

 

来看看运行效果:

 

我们可以看到,这样写非常的麻烦,后期维护的时候,如果某个地方需要增删改查,不说修改代码,首先直接去看代码眼睛就晕了。这个时候,继承就派上用场了。

 


 

继承的作用:

如果我们使用继承来优化上面的代码,可以创建一个Humanity类,然后把学生和老师的一些共同属性放到里面去,可以达到优化代码的作用。代码实现如下:

  1 package com.hw.static0127;
  2 public class School{
  3     public static void main(String[] args){
  4         Teacher t1 = new Teacher("老师",'女',25,7-8,3000,"生理健康");
  5         Student s1 = new Student("辰宇",'男',18,7-6,2,99);
  6         t1.show1();
  7         t1.eat();
  8         s1.test();
  9         s1.show2();
 10     }
 11 }
 12 
 13 class Humanity {
 14     private String name;
 15     private char sex;
 16     private int age;
 17     private int birthday;
 18     
 19     public Humanity(String name,char sex,int age,int birthday){
 20         this.name = name;
 21         this.sex = sex;
 22         this.age = age;
 23         this.birthday = birthday;
 24     }
 25     public void setName(String name){
 26         this.name = name;
 27     }
 28     public String getName(){
 29         return name;
 30     }
 31     public void setSex(char sex){
 32         this.sex = sex;
 33     }
 34     public char getSex(){
 35         return sex;
 36     }
 37     public void setAge(int age){
 38         this.age = age;
 39     }
 40     public int getAge(){
 41         return age;
 42     }
 43     public void setBirthday(int birthday){
 44         this.birthday = birthday;
 45     }
 46     public int getBirthday(){
 47         return birthday;
 48     }
 49 }
 50 
 51 class Teacher extends Humanity{
 52     private int salary;
 53     private String course;
 54     
 55     public Teacher(String name,char sex,int age,int birthday,int salary,String course){
 56         super(name,sex,age,birthday);   //通过this访问当前类里面的成员 通过super访问父类里面的成员
 57         this.salary = salary;
 58         this.course = course;
 59     }
 60     
 61     public void eat(){
 62         System.out.println(getName()+"在吃饭");
 63     }
 64     public void show1(){
 65         System.out.println("老师的性别是"+getSex()+",教授的课程是"+getCourse());
 66     }
 67     
 68     public void setSalary(int salary){
 69         this.salary = salary;
 70     }
 71     public int getSalary(){
 72         return salary;
 73     }
 74     public void setCourse(String course){
 75         this.course = course;
 76     }
 77     public String getCourse(){
 78         return course;
 79     }
 80 }
 81 
 82 class Student extends Humanity{
 83     private int rankNum;
 84     private double score;
 85     public Student(String name,char sex,int age,int birthday,int rankNum,double score){
 86         super(name,sex,age,birthday);  //访问父类里面的这些成员变量,我就不需要在Student类里面再写一遍了
 87         this.rankNum = rankNum;
 88         this.score = score;
 89     }
 90     public void setRankNum(int rankNum){
 91         this.rankNum = rankNum;
 92     }
 93     public int getRankNum(){
 94         return rankNum;
 95     }
 96     public void setScore(double score){
 97         this.score = score;
 98     }
 99     public double getScore(){
100         return score;
101     }
102     public void test(){
103         System.out.println(getName()+"正在考试");
104     }
105     public void show2(){
106         System.out.println("学生的名字是"+getName()+",性别是"+getSex()+",排名是"+getRankNum());
107     }
108 }

运行效果如下:

 

 对于以上几点说明:在Humanity里面使用的构造方法含有参数,这样一来,在Teacher类和Student类里面,只需要super就可实现对父类里面的属性进行构造。当然,在构造方法处传参的时候还是要记得把所有的参数都弄过来,只不过共同属性通过super来完成。

另外,我们还可以通过super访问父类里面的成员.就是说,当父类有一个属性name,子类也有一个属性name的时候,就可以通过super.name;来调用父类里面的name.不仅如此,super还可访问父类里面的方法和构造方法。

posted @ 2021-01-28 16:41  EvanTheBoy  阅读(71)  评论(0)    收藏  举报