class Person {
public String name;
public String sex;
public int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void setSex(String sex) {
this.sex = sex;
}
public void print() {
System.out.println("姓名:" + this.name + " 性别:" + this.sex + " 年龄:"
+ this.age);
}
}
public class Student extends Person {
public String school;
public String department;
public String studentno;
public Student(String name, int age, String school, String department,
String studentno) {
super(name, age);
this.school = school;
this.department = department;
this.studentno = studentno;
}
public void print() {
System.out.println("姓名:" + this.name + " 性别:" + this.sex + " 年龄:"
+ this.age + " 学院:" + this.school + " 系:" + this.department
+ " 学号:" + this.studentno);
}
public static void main(String[] args) {
Person a = new Person("张三", 24);
a.setSex("男");
a.print();
Student b = new Student("李四", 20, "清华大学", "计算机技术", "0828322109");
b.setSex("女");
b.print();
}
}
保存成Student.java文件,执行以下可以看出效果!