• Java接口


    abstract class Animal
    {  
        int legs;  
        public Animal(int legs){
                 this.legs=legs;
        }
        public Animal(){}
        public abstract void eat();
        public void walk(){
            //System.out.println("动物用"+legs+"走路"); 
        }
        public void play(){
        }
    }
    
    class Spider extends Animal{
          
               public Spider(int legs){
                   this.legs=legs;
               }
              public void  eat(){
                 //System.out.println("动物eat!";   
        }
    }
    
    
    interface Pet{
        public String getName();
        public void setName(String name);
    }
    
    
    class Cat extends Animal implements Pet{
        String name;
        public Cat(){}
        
        public Cat(String name){    
            this.name=name;
         }
    
        @Override 
        public String getName(){
            return this.name;
        }
        @Override
        public void setName(String name) {        
            this.name=name;
        }
    
        public void eat(){    
            System.out.println(name+"喜欢Jerry!");   
        }
        public void play(){    
            System.out.println(name+"喜欢毛线球!");   
        }
    
        
    }
    
    
    class Fish extends Animal implements Pet{
        String name;
        public Fish(){}
        public Fish(String name){    
            this.name=name;
        }
        @Override 
        public void setName(String name){
            this.name=name;
        }
        @Override 
        public String getName(){
            return this.name;
        }
        public void eat(){    
            System.out.println(name+"喜欢浮游生物!");   
        }
        @Override
        public void play(){    
            System.out.println(name+"喜欢水泡泡!");   
        }
        public void walk(){    
            System.out.println(name+"走路不用脚");   
        }
    }
    
    
    public class AnimalsTest{  
        public static void main(String[] args){        
            Cat Tom=new Cat("Tom");    
            Tom.eat();    
            Tom.play();
            Tom.setName("Tom's son");
            Tom.play();
            Fish Nemo=new Fish("Nemo");
            Nemo.eat();
            Nemo.play();
            Nemo.walk();
        }
  • 相关阅读:
    [Python_3] Python 函数 & IO
    [Python_2] Python 基础
    【一首小诗】每一个难捱的日子都是一首诗
    【排序算法】选择排序(Selection sort)
    【排序算法】冒泡排序(Bubble Sort)
    【待补充】[Python_1] Python 安装
    [IDEA_6] IDEA 集成 Python
    MySQL 的 CURD 操作
    [Spark SQL_1] Spark SQL 配置
    MySQL 基础
  • 原文地址:https://www.cnblogs.com/xuhuaiqu/p/4474510.html
Copyright © 2020-2023  润新知