package student; class Student{     //父类属性访问权限设置为protected,可供子类访问     protected String name;     protected int age;     public Student(String name,int age)     {         this.name=name;         this.age=age;     }     public void show()     {         System.out.print("姓名:"+this.name+" ");         System.out.print("年龄:"+this.age);     } } class Undergraduate extends Student{          private String degree;          public Undergraduate(String name,int age,String degree)     {         super(name,age);//使用super()函数来调用父类相应的构造函数;         this.degree=degree;     }     public void show()     {         System.out.print("姓名:"+this.name+" ");         System.out.print("年龄:"+this.age+" ");         System.out.print("学位:"+this.degree);     } }  public class StudentTest {          public static void main(String[] args)     {         System.out.println("===学生类测试===");         Student s=new Student("李明",22);         s.show();         System.out.println();         System.out.println("===本科生类测试===");         s=new Undergraduate("张三",23,"学士学位");         s.show();     } }
点赞 评论

相关推荐

不入算法余生悔:大二开始实习,本科三年经验,再考个研,到研二正好是五年经验
点赞 评论 收藏
分享
牛客网
牛客企业服务