(继承+封装)编程:定义一个人类(Person),包括属性:姓名、性别、年龄、国籍;
包括的方法:吃饭、睡觉,工作
(1)根据人类,定义一个学生子类,增加属性:学校、学号;重写工作方法(实现内容为学习)
(2)根据人类,定义一个工人类,增加属性:单位、工龄;重写工作方法
(3)根据学生类,定义一个学生*类(StudentLeader),增加属性:职务;增加方法:开会
(4)定义一个测试类,分别创建上述3类具体人物的对象并将信息打印在控制台上.
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(){
("吃饭");
}
public void sleep(){
("睡觉");
}
public void jop(){
("工作");
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
", nationality='" + nationality + '\'' +
'}';
}
}
class StuentPerson extends Person{
private String school;
private int id;
public StuentPerson() {
}
public StuentPerson(String school, int id) {
= school;
= id;
}
public StuentPerson(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() {
("学生学习");
}
}
class WorkerPenson extends Person{
private String workplace;
private int seniority;
public WorkerPenson() {
}
public WorkerPenson(String workplace, int seniority) {
= workplace;//单位
= 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() {
("工人工作");
}
}
class StudentLeader extends StuentPerson{
private String post;//职务
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(){
("学生*开会");
}
}
class PensonTest{
public static void main(String[] args) {
StuentPerson stuentPerson = new StuentPerson("张三","男",21,"中国","四川工业",101);
();
(()+()+()+()+()+());
WorkerPenson workerPenson = new WorkerPenson("李四","女",32,"中国","中信银行",3);
();
(()+()+()+()+()+());
StudentLeader studentLeader = new StudentLeader("学生会主席");
();
();
(()+()+()+()+()+()+());
();
();
();
();
();
();
}
}
(多态)在上一个题目的基础上,定义一个Person类型的数组,存储多个不同类型的子类型对象,
(1)统计并打印输出数组中所有学生*的个数
(2)打印输出所有学生的信息
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() {
("吃饭");
}
public void sleep() {
("睡觉");
}
public abstract void jop();
@Override
public String toString() {
return "姓名='" + name + '\'' +
", 性别='" + sex + '\'' +
", 年龄=" + age +
", 国籍='" + 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() {
("学生学习");
}
@Override
public String toString() {
return ()+" ,学校:"+school+" ,学号:"+id;
}
}
class WorkerPenson extends Person {
private String workplace;
private int seniority;
public WorkerPenson() {
}
public WorkerPenson(String workplace, int seniority) {
= workplace;//单位
= 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() {
("工人工作");
}
@Override
public String toString() {
return ()+" ,单位:"+workplace+" ,工龄:"+seniority;
}
}
class StudentLeader extends StudentPerson {
private String post;//职务
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() {
("学生*开会");
}
@Override
public String toString() {
return ()+"职务:"+post;
}
}
class PersonTest {
public static void main(String[] args) {
WorkerPenson worker = new WorkerPenson("张三","男",23,"中国","中国石油",2);
Person worker1 = new WorkerPenson("李四","男",30,"中国","中国联通",6);
StudentPerson studentPerson = new StudentPerson("马瑞","女",30,"中国","四川工业",101);
Person studentPerson1 = new StudentPerson("杰克","女",23,"中国","西南财经",201);
StudentLeader studentLeader = new StudentLeader("汤姆","男",34,"中国","四川工业",108,"文体部");
Person studentLeader1 = new StudentLeader("肉丝","男",19,"中国","西南",112,"外联部");
Person studentLeader2 = new StudentLeader("白菜","男",20,"中国","西南",333,"全媒体");
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());
}
}
("学生*的个数为:"+count);
}
}