1、继承实例

package com.mingrisoft;

public class Animal {

    public Animal(){
        
    }
    public void cry(){
        
        System.out.println("动物发出叫声!");
    }
    
}
package com.mingrisoft;
    
    public class Dog extends Animal{
        
        public Dog(){
            
        }
        public void cry(){
            
            System.out.println("狗发出旺旺声");
        
    }

}
package com.mingrisoft;

public class Sheep extends Animal{

    
}
package com.mingrisoft;

public class Zoo {
    
    public static void main (String[]args){
        
    Dog dog=new Dog();
    
    System.out.println("执行dog.cry();语句时的输出结果:");
    
    dog.cry();
    
    Sheep sheep=new Sheep();
    
    System.out.println("执行sheep.cry();语句时的输出结果:");
    
    sheep.cry();
}
}

执行dog.cry();语句时的输出结果:
狗发出旺旺声
执行sheep.cry();语句时的输出结果:
动物发出叫声!

posted on 2016-01-19 11:32  悠悠小花  阅读(201)  评论(0编辑  收藏  举报