Java题目:建立一个人类(Person)和学生类(Student) 功能要求:

2024-11-27 16:39:33
推荐回答(1个)
回答(1):

package test;
/**
* Person中包含4个保护型的数据成员name,addr, sex,age分别为字符串,字符串,字符及整型。
表示:姓名、地址、性别和年龄。一个四参构造函数,一个两参构造函数,一个无参构造函数,一个输出函数用于显示四种属性。
* @author Administrator
*
*/public class Person {
protected String name;
protected String addr;
protected char sex;
protected int age;

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

public Person(String name,String addr){
this.addr = addr;
this.name = name;
}

public Person(){

}

public void print(){
System.out.println("name="+name+",addr="+addr+",sex="+sex+",age="+age);
}
}
=====================================================package test;public class Student extends Person{
private int math;
private int english; public Student(int math,int english) {
this.math = math;
this.english = english;
} public Student(String name, String addr, char sex, int age, int math,int english) {
super(name, addr, sex, age);
this.math = math;
this.english = english;
} public Student() {
super();
}

public void print(){
System.out.println("name="+name+",addr="+addr+",sex="+sex+",age="+age+",math="+math+",english="+english);
}
}
2种关系在Myeclipse中都可以自动生成,右键source --》 constructor中就是构造函数的自动生成