Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

简化代码 #214

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 29 additions & 79 deletions src/main/java/com/github/hcsp/polymorphism/World.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,129 +11,79 @@ public class World {
// 在建造成类型体系后,请尝试化简这个啰嗦的方法,体会多态带来的好处
public static void 会飞的东西飞() {
for (Object obj : objects) {
if (obj instanceof 麻雀) {
((麻雀) obj).飞();
} else if (obj instanceof 喜鹊) {
((喜鹊) obj).飞();
} else if (obj instanceof 蝴蝶) {
((蝴蝶) obj).飞();
} else if (obj instanceof 飞机) {
((飞机) obj).飞();
if (obj instanceof 会飞的东西) {
((麻雀) obj).会飞();
}
}
}
// 在建造成类型体系后,请尝试化简这个啰嗦的方法,体会多态带来的好处
public static void 会叫的东西叫() {
for (Object obj : objects) {
if (obj instanceof 麻雀) {
((麻雀) obj).叫();
} else if (obj instanceof 喜鹊) {
((喜鹊) obj).叫();
} else if (obj instanceof 救护车) {
((救护车) obj).叫();
} else if (obj instanceof 猫) {
((猫) obj).叫();
} else if (obj instanceof 狗) {
((狗) obj).叫();
if (obj instanceof 会叫) {
((会叫) obj).会叫();
}
}
}
// 在建造成类型体系后,请尝试化简这个啰嗦的方法,体会多态带来的好处
public static void 动物都能新陈代谢() {
for (Object obj : objects) {
if (obj instanceof 麻雀) {
((麻雀) obj).新陈代谢();
} else if (obj instanceof 喜鹊) {
((喜鹊) obj).新陈代谢();
} else if (obj instanceof 蝴蝶) {
((蝴蝶) obj).新陈代谢();
} else if (obj instanceof 猫) {
((猫) obj).新陈代谢();
} else if (obj instanceof 狗) {
((狗) obj).新陈代谢();
if (obj instanceof 新陈代谢的动物) {
((新陈代谢的动物) obj).会新陈代谢();
}
}
}

static class 麻雀 {
public void 新陈代谢() {
System.out.println("新陈代谢");
}

public void 飞() {
System.out.println("鸟儿飞");
}
static class 麻雀 extends 鸟{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'{' 前应有空格。


public void 叫() {
System.out.println("叽叽喳喳");
}
}

static class 喜鹊 {
public void 新陈代谢() {
System.out.println("新陈代谢");
}

public void 飞() {
System.out.println("鸟儿飞");
}

public void 叫() {
System.out.println("叽叽喳喳");
}
static class 喜鹊 extends 鸟{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'{' 前应有空格。

}

static class 蝴蝶 {
public void 新陈代谢() {
System.out.println("新陈代谢");
}

public void 飞() {
static class 蝴蝶 implements 会飞 {
@Override
public void 会飞() {
System.out.println("蝴蝶飞");
}
}

static class 飞机 {
public void 飞() {
static class 飞机 implements 会飞 {
@Override
public void 会飞() {
System.out.println("飞机飞");
}
}

static class 救护车 {
public void 叫() {
static class 救护车 implements 会叫{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'{' 前应有空格。

@Override
public void 会叫() {
System.out.println("哇呜哇呜");
}
}

static class 猫 {
public void 新陈代谢() {
System.out.println("新陈代谢");
}

public void 叫() {
static class 猫 extends 活的动物 implements 会叫{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'{' 前应有空格。

@Override
public void 会叫() {
System.out.println("喵喵喵");
}
}

static class 狗 {
public void 新陈代谢() {
System.out.println("新陈代谢");
}
}

public void 叫() {
static class 狗 extends 活的动物 implements 会叫{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'{' 前应有空格。

@Override
public void 会叫() {
System.out.println("汪汪汪");
}
}

interface 动物 {
void 新陈代谢();


public class 会飞的东西 {
}

interface 会飞的东西 {
void 飞();
public class 会叫的东西 {
}

interface 会叫的东西 {
void 叫();
public class 动物 {
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/github/hcsp/polymorphism/会叫.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.github.hcsp.polymorphism;

public interface 会叫 {
void 会叫();
}
5 changes: 5 additions & 0 deletions src/main/java/com/github/hcsp/polymorphism/会飞.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.github.hcsp.polymorphism;

public interface 会飞 {
void 会飞();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.github.hcsp.polymorphism;

public interface 新陈代谢的动物 {
void 会新陈代谢();
}
8 changes: 8 additions & 0 deletions src/main/java/com/github/hcsp/polymorphism/活的动物.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.github.hcsp.polymorphism;

public class 活的动物 implements 新陈代谢的动物 {
@Override
public void 会新陈代谢() {
System.out.println("新陈代谢");
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/github/hcsp/polymorphism/鸟.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.github.hcsp.polymorphism;

public class 鸟 extends 活的动物 implements 会叫, 会飞 {
@Override
public void 会叫() {
System.out.println("叽叽喳喳");
}

@Override
public void 会飞() {
System.out.println("鸟儿飞");
}
}