java写一个Person类,包含name、age、sex属性,再写一个Student类继承Person类,增加School属性,并实例化。(用构造方法实现,并使用super())
class Person {
private String name;
private int age;
private String sex;
public Person(){
}
public Person(String name, int age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
public void setName(String name) {this.name = name;}
public String getName() {return this.name;}
public void setAge(int age) {this.age = age;}
public String getAge() {return this.age;}
public void setSex(String sex) {this.sex = sex;}
public String getSex() {return this.sex;}
}
public class Student extends Person{
private String school;
public Student(){
}
public Student(String school, String name, int age, String sex) {
super(name, age, sex);
this.school = school;
}
public void setSchool(String school) {this.school = school;}
public String getSchool() {return this.school;}
}
class Person {
private String name;
private int age;
private String sex;
public Person(String name, int age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
public void setName(String name) {this.name = name;}
public String getName() {return this.name;}
public void setAge(int age) {this.age = age;}
public String getAge() {return this.age;}
public void setSex(String sex) {this.sex = sex;}
public String getSex() {return this.sex;}
}
public class Student extends Person{
private String school;
public Student(String school, String name, int age, String sex) {
super(name, age, sex);
this.school = school;
}
public void setSchool(String school) {this.school = school;}
public String getSchool() {return this.school;}
public void static main(String[] args) {
Person s1 = new Student("清华大学", "刘备", 18, "男");
}
}
package Chapter1;
public class Student extends Person{
private String school;
public Student(String name, int age, String sex,String school) {
super(name, age, sex);
this.school = school;
// TODO Auto-generated constructor stub
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public static void main(String[] args) {
Student s = new Student("小明",10,"F","希望小学");
System.out.println(s.getName()+"今年"+s.getAge()+"岁,他在"+s.getSchool()+"上学");
}
}
class Person {
private String name;
private int age;
private String sex;
public Person(String name, int age, String sex) {
super();
this.name = name;
this.age = age;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}