5 changed files with 43 additions and 0 deletions
@ -0,0 +1,5 @@ |
|||||
|
package W6; |
||||
|
|
||||
|
public abstract class Animal { |
||||
|
public abstract void makeSound(); |
||||
|
} |
||||
@ -0,0 +1,13 @@ |
|||||
|
package W6; |
||||
|
|
||||
|
public class AnimalTest { |
||||
|
public static void main(String args[]){ |
||||
|
Animal dog=new Dog; |
||||
|
Animal cat=new Cat; |
||||
|
dog.makeSound(); |
||||
|
cat.makeSound(); |
||||
|
if (dog instanceof Swimmable){ |
||||
|
((Swimmable)dog).swim(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,8 @@ |
|||||
|
package W6; |
||||
|
|
||||
|
public class Cat extends Animal{ |
||||
|
@Override |
||||
|
public void makeSound(){ |
||||
|
System.out.println("喵喵喵"); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,12 @@ |
|||||
|
package W6; |
||||
|
|
||||
|
public class Dog extends Animal implements Swimmable{ |
||||
|
@Override |
||||
|
public void makeSound(){ |
||||
|
System.out.println("汪汪汪"); |
||||
|
} |
||||
|
@Override |
||||
|
public void swim(){ |
||||
|
System.out.println("狗会游泳"); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,5 @@ |
|||||
|
package W6; |
||||
|
|
||||
|
public interface Swimmable { |
||||
|
void swim(); |
||||
|
} |
||||
Loading…
Reference in new issue