首页 > 编程语言 > 详细

java学习之第五章编程题示例(初学篇)

时间:2014-04-30 04:27:21      阅读:608      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
1 /*
2   Animal.java
3 */
4 package animal;
5 
6 public abstract class Animal {
7  public    abstract void cry();
8  public    abstract String getanimalName();
9 }
View Code
bubuko.com,布布扣
 1 //Dog.java
 2 package animal;
 3 
 4 public class Dog extends Animal
 5 {
 6 
 7     String aa="旺旺";
 8     String dog_name ="旺财";
 9     @Override
10     public void cry() {
11         // TODO Auto-generated method stub
12         System.out.println("小狗的叫声"+aa);    
13     }
14 
15     @Override
16     public String getanimalName() {
17         // TODO Auto-generated method stub
18      return dog_name;
19     } 
20 }
View Code
bubuko.com,布布扣
 1 //Cat.java
 2 package animal;
 3 
 4 public  class Cat extends Animal
 5 {
 6     String aa="喵喵";
 7     String bb="花花";
 8     @Override
 9     public void cry() {
10         // TODO Auto-generated method stub
11      System.out.println("小猫的叫声: "+aa);
12     }
13 
14     @Override
15     public String getanimalName() {
16         // TODO Auto-generated method stub
17         return bb;
18     }
19 }
View Code
bubuko.com,布布扣
 1 //Application.java
 2 package animal;
 3 
 4 public class Application {
 5     public static void main(String args[])
 6     {
 7       Simulator mysimulator =new Simulator();
 8       mysimulator.playSound(new Dog());
 9       mysimulator.playSound(new Cat());
10     }
11 }
View Code
bubuko.com,布布扣
 1 //Simulator.java
 2 package animal;
 3 
 4 public class Simulator {
 5     void playSound(Animal st)
 6     {
 7         System.out.println(st.getanimalName()+"的声音");
 8         st.cry();
 9     }
10 }
View Code

 

java学习之第五章编程题示例(初学篇),布布扣,bubuko.com

java学习之第五章编程题示例(初学篇)

原文:http://www.cnblogs.com/gongxijun/p/3695699.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!