class Student {     protected String name;//姓名     protected int    age;//年龄      private static int num = 0;     public Student(String name,int age)     {         if(name != null)             this.name = name;         else             this.name ="Student " + num++;         if(age >= 0)             this.age  = age;         else             this.age  = 0;     }     public void show()     {         System.out.println(this.name + "的年龄是:" + this.age);     } } class Undergraduate extends Student {     private  String degree;//学位     public Undergraduate(String name,int age,String degree)     {         super(name,age);         this.degree = degree;     }     public void show()     {         System.out.println(this.name + "的年龄是:" + this.age + ",学位是:" + this.degree);     } } public class Test {     public static void main(String[] args)     {         Student s1 = new Student("小明",23);         Student s2 = new Student("小米",36);         Student s3 = new Student("小亮",12);         Student s4 = new Undergraduate("小红",37,"博士");         Student s5 = new Undergraduate("小辉",25,"本科");                  s1.show();         s2.show();         s3.show();         s4.show();         s5.show();     } }
点赞 1

相关推荐

10-11 17:45
门头沟学院 Java
点赞 评论 收藏
分享
牛客网
牛客企业服务