定义一个Person类,其中包括:1.定义属性:姓名、年龄、民族作为成员变量。定义静态成员变量:人数2.定义构造方法:对成员变量进行初始化。3.定义多个方法:分别显示相应的属性值,例如getName(){System.out.print("名称="+name+";"); }4.定义一个方法“成长”:实现年龄的增加,每执行一次年龄增加1 题目显示不全,完整题目描述:
完整代码如下:
package naizi;
class Person {
protected String Name;
protected int Age;
protected String Nationality;
public static int count=0;
Person(String Name,int Age,String Nationality){
this.Name = Name;
this.Age = Age;
this.Nationality = Nationality;
count++;
}
//显示属性值
public void getName() {
System.out.println("Name="+Name+";");
}
public void getAge() {
System.out.println("Age="+Age+";");
}
public void getNationality() {
System.out.println("Nationality="+Nationality+";");
}
//成长方法
public void growUp(){
Age++;
}
//显示人数方法
public void show(){
System.out.println("Person count:"+count+";");
}
}
//定义Nationality接口
interface Nationality{
public void custom();
public void policy();
}
//定义一个Student类继承Person类,实现Nationality接口
public class Student extends Person implements Nationality{
//子类的新成员变量
protected String Education;
//子类的构造方法调用父类的构造方法
Student(String Name,int Age,String Nationality,String Education){
super(Name, Age, Nationality);
this.Name = Name;
this.Age = Age;
this.Nationality = Nationality;
this.Education = Education;
}
//构造方法的重载,并且默认如果一个学生没有传入民族,默认为汉族
Student(String Name,String Education){
this(Name, 0, "han", Education);
this.Name = Name;
this.Education = Education;
}
//覆盖父类的成长方法
public void growUp(){
if("Undergraduate".equals(Education)){
System.out.println("Undergraduate;");
}else {
System.out.println("Postgraduate;");
}
Age++;
super.growUp();
}
public void custom(){
System.out.println("Spring Festival, Dragon Boat Festival, Mid-Autumn Festival");
}
public void policy(){
if("han".equals(Nationality)){
System.out.println("No preferential policies;");
}else {
System.out.println("Have preferential policies;");
}
}
//请参考main函数的内容编写以上内容
public static void main(String args[]){
Person a=new Person("zhangsan",19,"han");
a.getName();
a.getAge();
a.getNationality();
a.growUp();
a.getAge();
a.show();
Person b=new Person("lisi",39,"man");
b.getName();
b.getAge();
b.getNationality();
b.growUp();
b.getAge();
b.show();
Student c=new Student("wanger",33,"man","Postgraduate");
c.growUp();
c.custom();
c.policy();
c.show();
Student d=new Student("wanger","Undergraduate");
d.growUp();
d.custom();
d.policy();
d.show();
}
}
测试如图: