zoukankan      html  css  js  c++  java
  • JAVA基础-多态

    多态 polymophism: 动态绑定, 迟绑定, 指在执行期间(java), 而不是编译期间(javac), 判断所引用对象的实际类型, 根据实际类型调用响应的方法.

    3个条件: 1. 继承 2. 重写 3. 父类引用指向子类对象

    优点: 可扩展

    abstract class Animal {
      private String name;
      Animal(String name) {this.name = name;}
      
      public void enjoy(){
        System.out.println("叫声......");
      }
      
      //public abstract void enjoy();
    }
    
    //abstract 
    class Cat extends Animal { private String eyesColor; Cat(String n,String c) {super(n); eyesColor = c;} public void enjoy() { System.out.println("猫叫声......"); } //public abstract void enjoy(); } class Dog extends Animal { private String furColor; Dog(String n,String c) {super(n); furColor = c;} public void enjoy() { System.out.println("狗叫声......"); } } class Bird extends Animal { Bird() { super("bird"); } public void enjoy() { System.out.println("鸟叫声......"); } } class Lady { private String name; private Animal pet; Lady(String name,Animal pet) { this.name = name; this.pet = pet; } public void myPetEnjoy(){pet.enjoy();} } public class Test { public static void main(String args[]){ Cat c = new Cat("catname","blue"); Dog d = new Dog("dogname","black"); Bird b = new Bird(); Lady l1 = new Lady("l1",c); Lady l2 = new Lady("l2",d); Lady l3 = new Lady("l3",b); l1.myPetEnjoy(); l2.myPetEnjoy(); l3.myPetEnjoy(); } }

      

  • 相关阅读:
    使用cwRsync在Windows的目录之间增量同步文件
    Linux搭建lnmp环境
    ie下文件上传无权访问的问题
    10
    8
    9
    7
    网络爬虫环境配置之的模块安装
    pip的更新问题
    【转】进程与线程的一个简单解释
  • 原文地址:https://www.cnblogs.com/wujixing/p/5328957.html
Copyright © 2011-2022 走看看