A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始


加油!
回复 使用道具 举报
混乱的世界 发表于 2015-3-21 10:48
往期的技术贴也可以得分么

最近的10期都可以。
回复 使用道具 举报
AsyncTask 发表于 2015-3-21 23:38
Done!不知这样理解是否合适?

写的听不错,不过还有改善的余地,比如work可以是一个接口,而不应该给每一种工作都作为一个接口,世间工作千万种。:
  1. package com.itheima.demo2;

  2. public class ObjectOrientedDemo {

  3.         public static void main(String[] args) {
  4.                 // TODO Auto-generated method stub

  5.                 /**
  6.                  *         题目:请用java中面向对象的思想用代码描述如下内容:小红(女)有爸爸和妈妈,爸爸和妈妈分别有自己的爸爸和妈妈。。。,小红的爸爸会做工作A和B,小红的妈妈会做工作C和D。
  7.                         要求:将上面的情景用Java类来描述,要求自己抽象出接口和对象。
  8.                         上传代码的时候将运算结果截图一起提交。
  9.                  * */
  10.                
  11.                 /**
  12.                  *         小红(XiaoHong)她爸是Romeo,他妈是Juliet,
  13.                  *
  14.                  *         她爸有自己的爸爸(Tom),即祖父。
  15.                  *         她爸有自己的妈妈(Marry),即祖母。
  16.                  *
  17.                  *         他妈有自己的爸爸(Alex),即外祖父
  18.                  *         他妈有自己的妈妈(Grace),即外祖母
  19.                  *
  20.                  *         此处就省略祖父母,外祖父母的爸爸和妈妈,都设置为null
  21.                  *
  22.                  * */
  23.                
  24.                 //grandfatherOfFather, grandmotherOfFather分别作为小红她爸的爸爸和妈妈
  25.                 Person grandfatherOfFather = new Person("Tom", Person.MALE, null, null);
  26.                 Person grandmotherOfFather = new Person("Marry", Person.FEMALE, null, null);
  27.                
  28.                 //grandfatherOfFather, grandmotherOfFather分别作为小红她爸妈的爸爸和妈妈
  29.                 Person grandfatherOfMather = new Person("Alex",Person.MALE, null, null);
  30.                 Person grandmotherOfMather = new Person("Grace",Person.FEMALE, null, null);
  31.                
  32.                 //father为小红他爸爸
  33.                 ABPerson father = new ABPerson("Romeo",Person.MALE, grandfatherOfFather, grandmotherOfFather);
  34.                 //father为小红他妈妈
  35.                 CDPerson mother = new CDPerson("Juliet",Person.FEMALE, grandfatherOfMather, grandmotherOfMather);
  36.                
  37.                 //小红本人
  38.                 Person xiaoHong = new Person("XiaoHong",Person.FEMALE, father, mother);
  39.                
  40.                 System.out.println("小红:" + xiaoHong);
  41.                 System.out.println("小红爸爸 : " + father);
  42.                 System.out.println("小红妈妈 : " + mother);
  43.         }

  44. }

  45. //某人具有某种功能A, B, C, D使用接口来实现,提供一种新的功能
  46. interface WorkA {
  47.         void doWorkA();
  48. }

  49. interface WorkB {
  50.         void doWorkB();
  51. }

  52. interface WorkC {
  53.         void doWorkC();
  54. }

  55. interface WorkD {
  56.         void doWorkD();
  57. }

  58. /**
  59. * 抽象出Person类,公有类,里面持有两个Person引用作为其爸爸和妈妈
  60. * @author Joey
  61. *
  62. */
  63. class Person {
  64.        
  65.         public static int MALE = 1;
  66.         public static int FEMALE = 0;
  67.        
  68.         private String name;
  69.         private int gender;
  70.         private Person father;
  71.         private Person mother;
  72.        
  73.         //enum无法重写toString()
  74. //        static enum int {
  75. //                MALE, FEMALE;
  76. //               
  77. //                @Override
  78. //                public String toString() {
  79. //                        return this == MALE ? "male" : "female";
  80. //                }
  81. //        }
  82.         /**
  83.          * 构造函数传入father和mother
  84.          * @param father
  85.          * @param mother
  86.          * 面向对象理解为人一出生就要确定谁是他(她)的爸妈
  87.          *
  88.          */
  89.         public Person(String name, int gender, Person father, Person mother) {
  90.                 //初始化一个人的姓名,性别,其父母
  91.                 this.name = name;
  92.                 this.gender = gender;
  93.                 this.father = father;
  94.                 this.mother = mother;
  95.         }

  96.         public Person getFather() {
  97.                 return father;
  98.         }
  99.        
  100.         @Override
  101.         public String toString() {
  102.                 String mGender = gender == Person.MALE ? "male" : "female";
  103.                 StringBuffer sb = new StringBuffer();
  104.                 sb.append("My name is " + name + " ,")
  105.                   .append("I am a " + mGender + " , ")
  106.                   .append("my father is " + getFather().getName() + " , ")
  107.                   .append("my mother is " + getMother().getName() + " . ");
  108.                 return sb.toString();
  109.         }
  110.        
  111.         //出生后不排除他(她)认别人当爸
  112.         public void setFather(Person father) {
  113.                 this.father = father;
  114.         }

  115.         public Person getMother() {
  116.                 return mother;
  117.         }

  118.         //出生后不排除他(她)认别人当妈
  119.         public void setMother(Person mother) {
  120.                 this.mother = mother;
  121.         }
  122.        
  123.         public String getName() {
  124.                 return name;
  125.         }

  126.         public void setName(String name) {
  127.                 this.name = name;
  128.         }
  129. }

  130. //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 分割线 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

  131. /**
  132. *         备注:
  133. *                 此处也可使用抽象类abstract类来实现,通过策略模式可以使各个A ,B, C, D的工作完成相同的逻辑
  134. *                 但没有interface借口可以多继承方便灵活实用
  135. *
  136. * */

  137. //此处该类用于描述小红他爸,或是类似小红他爸的那一类人:可以做A,B工作的人,不分男女
  138. class ABPerson extends Person implements WorkA, WorkB {

  139.         public ABPerson(String name, int gender, Person father, Person mother) {
  140.                 super(name, gender, father, mother);
  141.         }

  142.         @Override
  143.         public void doWorkA() {
  144.                 // TODO Auto-generated method stub
  145.                 System.out.println("I can do work A");
  146.         }

  147.         @Override
  148.         public void doWorkB() {
  149.                 // TODO Auto-generated method stub
  150.                 System.out.println("I can do work B");
  151.         }
  152.        
  153.         @Override
  154.         public String toString() {
  155.                 return super.toString() + "I can do work A and B." ;
  156.         }
  157. }

  158. //此处该类用于描述小红他妈,或是类似小红他妈的那一类人:可以做A,B工作的人,不分男女
  159. class CDPerson extends Person implements WorkC, WorkD {

  160.         public CDPerson(String name, int gender, Person father, Person mother) {
  161.                 super(name, gender, father, mother);
  162.         }

  163.         @Override
  164.         public void doWorkD() {
  165.                 // TODO Auto-generated method stub
  166.                 System.out.println("I can do work D");
  167.         }

  168.         @Override
  169.         public void doWorkC() {
  170.                 // TODO Auto-generated method stub
  171.                 System.out.println("I can do work C");
  172.         }

  173.         @Override
  174.         public String toString() {
  175.                 return super.toString() + "I can do work C and D." ;
  176.         }
  177.        
  178. }
复制代码
回复 使用道具 举报
RaymingChan 发表于 2015-3-23 17:35
阳哥请查阅,不知道是否理解错。

命名风格有点儿像C。不管是祖父母还是父母都是Person的子类,一个Person应该哟两个Person为期构造参数。
回复 使用道具 举报
AsyncTask 发表于 2015-3-23 22:28
阳哥,代码有个Bug,祖父母的父母为null,但打印对象时没有做null判断处理,会抛出空指针异常。偷懒没注 ...

好的,看到了。不错。
回复 使用道具 举报
AsyncTask 发表于 2015-3-23 22:28
阳哥,代码有个Bug,祖父母的父母为null,但打印对象时没有做null判断处理,会抛出空指针异常。偷懒没注 ...

汇总贴:http://bbs.itheima.com/thread-180149-1-1.html
回复 使用道具 举报
西门夜说 发表于 2015-3-24 23:21
阳老师 求技术分 求黑马币  跪求啊

提交正确答案就可能获得技术分。
回复 使用道具 举报
西门夜说 发表于 2015-3-24 23:43
求个简单点的题 我才看了前几天的视频

基础好,一切都好。在就业班一般学习不好的都是基础不好的,加油。
回复 使用道具 举报
李大大 发表于 2015-3-26 19:57
阳哥,我是新手,来来学习学习

欢迎
回复 使用道具 举报
nian 发表于 2015-3-26 07:59
王振阳老师 真好 就您给我技术分 嘎嘎~~~赞!

答题就有分。
回复 使用道具 举报
Zack 发表于 2015-4-13 10:51
前辈,小弟来领个题做做~看起来人气好高

感谢支持,哈哈,500楼我自己占啦。
回复 使用道具 举报
wj5877 发表于 2015-4-15 12:40
阳哥来晚了,看看这个行不行啊??

不管是Father和Mother还是小红都是Person,Person的构造函数必须有两个Person(一个作为母亲,一个作为父亲),这也是一种类似链表的数据结构,一环套一环。工作是一个接口即可。
回复 使用道具 举报
回复 使用道具 举报
杜应超 发表于 2015-4-21 21:39
可能没理解对,请老师指正

继续加油、。
回复 使用道具 举报

抽取的还不是很好,继续加油。
回复 使用道具 举报
123
您需要登录后才可以回帖 登录 | 加入黑马