首页上一页 1 下一页尾页 2 条记录 1/1页
关于super关键字的使用,如何使子类中新定义的变量,通过重写父类的方法输出
发表在Java图书答疑
2021-03-26
《Java从入门到精通(第4版)》第10章 接口、继承与多态
是否精华
是
否
版块置顶:
是
否
public class Student { int id;//学号 String name;//姓名 double mathGrades;//数学成绩啊 double computerGrades;//计算机成绩 public Student(int id,String name,double mathGrades,double computerGrades){//构造方法 this.id = id ; this.name = name; this.mathGrades = mathGrades; this.computerGrades = computerGrades ; } int getId(){//获取id return id; } void setId(int a){//修改id id = a; } String getName(){//获取name return name; } void setId(String b){//修改name name = b; } double getMathGrades(){//获取mathGrades return mathGrades; } void setMathGrades(double c){//修改mathGrades mathGrades = c; } double getComputerGrades(){//获取computerGrades return computerGrades; } void setComputerGrades(double d){//修改computerGrades computerGrades = d ; } void print (){ System.out.println("学号:"+this.getId()); System.out.println("姓名:"+this.getName()); System.out.println("数学成绩:"+this.getMathGrades()); System.out.println("计算机成绩:"+this.getComputerGrades()); } } class PostGradute extends Student{ String mentor;//导师 String researchDirection;//研究方向 public PostGradute(int id, String name, double mathGrades, double computerGrades) { //继承父类的构造方法 super(id, name, mathGrades, computerGrades); } String getMentor(){//获取mentor return mentor; } void setMentor(String e){//修改mentor mentor =e ; } String getResearchDirection(){//获取researchDirection return researchDirection; } void setResearchDirection(String f){//修改researchDirection researchDirection = f ; } void print (){ System.out.println("学号:"+this.getId()); System.out.println("姓名:"+this.getName()); System.out.println("数学成绩:"+this.getMathGrades()); System.out.println("计算机成绩:"+this.getComputerGrades()); System.out.println("导师:"+this.getMentor()); System.out.println("研究方向:"+this.getResearchDirection()); } public static void main(String[] args) { Student st = new Student(20143240,"李四",65.5,75); st.print(); PostGradute pg = new PostGradute(20143240,"李四",65.3,70); pg.print(); }
于2021-03-26 22:12:17编辑