编写一程序,定义一个抽象类Person

来源:百度知道 编辑:UC知道 时间:2024/07/05 05:08:33
编写一程序,定义一个抽象类Person,属性包括姓名(name),包含一个传递姓名参数的构造方法,还有显示信息的抽象方法showinfo()。创建它的子类Student,属性包括:姓名(name),年级(grade),专业(specialty)。并且重写父类的方法。并创建测试类,创建学生类,并输出信息。

public class Test {
public static void main(String args[]) {
Student student = new Student("a", 1, "b");
student.showInfo();
}
}

abstract class Person {
protected String name;

Person(String name) {
this.name = name;
}

abstract void showInfo();
}

class Student extends Person {
int grade;
String specialty;

Student(String name, int grade, String specialty) {
super(name);
this.grade = grade;
this.specialty = specialty;
}

void showInfo() {
System.out.printf("Name: %s, Grade: %d, Specialty: %s\n",
name, grade, specialty);
}

}