zoukankan      html  css  js  c++  java
  • Java-9 面向对象(多态)

    7.多态

    • 要实现多态三个前提:
    1. 有继承关系。
    2. 父类的引用指向子类的对象。
    3. 方法的重写
    
    • 四种调用
    同名的成员变量:用的是父类的
    同名的静态方法:用的是父类的
    同名成员方法: 用的是子类
    无法调用子类中独有的属性和方法。
    同名的静态变量:调用的是父类的
    父类中独有的:用的是父类的
    
    • 两种转型
    向上转型: Person p = new Teacher();强制类型转型。
    向下转型:  Teacher t = (Teacher)p; 父类的引用指向子类的对象
    
    • 示例代码:
    package com.xjk;
    
    public class TestDemo {
    	public static void main(String[] args) {
    		Animal am = new Cat();
    		am.eat();// 猫咪吃东西
    		am.sleep();// 动物睡觉了
    		am.run();// 动物跑了
    		System.out.println(am.num);//10
    		System.out.println(am.age);// 20
    		am.catchMouse();// 报错 因为子类独有
    		System.out.println(am.name);// 报错 因为子类独有
    		Cat ct = (Cat)am; // 向下转型
    		ct.sleep();// 猫在睡觉
    		ct.run();// 动物跑了
    		
    	}
    }
    
    class Animal{
    	int num = 10;
    	static int age = 20;
    	public void eat() {
    		System.out.println("动物吃东西");
    	}
    	public static void sleep() {
    		System.out.println("动物睡觉了");
    	}
    	public void run() {
    		System.out.println("动物跑了");
    	}
    }
    
    
    class Cat extends Animal{
    	int num = 70;
    	static int age = 90;
    	String name = "Tom Cat";
    	public void eat() {
    		System.out.println("猫咪吃东西");
    	}
    	public static void sleep() {
    		System.out.println("猫在睡觉");
    	}
    	public void catchMouse() {
    		System.out.println("猫在抓老鼠");
    	}
    }
    
    • 多态练习
    package com.xjk;
    
    public class DuoTaiDemo {
    	public static void main(String[] args) {
    		// 用父类的引用指向子类的对象
    		HenEgg jd = new HenEgg();
    		DuckEgg yd = new DuckEgg();
    		Eegg ed = new Eegg();
    		Egg[] dan = new Egg[3];
    		dan[0] = jd;
    		dan[1] = yd;
    		dan[2] = ed;
    	}
    }
    
    // HenEgg,DuckEgg,Eegg都继承Egg
    class HenEgg extends Egg{
    	
    }
    
    class DuckEgg extends Egg {
    	
    }
    
    class Eegg extends Egg{
    	
    }
    
    class Egg{
    	
    }
    
    • 多态练习2
    package com.xjk;
    
    
    // 在没有多态情况下:
    public class DuoTaiDemo2 {
    	public static void main(String[] args) {
    		// 传入Cats实例后对象
    		makeCatCry(new Cats());
    		makeDogCry(new Dogs());
    	}
    	public static void makeCatCry(Cats c) {
    		c.cry();
    	}
    	public static void makeDogCry(Dogs d) {
    		d.cry();
    	}
    }
    
    
    class Cats{
    	public void cry() {
    		System.out.println("猫,在叫");
    	}
    }
    
    
    class Dogs{
    	public void cry() {
    		System.out.println("狗,在叫");
    	}
    }
    
    // 显然上面代码过于繁琐,当有多态情况下:
    package com.xjk;
    
    public class DuoTaiDemo2 {
    	public static void main(String[] args) {
    		makeAnimalCry(new Dogs());
    		makeAnimalCry(new Cats());
    	}
    	public static void makeAnimalCry(Animals a) {
    		a.cry();
    	}
    }
    
    class Animals{
    	public void cry() {
    		
    	}
    }
    
    class Dogs extends Animals{
    	public void cry() {
    		System.out.println("狗,在叫");
    	}
    }
    
    class Cats extends Animals{
    	public void cry() {
    		System.out.println("猫,在叫");
    	}
    }
    
  • 相关阅读:
    hadoop
    flume
    IP地址查询
    flinkStreamSQL
    StreamSets
    Algorightm----DynamicProgramming
    google
    vue学习
    使用Webstorm快速启动Vue项目配置
    数据库——关系代数中的除法运算【转】
  • 原文地址:https://www.cnblogs.com/xujunkai/p/13765693.html
Copyright © 2011-2022 走看看