5 changed files with 43 additions and 0 deletions
@ -0,0 +1,3 @@ |
|||
public abstract class Animal{ |
|||
public abstract void makeVoice(); |
|||
} |
|||
@ -0,0 +1,6 @@ |
|||
public class Cat extends Animal{ |
|||
@Override |
|||
public void makeVoice(){ |
|||
System.out.println("曼波,曼波,哦马资历———曼波"); |
|||
} |
|||
} |
|||
@ -0,0 +1,11 @@ |
|||
public class Dog extends Animal implements Swimmable{ |
|||
@Override |
|||
public void makeVoice(){ |
|||
System.out.println("汪汪汪"); |
|||
} |
|||
@Override |
|||
public void swim(){ |
|||
System.out.println("狗会游泳"); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,20 @@ |
|||
public class Main { |
|||
public void main(){ |
|||
Animal cat = new Cat(); |
|||
Animal dago = new Dog(); |
|||
cat.makeVoice(); |
|||
dago.makeVoice(); |
|||
if(cat instanceof Swimmable){ |
|||
System.out.println("猫会游泳"); |
|||
} |
|||
else{ |
|||
System.out.println("猫不会游泳"); |
|||
if (dago instanceof Swimmable){ |
|||
System.out.println("大狗会游泳"); |
|||
} |
|||
else{ |
|||
System.out.println("大狗不会游泳"); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,3 @@ |
|||
public interface Swimmable { |
|||
public abstract void swim(); |
|||
} |
|||
Loading…
Reference in new issue