zoukankan      html  css  js  c++  java
  • Java:简单的多态实例

    多态:

    多态指同一个实体同时具有多种形式。它是面向对象程序设计(OOP)的一个重要特征。如果一个语言只支持类而不支持多态,只能说明它是基于对象的,而不是面向对象的。
    * 多态是出现在具有继承关系的两个类的对象之间,所以它不像方法重载(发生在一个类中)在编译期间发生(也就是确定下来),而是在运行期间发生(确定下来)。*

    • 一个父类类型的引用可以指向他任何一个子类的对象
    • [相同]类域的[不同]对象执行[同一]方法的时候会有[不同]的表现

    有一个比较经典的多态实例:
    有一个Animal类,它有Cat,和Dog两个子类,在Animal中有个say方法,当Cat调用这个方法的时候输出的是“小猫喵喵喵”,当Dog调用这个方法时,输出的是“小狗汪汪汪”,这就是Java多态的实现。

    /*
    对象的多态性:动物 x = new 猫();
    函数的多态性:函数重载、重写
      
    1、多态的体现
            父类的引用指向了自己的子类对象
            父类的引用也可以接收自己的对象
    2、多态的前提
            必须是类与类之间只有关系,要么继承或实现
            通常还有一个前提,存在覆盖
    3、多态的好处
            多态的出现大大的提高了程序的扩展性
    4、多态的弊端
            只能使用父类的引用访问父类的成员
    5、多态的应用
      
    6、注意事项
    */ 
       
    /*
    需求:
    猫,狗。
    */ 
       
    abstract class Animal 
    { 
        abstract void eat(); 
    } 
       
    class Cat extends Animal 
    { 
        public void eat() 
        { 
            System.out.println("吃鱼"); 
        } 
        public void catchMouse() 
        { 
            System.out.println("抓老鼠"); 
        } 
    } 
       
    class Dog extends Animal 
    { 
        public void eat() 
        { 
            System.out.println("吃骨头"); 
        } 
        public void kanJia() 
        { 
            System.out.println("看家"); 
        } 
    } 
       
    class DuoTaiDemo 
    { 
        public static void main(String[] args) 
        { 
            function(new Cat()); 
            function(new Dog()); 
               
            Animal a = new Cat();//向上转型 
            a.eat(); 
               
            Cat c = (Cat)a;//向下转型 
            c.catchMouse(); 
               
               
        } 
           
        public static void function(Animal a) 
        { 
            a.eat(); 
            //用于子类型有限 
            //或判断所属类型进而使用其特有方法 
            if(a instanceof Cat) 
            { 
                Cat c = (Cat)a; 
                c.catchMouse(); 
            } 
            else if(a instanceof Dog) 
            { 
                Dog c = (Dog)a; 
                c.kanJia(); 
            } 
        }    
    }   
    

      

    这里我们写另外一个:
    —父类:Person.java

    import java.util.Scanner;
    
    public class Person {
        public int salary;
        public int allSalary(){
            return 0;
        }
        public static void main(String[] args) {
            Person p = null;
    
            for (int n = 0; n < 3; n++) {
                @SuppressWarnings("resource")
                Scanner sc = new Scanner(System.in);
                System.out.println("请输入一个1-3的数字:
    1 is Teacher
    2 is Scientis
    3 is Waiter");
                int i = sc.nextInt();
                //分别指向了不同的子类,输出的结果是不一样的
                if (i == 1) {
                    p = new Teacher(5000);
                    System.out.println(p.allSalary());
                } else if (i == 2) {
                    p = new Scientist(5000);
                    System.out.println(p.allSalary());
                } else if (i == 3) {
                    p = new Waiter(5000);
                    System.out.println(p.allSalary());
                } else {
                    System.out.println("?
    请输入1-3");
                }
    
            }
    }
    }
    

    —子类:Scientist .java

    public class Scientist extends Person{
        public Scientist(int salary){
            this.salary = salary;
        }
    
        public int allSalary(){
            int aa = salary*12+36500;
            System.out.print("五五开一年的工资:");
            return aa;
        }
    }
    

    Waiter.java

    public class Waiter extends Person{
        public Waiter(int salary){
            this.salary = salary;
        }
    
        public int allSalary(){
            int aa = salary*12;
            System.out.print("服务员一年的工资:");
            return aa;
        }
    }
    

    Teacher .java

    public class Teacher extends Person{
        public Teacher(int salary){
            this.salary = salary;
        }
    
        public int allSalary(){
            int aa = salary*12+3650;
            System.out.print("教师一年的工资:");
            return aa;
        }
    }
    

      

      

      

      

    声明 欢迎转载,但请保留文章原始出处:) 博客园:https://www.cnblogs.com/chenxiaomeng/ 如出现转载未声明 将追究法律责任~谢谢合作
  • 相关阅读:
    ffmpeg+EasyDSS流媒体服务器实现稳定的rtmp推流直播
    ffmpeg+EasyDSS流媒体服务器实现稳定的rtmp推流直播
    EasyPlayerPro安卓流媒体播放器实现Android H.265硬解码流程
    EasyPlayerPro安卓流媒体播放器实现Android H.265硬解码流程
    EasyPlayer RTSP安卓Android播放器架构简析
    EasyPlayer RTSP安卓Android播放器架构简析
    解决RTMP推送时间戳问题引起HLS切片不均匀导致手机浏览器播放卡顿的问题
    解决RTMP推送时间戳问题引起HLS切片不均匀导致手机浏览器播放卡顿的问题
    EasyPusher/EasyDarwin支持H.265 RTSP/RTP直播推流与分发播放
    EasyPusher/EasyDarwin支持H.265 RTSP/RTP直播推流与分发播放
  • 原文地址:https://www.cnblogs.com/chenxiaomeng/p/14441674.html
Copyright © 2011-2022 走看看