/** * Design a student class Student and its subclass Undergradute, the requirements are as follows: * 1. The Student class has name (name) and age (age), a constructor with two parameters, * Used to assign value to the name and age attributes, a show() method dayinStudent attribute information. * @author FeiEr * */ public class Student { String name; int age; public Student(String name,int age) { this.name = name; this.age = age; } public void show() { System.out.println("I am"+name+","+"this year"+age+"year old!"); } }
/** * 2. Undergraduate class adds a degree attribute. one contains three * The construction method of the parameters, the first two parameters are used to assign values to the inherited name and age, and the third parameter is assigned to the degree profession, * A show() method prints the Undergrate's attribute information. * @author FeiEr * */ public 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("I am "+name+","+"this year"+age+"year"+","+degree+"graduated!"); } }
/** * 3. Create Student object and Undergraduate attribute information in the test class. * @author FeiEr * */ public class Test001 { public static void main(String[] args) { Student student = new Student("王飞",24); Undergraduate undergraduate = new Undergraduate("王剑",23,"硕士"); student.show(); undergraduate.show(); } }