写的不错,可以作为参考答案:
- package YangGe31;
- /*
- * 题目:请用java中面向对象的思想用代码描述如下内容:小红(女)有爸爸和妈妈,爸爸和妈妈分别有自己的爸爸和妈妈。。。,小红的爸爸会做工作A和B,小红的妈妈会做工作C和D。
- */
- public class XiaoHong {
- public static void main(String[] args) {
- Father father = new Father("male");
- Mother mother = new Mother("female");
-
- father.jobA();
- father.jobB();
- mother.jobC();
- mother.jobD();
-
- Person xiaoHong = new Person("female", father, mother);
-
- System.out.println("XiaoHong is a " + xiaoHong.sex);
- }
- }
- class Person {
- String sex;
-
- public Person(String sex, Object Father, Object Mother) {
- this.sex = sex;
- }
-
- public Person(String sex) {
- this.sex = sex;
- }
- }
- class Mother extends Person implements JobC, JobD {
-
- public Mother(String sex) {
- super(sex);
- }
- public void jobC() {
- System.out.println("Mother works jobC");
- }
-
- public void jobD() {
- System.out.println("Mother works jobD");
- }
- }
- class Father extends Person implements JobA, JobB {
- public Father(String sex) {
- super(sex);
- }
- public void jobA() {
- System.out.println("Father works jobA");
- }
-
- public void jobB() {
- System.out.println("Father works jobB");
- }
- }
- interface JobA {
- public void jobA();
- }
- interface JobB {
- public void jobB();
- }
- interface JobC {
- public void jobC();
- }
- interface JobD {
- public void jobD();
- }
复制代码 |