zoukankan      html  css  js  c++  java
  • 面向对象-多态

     最重要的一句话:父类的引用,指向子类的对象

    多态的三个重要条件:
    1.要有继承
    2.要有重写
    3.父类的引用指向子类对象
    多态的分类:
    1.person为父类,student为子类。那么:person p=new student();
    2.fliable为接口,bird为实现接口的类,那么:fliable f=new bird();
    3.fliable为抽象类,bird为继承fliable的类,那么:fliablef=new bird();

    例子:

    package duotai;
    
    public class Duotai {
    
        public static void main(String[] args) {
            Fu child = new Zi();//父类的引用    指向子类对象
            child.Scream();
        }
    
    }
    
    
    
    class Fu{ //public 修饰符 (此处不能加)一个Java文件中最外层只能有一个
        public void Scream(){
            System.out.println("父类的");
        }
    }
    class Zi extends Fu{
        public void Scream(){ //重写了父类的Scream方法
            System.out.println("子类的");
        }
    }
  • 相关阅读:
    9.17考试
    Something
    tesuto-Mobius
    7.22考试
    填坑...P1546 最短网络 Agri-Net
    P1125 笨小猴
    P2822 组合数问题
    致我们曾经刷过的水题
    Luogu P1186 玛丽卡
    Luogu P1726 上白泽慧音
  • 原文地址:https://www.cnblogs.com/donghb/p/7216757.html
Copyright © 2011-2022 走看看