java定义一个Person类,包括成员变量name(姓名)、sex(性别)、age(年龄)和成员方法p

java!!!定义一个Person类,包括成员变量name(姓名)、sex(性别)、age(年龄)和成员方法print(),该方法输出成员变量的信息,并定义一个包含两个参数name和age 的构造方法,实现对name和age赋初值。再定义一个子类Student,子类不仅具有父类的成员变量还定义了新成员变量school(学院)、department(系)和studentno(学号)。要求子类覆写了父类的print 方法,输出Student成员信息,并且给出构造方法实现对Student类成员变量赋初值

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文件,执行以下可以看出效果!
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2016-03-15
public class Person {

private static String name;

private static String sex;

private static int age;

Person(String name, int age) {
this.name = name;
this.age = age;
}

public void print() {
System.out.print("名字:" + name + " 性别:" + sex + " 年龄:" + age + " ");
}
}

public class Student extends Person {

private static String school;

private static String department;

private static String studentno;

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() {
super.print();
System.out.println("学校是:" + school + " 系是:" + department + " 学号是:"
+ studentno);
}

}本回答被提问者和网友采纳
第2个回答  2010-10-11
用super继承父类构造的方法(这里我多写了sex)。你可以以公共函数将父类的构造方法写在print里,这里写在外面。对父类name和age赋初值的方法可以参考子类的赋值的写法.

public class Person{
public String name;
public String sex;
public int age;

public Person(String name,String sex,int age){
this.name = name;
this.sex = sex;
this.age = age;
}

public void print(){
System.out.printIn(name);
System.out.printIn(sex);
System.out.printIn(age);
}
}

public class Student extends Student{
public String school;
public String department;
public int studentno;

public Student(String name,String sex,int age,String school,String department,int studentno){
super(name,sex,age);
this.school = school;
this.department = department;
this.studentno = studentno;
}

public void print(){
super.print();
System.out.printIn(school);
System.out.printIn(department);
System.out.printIn(studentno);
}

public void main(){
Student student = new Student("lw","男",21,"电大","软件系",12);
student.print();
}
}
相似回答