File tree Expand file tree Collapse file tree 7 files changed +88
-25
lines changed
Expand file tree Collapse file tree 7 files changed +88
-25
lines changed Original file line number Diff line number Diff line change 11<?xml version =" 1.0" encoding =" UTF-8" ?>
22<projectDescription >
3- <name >JavaBase </name >
3+ <name >JavaBaseCrazy </name >
44 <comment ></comment >
55 <projects >
66 </projects >
Original file line number Diff line number Diff line change 11package com .xinan .testoop ;
22
33public class Dog extends Pet {
4+ public Dog () {
5+ super ();
6+ }
7+
8+ public Dog (String name ) {
9+ super (name );
10+ }
11+
12+ public Dog (String name , int age , int love ) {
13+ super (name , age , love );
14+ }
15+ public void eat (){
16+ System .out .println ("小狗吃东西" );
17+ }
418 public void catchingFlyDisc (){
5- System .out .println ("" );
19+ System .out .println ("小狗玩飞碟 " );
620 }
721}
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 11package com .xinan .testoop ;
22
33public class Master {
4-
5- public Pet getPet (String typeId ){
6- if (typeId .equalsIgnoreCase ("dog" )){
7- Pet pet =new Dog ();
8- return pet ;
9- }
10- else {
11- return null ;
12- }
4+ public void play (Pet pet ){//使用父类Pet pet作为方法形参实现多态
5+ if (pet instanceof Dog ){
6+ Dog dog =(Dog ) pet ;
7+ dog .catchingFlyDisc ();
8+ }
9+ else if (pet instanceof Penguin ){
10+ Penguin penguin =(Penguin ) pet ;
11+ penguin .swimming ();
12+ }
13+ else {
14+ System .out .println ("您输入的有误" );
15+ }
16+ }
17+ public Pet getPet (String typeId ){
18+ if (typeId .equalsIgnoreCase ("dog" )){//equalsIgnoreCase忽略大小写
19+ Pet pet =new Dog ();
20+ return pet ;
21+ }
22+ else if (typeId .equalsIgnoreCase ("Penguin" )){
23+ Pet pet =new Penguin ();
24+ return pet ;
25+ }
26+ else {
27+ System .out .println ("您输入的有误" );
28+ return null ;
1329 }
1430 }
31+ }
1532
Original file line number Diff line number Diff line change 11package com .xinan .testoop ;
22
3- public class Penguin extends Pet {
4- public void swimming (){
5- System .out .println ("" );
3+ public class Penguin extends Pet {
4+
5+ public Penguin () {
6+ super ();
7+ }
8+
9+ public Penguin (String name ) {
10+ super (name );
11+ }
12+
13+ public Penguin (String name , int age , int love ) {
14+ super (name , age , love );
15+ }
16+
17+ public void eat () {
18+ System .out .println ("Penguin eat!" );
19+ }
20+
21+ public void swimming () {
22+ System .out .println ("和企鹅玩游泳!!" );
623 }
7- }
24+ }
Original file line number Diff line number Diff line change 11package com .xinan .testoop ;
22
33public abstract class Pet {
4-
4+ String name ;
5+ int age =50 ;
6+ private int love =100 ;
7+ public Pet (){}
8+ public Pet (String name ){
9+ super ();
10+ this .name =name ;
11+ }
12+ public Pet (String name ,int age ,int love ){
13+ super ();
14+ this .name =name ;
15+ this .age =age ;
16+ this .love =love ;
17+ }
18+ public final void print (){
19+ System .out .println ("name " +name +"age" +age +"love" +love );
20+ }
21+ public void eat (){
22+ System .out .println ("pet eat" );
23+ }
524}
Original file line number Diff line number Diff line change 1+ package com .xinan .testoop ;
2+
3+ public class Test {
4+
5+ }
You can’t perform that action at this time.
0 commit comments