赞
踩
(继承+封装)编程:定义一个人类(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) {
- this.name = name;
- this.sex = sex;
- this.age = age;
- this.nationality = nationality;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getSex() {
- return sex;
- }
-
- public void setSex(String sex) {
- this.sex = sex;
- }
-
- public int getAge() {
- return age;
- }
-
- public void setAge(int age) {
- this.age = age;
- }
-
- public String getNationality() {
- return nationality;
- }
-
- public void setNationality(String nationality) {
- this.nationality = nationality;
- }
-
- public void eat(){
- System.out.println("吃饭");
- }
- public void sleep(){
- System.out.println("睡觉");
- }
- public void jop(){
- System.out.println("工作");
- }
-
- @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) {
- this.school = school;
- this.id = id;
- }
-
- public StuentPerson(String name, String sex, int age, String nationality, String school, int id) {
- super(name, sex, age, nationality);
- this.school = school;
- this.id = id;
- }
-
- public String getSchool() {
- return school;
- }
-
- public void setSchool(String school) {
- this.school = school;
- }
-
- public int getId() {
- return id;
- }
-
- public void setId(int id) {
- this.id = id;
- }
-
- @Override
- public void jop() {
- System.out.println("学生学习");
- }
- }
- class WorkerPenson extends Person{
- private String workplace;
- private int seniority;
-
- public WorkerPenson() {
- }
- public WorkerPenson(String workplace, int seniority) {
- this.workplace = workplace;//单位
- this.seniority = seniority;//工龄
- }
-
- public WorkerPenson(String name, String sex, int age, String nationality, String workplace, int seniority) {
- super(name, sex, age, nationality);
- this.workplace = workplace;
- this.seniority = seniority;
- }
-
- public String getWorkplace() {
- return workplace;
- }
-
- public void setWorkplace(String workplace) {
- this.workplace = workplace;
- }
-
- public int getSeniority() {
- return seniority;
- }
-
- public void setSeniority(int seniority) {
- this.seniority = seniority;
- }
-
- @Override
- public void jop() {
- System.out.println("工人工作");
- }
- }
- class StudentLeader extends StuentPerson{
- private String post;//职务
-
- public StudentLeader() {
- }
- public StudentLeader(String post) {
- this.post = post;
- }
-
- public StudentLeader(String school, int id, String post) {
- super(school, id);
- this.post = post;
- }
-
- public StudentLeader(String name, String sex, int age, String nationality, String school, int id, String post) {
- super(name, sex, age, nationality, school, id);
- this.post = post;
- }
-
- public String getPost() {
- return post;
- }
-
- public void setPost(String post) {
- this.post = post;
- }
-
- public void meeting(){
- System.out.println("学生干部开会");
- }
-
- }
- class PensonTest{
- public static void main(String[] args) {
- StuentPerson stuentPerson = new StuentPerson("张三","男",21,"中国","四川工业",101);
- stuentPerson.jop();
- System.out.println(stuentPerson.getName()+stuentPerson.getSex()+stuentPerson.getAge()+stuentPerson.getNationality()+stuentPerson.getSchool()+stuentPerson.getId());
-
- WorkerPenson workerPenson = new WorkerPenson("李四","女",32,"中国","中信银行",3);
- workerPenson.jop();
- System.out.println(workerPenson.getName()+workerPenson.getSex()+workerPenson.getAge()+workerPenson.getNationality()+workerPenson.getWorkplace()+workerPenson.getSeniority());
-
- StudentLeader studentLeader = new StudentLeader("学生会主席");
- studentLeader.jop();
- studentLeader.meeting();
- System.out.println(stuentPerson.getName()+stuentPerson.getSex()+stuentPerson.getAge()+stuentPerson.getNationality()+stuentPerson.getSchool()+stuentPerson.getId()+studentLeader.getPost());
-
- stuentPerson.eat();
- stuentPerson.sleep();
-
- workerPenson.eat();
- workerPenson.sleep();
-
- studentLeader.eat();
- studentLeader.sleep();
-
-
- }
- }
(多态)在上一个题目的基础上,定义一个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) {
- this.name = name;
- this.sex = sex;
- this.age = age;
- this.nationality = nationality;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getSex() {
- return sex;
- }
-
- public void setSex(String sex) {
- this.sex = sex;
- }
-
- public int getAge() {
- return age;
- }
-
- public void setAge(int age) {
- this.age = age;
- }
-
- public String getNationality() {
- return nationality;
- }
-
- public void setNationality(String nationality) {
- this.nationality = nationality;
- }
-
- public void eat() {
- System.out.println("吃饭");
- }
-
- public void sleep() {
- System.out.println("睡觉");
- }
-
- 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) {
- this.school = school;
- this.id = id;
- }
-
- public StudentPerson(String name, String sex, int age, String nationality, String school, int id) {
- super(name, sex, age, nationality);
- this.school = school;
- this.id = id;
- }
-
- public String getSchool() {
- return school;
- }
-
- public void setSchool(String school) {
- this.school = school;
- }
-
- public int getId() {
- return id;
- }
-
- public void setId(int id) {
- this.id = id;
- }
-
- @Override
- public void jop() {
- System.out.println("学生学习");
- }
-
- @Override
- public String toString() {
- return super.toString()+" ,学校:"+school+" ,学号:"+id;
- }
- }
-
- class WorkerPenson extends Person {
- private String workplace;
- private int seniority;
-
- public WorkerPenson() {
- }
-
- public WorkerPenson(String workplace, int seniority) {
- this.workplace = workplace;//单位
- this.seniority = seniority;//工龄
- }
-
- public WorkerPenson(String name, String sex, int age, String nationality, String workplace, int seniority) {
- super(name, sex, age, nationality);
- this.workplace = workplace;
- this.seniority = seniority;
- }
-
- public String getWorkplace() {
- return workplace;
- }
-
- public void setWorkplace(String workplace) {
- this.workplace = workplace;
- }
-
- public int getSeniority() {
- return seniority;
- }
-
- public void setSeniority(int seniority) {
- this.seniority = seniority;
- }
-
- @Override
- public void jop() {
- System.out.println("工人工作");
- }
-
- @Override
- public String toString() {
- return super.toString()+" ,单位:"+workplace+" ,工龄:"+seniority;
- }
- }
-
- class StudentLeader extends StudentPerson {
- private String post;//职务
-
- public StudentLeader() {
- }
-
- public StudentLeader(String post) {
- this.post = post;
- }
-
- public StudentLeader(String school, int id, String post) {
- super(school, id);
- this.post = post;
- }
-
- public StudentLeader(String name, String sex, int age, String nationality, String school, int id, String post) {
- super(name, sex, age, nationality, school, id);
- this.post = post;
- }
-
- public String getPost() {
- return post;
- }
-
- public void setPost(String post) {
- this.post = post;
- }
-
- public void meeting() {
- System.out.println("学生干部开会");
- }
-
- @Override
- public String toString() {
- return super.toString()+"职务:"+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<person.length;i++){
- if ((person[i] instanceof StudentLeader)==true){
- count++;
- }
- if ((person[i] instanceof StudentPerson) ==true){
- System.out.println(person[i].toString());
- }
- }
- System.out.println("学生干部的个数为:"+count);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。