写的听不错,不过还有改善的余地,比如work可以是一个接口,而不应该给每一种工作都作为一个接口,世间工作千万种。:
- package com.itheima.demo2;
- public class ObjectOrientedDemo {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- /**
- * 题目:请用java中面向对象的思想用代码描述如下内容:小红(女)有爸爸和妈妈,爸爸和妈妈分别有自己的爸爸和妈妈。。。,小红的爸爸会做工作A和B,小红的妈妈会做工作C和D。
- 要求:将上面的情景用Java类来描述,要求自己抽象出接口和对象。
- 上传代码的时候将运算结果截图一起提交。
- * */
-
- /**
- * 小红(XiaoHong)她爸是Romeo,他妈是Juliet,
- *
- * 她爸有自己的爸爸(Tom),即祖父。
- * 她爸有自己的妈妈(Marry),即祖母。
- *
- * 他妈有自己的爸爸(Alex),即外祖父
- * 他妈有自己的妈妈(Grace),即外祖母
- *
- * 此处就省略祖父母,外祖父母的爸爸和妈妈,都设置为null
- *
- * */
-
- //grandfatherOfFather, grandmotherOfFather分别作为小红她爸的爸爸和妈妈
- Person grandfatherOfFather = new Person("Tom", Person.MALE, null, null);
- Person grandmotherOfFather = new Person("Marry", Person.FEMALE, null, null);
-
- //grandfatherOfFather, grandmotherOfFather分别作为小红她爸妈的爸爸和妈妈
- Person grandfatherOfMather = new Person("Alex",Person.MALE, null, null);
- Person grandmotherOfMather = new Person("Grace",Person.FEMALE, null, null);
-
- //father为小红他爸爸
- ABPerson father = new ABPerson("Romeo",Person.MALE, grandfatherOfFather, grandmotherOfFather);
- //father为小红他妈妈
- CDPerson mother = new CDPerson("Juliet",Person.FEMALE, grandfatherOfMather, grandmotherOfMather);
-
- //小红本人
- Person xiaoHong = new Person("XiaoHong",Person.FEMALE, father, mother);
-
- System.out.println("小红:" + xiaoHong);
- System.out.println("小红爸爸 : " + father);
- System.out.println("小红妈妈 : " + mother);
- }
- }
- //某人具有某种功能A, B, C, D使用接口来实现,提供一种新的功能
- interface WorkA {
- void doWorkA();
- }
- interface WorkB {
- void doWorkB();
- }
- interface WorkC {
- void doWorkC();
- }
- interface WorkD {
- void doWorkD();
- }
- /**
- * 抽象出Person类,公有类,里面持有两个Person引用作为其爸爸和妈妈
- * @author Joey
- *
- */
- class Person {
-
- public static int MALE = 1;
- public static int FEMALE = 0;
-
- private String name;
- private int gender;
- private Person father;
- private Person mother;
-
- //enum无法重写toString()
- // static enum int {
- // MALE, FEMALE;
- //
- // @Override
- // public String toString() {
- // return this == MALE ? "male" : "female";
- // }
- // }
- /**
- * 构造函数传入father和mother
- * @param father
- * @param mother
- * 面向对象理解为人一出生就要确定谁是他(她)的爸妈
- *
- */
- public Person(String name, int gender, Person father, Person mother) {
- //初始化一个人的姓名,性别,其父母
- this.name = name;
- this.gender = gender;
- this.father = father;
- this.mother = mother;
- }
- public Person getFather() {
- return father;
- }
-
- @Override
- public String toString() {
- String mGender = gender == Person.MALE ? "male" : "female";
- StringBuffer sb = new StringBuffer();
- sb.append("My name is " + name + " ,")
- .append("I am a " + mGender + " , ")
- .append("my father is " + getFather().getName() + " , ")
- .append("my mother is " + getMother().getName() + " . ");
- return sb.toString();
- }
-
- //出生后不排除他(她)认别人当爸
- public void setFather(Person father) {
- this.father = father;
- }
- public Person getMother() {
- return mother;
- }
- //出生后不排除他(她)认别人当妈
- public void setMother(Person mother) {
- this.mother = mother;
- }
-
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
- //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 分割线 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
- /**
- * 备注:
- * 此处也可使用抽象类abstract类来实现,通过策略模式可以使各个A ,B, C, D的工作完成相同的逻辑
- * 但没有interface借口可以多继承方便灵活实用
- *
- * */
- //此处该类用于描述小红他爸,或是类似小红他爸的那一类人:可以做A,B工作的人,不分男女
- class ABPerson extends Person implements WorkA, WorkB {
- public ABPerson(String name, int gender, Person father, Person mother) {
- super(name, gender, father, mother);
- }
- @Override
- public void doWorkA() {
- // TODO Auto-generated method stub
- System.out.println("I can do work A");
- }
- @Override
- public void doWorkB() {
- // TODO Auto-generated method stub
- System.out.println("I can do work B");
- }
-
- @Override
- public String toString() {
- return super.toString() + "I can do work A and B." ;
- }
- }
- //此处该类用于描述小红他妈,或是类似小红他妈的那一类人:可以做A,B工作的人,不分男女
- class CDPerson extends Person implements WorkC, WorkD {
- public CDPerson(String name, int gender, Person father, Person mother) {
- super(name, gender, father, mother);
- }
- @Override
- public void doWorkD() {
- // TODO Auto-generated method stub
- System.out.println("I can do work D");
- }
- @Override
- public void doWorkC() {
- // TODO Auto-generated method stub
- System.out.println("I can do work C");
- }
- @Override
- public String toString() {
- return super.toString() + "I can do work C and D." ;
- }
-
- }
复制代码 |