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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

练习练习
回复 使用道具 举报
新手尝试答题
回复 使用道具 举报
新人,领题。
回复 使用道具 举报
DxxD 中级黑马 2015-3-15 20:38:45
244#
上一次阳哥提到的,创建小红需要父母作为参数传进去,这次可以了,可以给满分了吧:lol,PS:多谢论坛的同学帮忙

HmTest2.rar

12.42 KB, 阅读权限: 100, 下载次数: 1

评分

参与人数 1技术分 +1 收起 理由
王震阳老师 + 1 赞一个!

查看全部评分

回复 使用道具 举报
真的有分啊:lol
回复 使用道具 举报
先写一个“人”这个类;
然后再写“男人”“女人”两个类,继承人这个类,Override“人”类的方法;
最后写爸爸爷爷两个类继承“男人”, 妈妈奶奶和小红继承“女人”;
这就是我的想法,接触面向对象没多久,别笑话。。
回复 使用道具 举报
Mra 中级黑马 2015-3-15 21:45:23
247#
只能这么简陋了

Test3.zip

718 Bytes, 阅读权限: 200, 下载次数: 1

源代码

回复 使用道具 举报
是这样吗?

WPBP)EOC0OVF`CIQEO(A$K7.png (5.96 KB, 下载次数: 78)

WPBP)EOC0OVF`CIQEO(A$K7.png

PersonDemo.zip

594 Bytes, 阅读权限: 200, 下载次数: 1

评分

参与人数 1技术分 +1 收起 理由
王震阳老师 + 1 赞一个!

查看全部评分

回复 使用道具 举报
求题目求题目求题目求题目
回复 使用道具 举报
我要领题,请出示
回复 使用道具 举报
老师辛苦了
回复 使用道具 举报
不错不错
回复 使用道具 举报
好像拿但是还没有学到,争取下次
回复 使用道具 举报
新人,,先看看:)
回复 使用道具 举报
今天刚看到,临时抱佛脚:D

Demo.zip

20.39 KB, 下载次数: 108

评分

参与人数 1技术分 +1 收起 理由
王震阳老师 + 1 赞一个!

查看全部评分

回复 使用道具 举报
a1301155262 发表于 2015-3-15 16:33
管理员权限怎么设置??找不到,我选了个最高权限。

继续努力,加油。
回复 使用道具 举报
和谐木马 发表于 2015-3-15 15:33
老师,现在算结束了么?上课时间不多……
把代码和截图一起压缩上传
PS:JAVA新手,想法或过程有什么不对求 ...

写的不错,大家可以参考:
  1. /*
  2.         题目:请用java中面向对象的思想用代码描述如下内容:小红(女)有爸爸和妈妈,爸爸和妈妈分别有自己的爸爸和妈妈。。。,小红的爸爸会做工作A和B,
  3.         小红的妈妈会做工作C和D。
  4.         要求:将上面的情景用Java类来描述,要求自己抽象出接口和对象。
  5. */

  6. class PersonTest
  7. {
  8.         public static void main(String[] args)
  9.         {
  10.                 Person p=new Person("小红",'女');
  11.                 System.out.println("小红她爸");
  12.                 p.father.setWork(new Work_A());           //person对象的爸爸调用含工作接口的工作A
  13.                 p.father.setWork(new Work_B());
  14.                 System.out.println("小红她妈");
  15.                 p.mother.setWork(new Work_C());           //person对象的妈妈调用含工作接口的工作C
  16.                 p.mother.setWork(new Work_D());
  17.                 p.father.father.name="小红她爸的爸";      //给person对象的爸爸的爸爸的名字赋值
  18.                 p.mother.mother.name="小红她妈的妈";      //给person对象的妈妈的妈妈的名字赋值
  19.                 System.out.println("我是"+p.father.father.name);
  20.                 System.out.println("我是"+p.mother.mother.name);
  21.         }
  22. }

  23. class Person
  24. {
  25.         String name;
  26.         char sex;
  27.         Father father;
  28.         Mother mother;
  29.         //空参的构造函数
  30.         public Person(){
  31.         }
  32.         //带姓名和性别的Person类的构造函数
  33.         public Person(String name,char sex){
  34.                 this.name=name;
  35.                 this.sex=sex;
  36.                 /*
  37.                         构造Father,Mother类时,参数列表里的1只是一个无意义实数,是为了了无参的构造函数区分开来,
  38.                         表示此处调用的是个有参的构造函数
  39.                 */
  40.                 this.father=new Father(1);     //在构造一个Person类的时候也赋值它的一个"爸爸类"的属性
  41.                 this.mother=new Mother(1);     //在构造一个Person类的时候也赋值它的一个"妈妈类"的属性
  42.         }
  43. }

  44. //定义一个工作接口
  45. interface Work
  46. {
  47.         public abstract void working();
  48. }

  49. //定义一个工作A类实现工作接口
  50. class Work_A implements Work
  51. {
  52.         public void working()
  53.         {
  54.                 System.out.println("会干A工作");
  55.         }
  56. }
  57. //定义一个工作B类实现工作接口
  58. class Work_B implements Work
  59. {
  60.         public void working()
  61.         {
  62.                 System.out.println("会干B工作");
  63.         }
  64. }
  65. //定义一个工作C类实现工作接口
  66. class Work_C implements Work
  67. {
  68.         public void working()
  69.         {
  70.                 System.out.println("会干C工作");
  71.         }
  72. }
  73. //定义一个工作D类实现工作接口
  74. class Work_D implements Work
  75. {
  76.         public void working()
  77.         {
  78.                 System.out.println("会干D工作");
  79.         }
  80. }

  81. /*
  82.         因为爸爸还有爸爸和妈妈,所以在实例化一个爸爸类时还应该递归调用一个创建爸爸类的构造函数,但为了避免无限循环,
  83.         可以调用不同参数列表的构造函数,因此构造了一个有临时变量temp参数的构造函数,和最后调用的无参的构造函数。
  84.         妈妈类同理!
  85. */
  86. class Father extends Person        //定义一个爸爸类,它继承于Person类
  87. {
  88.         public Father(){
  89.         }
  90.         public Father(int temp){
  91.                 father=new Father();
  92.                 mother=new Mother();
  93.         }
  94.         public void setWork(Work work)
  95.         {
  96.                 if(work!=null)
  97.                         work.working();         //爸爸类调用带工作接口的类的工作函数
  98.         }
  99. }


  100. class Mother extends Person          //定义一个妈妈类,它继承于Person类
  101. {
  102.         //无参的构造函数
  103.         public Mother(){
  104.         }
  105.         //有一个临时变量temp的构造函数,这个temp只是为了区分无参的构造函数
  106.         public Mother(int temp){
  107.                 father=new Father();       //最里层调用无参的构造函数
  108.                 mother=new Mother();
  109.         }
  110.         public void setWork(Work work)
  111.         {
  112.                 if(work!=null)
  113.                         work.working();        //妈妈类调用带工作接口的类的工作函数
  114.         }
  115. }
复制代码
回复 使用道具 举报
shewim 发表于 2015-3-15 12:35
提交答案,麻烦老师帮忙看看!谢谢了...

写的不错,可以作为大家的参考:
  1. class ClassTest
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 String[] fWork = {"A","B"};
  6.                 String[] mWork = {"C","D"};
  7.                 Person f = new Person("红爸",new Person("红爷爷"),new Person("红奶奶"),fWork);
  8.                 Person m = new Person("红妈",new Person("红姥姥"),new Person("红姥爷"),mWork);
  9.                 Person d = new Person("小红",f,m);

  10.                 d.showPerson();
  11. //                System.out.println("Hello World!");
  12.         }
  13. }
  14. class Person
  15. {
  16.         private String name;
  17.         private Person father = null;
  18.         private Person mother = null;
  19.         private String[] work = null;

  20.         Person()
  21.         {
  22.         }
  23.         Person(String name)
  24.         {
  25.                 this.name = name;
  26.         }
  27.         Person(String name,String[] work)
  28.         {
  29.                 this.name = name;
  30.                 this.work = work;
  31.         }
  32.         Person(String name,Person father,Person mother)
  33.         {
  34.                 this.name = name;
  35.                 this.father = father;
  36.                 this.mother = mother;
  37.         }
  38.         Person(String name,Person father,Person mother,String[] work)
  39.         {
  40.                 this.name = name;
  41.                 this.father = father;
  42.                 this.mother = mother;
  43.                 this.work = work;
  44.         }

  45.         public void showPerson()
  46.         {
  47.                
  48.                 System.out.print("我是"+name);

  49.                 if (null != work)
  50.                 {
  51.                         System.out.print(",我会工作");
  52.                         for (int i = 0;i < work.length  ;i++ )
  53.                         {
  54.                                 System.out.print(work[i]);
  55.                                 if (work.length - 1 != i)
  56.                                 {
  57.                                         System.out.print("、");
  58.                                 }
  59.                         }
  60.                 }
  61.                 System.out.println();
  62.                 System.out.println();

  63.                 if (null != father)
  64.                 {
  65.                         System.out.println(name + "的爸爸:");
  66.                         father.showPerson();
  67.                 }
  68.                 if (null != mother)
  69.                 {
  70.                         System.out.println(name + "的妈妈:");
  71.                         mother.showPerson();
  72.                 }

  73.                
  74.         }

  75.         public void setName(String name)
  76.         {
  77.                 this.name = name;
  78.         }
  79.         public String getName()
  80.         {
  81.                 return this.name;
  82.         }

  83.         public void setFather(Person father)
  84.         {
  85.                 this.father = father;
  86.         }
  87.         public Person getFather()
  88.         {
  89.                 return father;
  90.         }

  91.         public void setMother(Person mother)
  92.         {
  93.                 this.mother = mother;
  94.         }
  95.         public Person getMother()
  96.         {
  97.                 return mother;
  98.         }
  99. }
复制代码
回复 使用道具 举报
djbcool 发表于 2015-3-15 03:16
按您的意思改了以后,是这个意思么,老师

写的不错,可以作为参考答案:
  1. package YangGe31;


  2. /*
  3. * 题目:请用java中面向对象的思想用代码描述如下内容:小红(女)有爸爸和妈妈,爸爸和妈妈分别有自己的爸爸和妈妈。。。,小红的爸爸会做工作A和B,小红的妈妈会做工作C和D。
  4. */
  5. public class XiaoHong {
  6.         public static void main(String[] args) {
  7.                 Father father = new Father("male");
  8.                 Mother mother = new Mother("female");
  9.                
  10.                 father.jobA();
  11.                 father.jobB();
  12.                 mother.jobC();
  13.                 mother.jobD();
  14.                
  15.                 Person xiaoHong = new Person("female", father, mother);
  16.                
  17.                 System.out.println("XiaoHong is a " + xiaoHong.sex);
  18.         }       
  19. }

  20. class Person {
  21.         String sex;
  22.        
  23.         public Person(String sex, Object Father, Object Mother) {
  24.                 this.sex = sex;
  25.         }
  26.        
  27.         public Person(String sex) {
  28.                 this.sex = sex;
  29.         }
  30. }

  31. class Mother extends Person implements JobC, JobD {
  32.        
  33.         public Mother(String sex) {
  34.                 super(sex);
  35.         }

  36.         public void jobC() {
  37.                 System.out.println("Mother works jobC");
  38.         }
  39.        
  40.         public void jobD() {
  41.                 System.out.println("Mother works jobD");
  42.         }
  43. }       

  44. class Father extends Person implements JobA, JobB {

  45.         public Father(String sex) {
  46.                 super(sex);
  47.         }

  48.         public void jobA() {
  49.                 System.out.println("Father works jobA");
  50.         }
  51.        
  52.         public void jobB() {
  53.                 System.out.println("Father works jobB");
  54.         }
  55. }

  56. interface JobA {
  57.         public void jobA();
  58. }

  59. interface JobB {
  60.         public void jobB();
  61. }

  62. interface JobC {
  63.         public void jobC();
  64. }

  65. interface JobD {
  66.         public void jobD();
  67. }



复制代码
回复 使用道具 举报
as604049322 发表于 2015-3-14 23:29
败在这题了~~~使用jdk1.8关于接口的新特性,望老师查阅,还有上一期我的高效的代码(最大子序列)
...

老师啊,person的构造必须有另2个person类完成,我必须得造2个没有父母的祖先才行呀,为了简单就当小红的父母是本来就存在的人类祖先了,,,
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马