(inherit+Package)programming:Defining a human(Person),Include attributes:Name、gender、age、Country of Citizenship;
Methods included:Have a meal、sleep,Work
(1)According to humans,Define a student subclass,Add properties:School、Student ID;重写Work方法(Implement content for learning)
(2)According to humans,Define a worker,Add properties:unit、Work experience;重写Work方法
(3)According to the student class,Define a student cadre class(StudentLeader),Add properties:Position;Add method:Meeting
(4)Define a test class,Create the above separately3A specific object and print information on the console.
public class Person {
private String name;
private String sex;
private int age;
private String nationality;
public Person() {
}
public Person(String name, String sex, int age, String nationality) {
= name;
= sex;
= age;
= nationality;
}
public String getName() {
return name;
}
public void setName(String name) {
= name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
= sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
= age;
}
public String getNationality() {
return nationality;
}
public void setNationality(String nationality) {
= nationality;
}
public void eat(){
("Have a meal");
}
public void sleep(){
("sleep");
}
public void jop(){
("Work");
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
", nationality='" + nationality + '\'' +
'}';
}
}
class StudentPerson extends Person{
private String school;
private int id;
public StudentPerson() {
}
public StudentPerson(String school, int id) {
= school;
= id;
}
public StudentPerson(String name, String sex, int age, String nationality, String school, int id) {
super(name, sex, age, nationality);
= school;
= id;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
= school;
}
public int getId() {
return id;
}
public void setId(int id) {
= id;
}
@Override
public void jop() {
("Student Study");
}
}
class WorkerPenson extends Person{
private String workplace;
private int seniority;
public WorkerPenson() {
}
public WorkerPenson(String workplace, int seniority) {
= workplace;//Unit
= seniority;//Seniority
}
public WorkerPenson(String name, String sex, int age, String nationality, String workplace, int seniority) {
super(name, sex, age, nationality);
= workplace;
= seniority;
}
public String getWorkplace() {
return workplace;
}
public void setWorkplace(String workplace) {
= workplace;
}
public int getSeniority() {
return seniority;
}
public void setSeniority(int seniority) {
= seniority;
}
@Override
public void jop() {
("Workers' Job");
}
}
class StudentLeader extends StudentPerson{
private String post;//Position
public StudentLeader() {
}
public StudentLeader(String post) {
= post;
}
public StudentLeader(String school, int id, String post) {
super(school, id);
= post;
}
public StudentLeader(String name, String sex, int age, String nationality, String school, int id, String post) {
super(name, sex, age, nationality, school, id);
= post;
}
public String getPost() {
return post;
}
public void setPost(String post) {
= post;
}
public void meeting(){
("Student cadres hold meeting");
}
}
class PensonTest{
public static void main(String[] args) {
StudentPerson stuentPerson = new StudentPerson("Zhang San","Male",21,"China","Sichuan Industry",101);
();
(()+()+()+()+()+()+()+()+());
WorkerPenson workerPenson = new WorkerPenson("Li Si","female",32,"China","CITIC Bank",3);
();
(()+()+()+()+()+()+()+()+());
StudentLeader studentLeader = new StudentLeader("Student Union President");
();
();
(()+()+()+()+()+()+()+()+()+());
();
();
();
();
();
();
}
}
(Polymorphic)Based on the previous question,Define aPersonArray of type,Store multiple subtype objects of different types,
(1)Statistics and prints out the number of all student cadres in the array
(2)Print out all student information
public abstract class Person {
private String name;
private String sex;
private int age;
private String nationality;
public Person() {
}
public Person(String name, String sex, int age, String nationality) {
= name;
= sex;
= age;
= nationality;
}
public String getName() {
return name;
}
public void setName(String name) {
= name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
= sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
= age;
}
public String getNationality() {
return nationality;
}
public void setNationality(String nationality) {
= nationality;
}
public void eat() {
("Have a meal");
}
public void sleep() {
("sleep");
}
public abstract void jop();
@Override
public String toString() {
return "Name='" + name + '\'' +
", Gender ='" + sex + '\'' +
", age=" + age +
", nationality='" + nationality;
}
}
class StudentPerson extends Person {
private String school;
private int id;
public StudentPerson() {
}
public StudentPerson(String school, int id) {
= school;
= id;
}
public StudentPerson(String name, String sex, int age, String nationality, String school, int id) {
super(name, sex, age, nationality);
= school;
= id;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
= school;
}
public int getId() {
return id;
}
public void setId(int id) {
= id;
}
@Override
public void jop() {
("Student Study");
}
@Override
public String toString() {
return ()+" ,School: "+school+" ,Student number: "+id;
}
}
class WorkerPenson extends Person {
private String workplace;
private int seniority;
public WorkerPenson() {
}
public WorkerPenson(String workplace, int seniority) {
= workplace;//Unit
= seniority;//Seniority
}
public WorkerPenson(String name, String sex, int age, String nationality, String workplace, int seniority) {
super(name, sex, age, nationality);
= workplace;
= seniority;
}
public String getWorkplace() {
return workplace;
}
public void setWorkplace(String workplace) {
= workplace;
}
public int getSeniority() {
return seniority;
}
public void setSeniority(int seniority) {
= seniority;
}
@Override
public void jop() {
("Workers' Job");
}
@Override
public String toString() {
return ()+" , unit: "+workplace+" , service life: "+seniority;
}
}
class StudentLeader extends StudentPerson {
private String post;//Position
public StudentLeader() {
}
public StudentLeader(String post) {
= post;
}
public StudentLeader(String school, int id, String post) {
super(school, id);
= post;
}
public StudentLeader(String name, String sex, int age, String nationality, String school, int id, String post) {
super(name, sex, age, nationality, school, id);
= post;
}
public String getPost() {
return post;
}
public void setPost(String post) {
= post;
}
public void meeting() {
("Student cadres hold meeting");
}
@Override
public String toString() {
return ()+"Position:"+post;
}
}
class PersonTest {
public static void main(String[] args) {
WorkerPenson worker = new WorkerPenson("Zhang San","Male",23,"China","PetroChina",2);
Person worker1 = new WorkerPenson("Li Si","Male",30,"China","China Unicom",6);
StudentPerson studentPerson = new StudentPerson("Ma Rui","female",30,"China","Sichuan Industry",101);
Person studentPerson1 = new StudentPerson("Jack","Female",23,"China","Southwest Finance",201);
StudentLeader studentLeader = new StudentLeader("Tom","Male",34,"China","Sichuan Industry",108,"Sports Department");
Person studentLeader1 = new StudentLeader("Shredded pork","Male",19,"China","Southwest",112,"External Relations Department");
Person studentLeader2 = new StudentLeader("Cache","Male",20,"China","Southwest",333,"All Media");
Person[] person = new Person[]{worker,worker1,studentPerson,studentPerson1,studentLeader,studentLeader1,studentLeader2};
int count=0;
for (int i=0;i<;i++){
if ((person[i] instanceof StudentLeader)==true){
count++;
}
if ((person[i] instanceof StudentPerson) ==true){
(person[i].toString());
}
}
("The number of student cadres is: "+count);
}
}