【第十四章】继承案例分析:
一.案例分析类:
建立一个人类和学生类,功能要求如下:
(1)Person中包含四个私有型的数据成员name,addr,sex,age,分别为字符串型,字符串型,字符型和整型。
一个四参构造,一个俩参构造,一个无参构造,一个输出方法显示四种属性。
(2)Student继承Person类,并增加成员math,English存放数学和英语成绩。一个六参构造方法,一个俩参构造方法,一个无参构造方法和重写输出方法用于显示六种属性。
class Person { private String name; private String addr; private char sex; private int age; public Person(){} public Person(String name ,String addr){ this(name,addr,'男',0); } public Person(String name, String addr, char sex, int age){ this.name = name; this.addr = addr; this.sex = sex; this.age = age; } public String getInfo(){ return "姓名:"+this.name + "\t地址:"+this.addr+ "\t性别:"+this.sex + "\t年龄:"+this.age; } } class Student extends Person { private double math; private double english; public Student(String name ,String addr){ super(name,addr); } public Student(String name,String addr,char sex,int age,double math,double english){ super(name,addr,sex,age); this.math = math; this.english = english; } public String getInfo(){ return super.getInfo()+ "\t数学成绩:"+this.math + "\t英语成绩:"+this.english; } } public class Tsy { public static void main(String[] args) { Student stu = new Student("同思远","白宫",'男',19,150.00,150.00); System.out.println(stu.getInfo()); } }