zoukankan      html  css  js  c++  java
  • Java(16):多态性

    package zzz;
    
    //    《创建多态》
    
    public class Demo1 {
    
        public static void main(String[] args) {
            
            Fish fish = new Fish();
            fish.eat();
    
            Rice rice = new Rice();
            rice.eatSomething();
            
            Apple apple = new Apple();
            apple.eatFruit();
        }
    
    }
    
    class Food {
        protected String food = "food";
        public void eat() {
            System.out.println("eat "+food);
        }
    }
    
    class Fish extends Food {
        private String food = "fish";
        @Override
        public void eat() {
            System.out.println("eat "+ super.food + ", such as "+this.food);
        }
    }
    
    abstract class Something {
        protected String something = "something";
        public abstract void eatSomething();
    }
    
    class Rice extends Something {
        private String something = "rice";
        public void eatSomething() {
            System.out.println("eat "+super.something+", such as "+this.something);
        }
    }
    
    interface Fruit {
    //    fruit默认修饰符 public static final 
        String fruit = "fruit";
        public void eatFruit();
    }
    
    class Apple implements Fruit {
    //    疑问:如何访问Fruit接口里的fruit成员变量?
        private String fruit = "apple";
        @Override
        public void eatFruit() {
            System.out.println("eat "+ this.fruit +" ...");
        }
    }
  • 相关阅读:
    Spring IOC
    C++ 内存模型
    C++ 多态
    Java 多态
    Java 自动装箱与自动拆箱
    C++ priority_queue
    多个页面使用到一些名称类的同一个接口,借助vuex实现
    element-ui自定义表单验证
    vue项目中导出excel文件
    数组对象根据某个属性进行排序
  • 原文地址:https://www.cnblogs.com/kenantongxue/p/14013693.html
Copyright © 2011-2022 走看看