-
Notifications
You must be signed in to change notification settings - Fork 105
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
base: master
Are you sure you want to change the base?
简化代码 #214
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 鸟{ | ||
|
||
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 鸟{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 会叫{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 会叫{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 会叫{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 动物 { | ||
} | ||
} |
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 会飞(); | ||
} |
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,8 @@ | ||
package com.github.hcsp.polymorphism; | ||
|
||
public class 活的动物 implements 新陈代谢的动物 { | ||
@Override | ||
public void 会新陈代谢() { | ||
System.out.println("新陈代谢"); | ||
} | ||
} |
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("鸟儿飞"); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'{' 前应有空格。