• 多态and内部类(java)


    多态

    父类作为方法的参数 掌握

    package com.qfedu.duotai3;
    
    /**
     * 奥特曼欺负各种各样的小动物
     * @author renrui
     *
     */
    public class App {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    
    		Outman laotie = new Outman();
    		
    		Dog dog = new Dog();
    		Cat cat = new Cat();
    		
    		laotie.beatAnimal(dog);
    		laotie.beatAnimal(cat);
    		
    	}
    
    }
    
    class Outman {
    	
    	// 方法的参数用的是父类的类型
    	public void beatAnimal(Animal animal) {
    		animal.beaten();
    	}
    	
    }
    
    abstract class Animal {
    	public abstract void beaten();
    }
    
    class Dog extends Animal {
    	
    	// 被打
    	@Override
    	public void beaten() {
    		System.out.println("奥特曼都敢欺负到我头上了,咬死你");
    	}
    }
    
    class Cat extends Animal {
    	
    	@Override
    	public void beaten() {
    		System.out.println("敢打我,挠花你的脸");
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    父类或者接口作为返回

    简单工厂模式

    ​ 创建对象,要创建的对象由相同的父类,或者有相同接口

    ​ 根据不同状态,创建不同的对象

    package com.qfedu.factory;
    
    public class App {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    
    		// 通过接口类型的变量引用不同实现类的对象
    		IPay aliPay = PayFactory.createPay(PayConstants.PAY_TYPE_ALI);
    		aliPay.pay();
    		
    		IPay wxPay = PayFactory.createPay(PayConstants.PAY_TYPE_WX);
    		wxPay.pay();
    	}
    
    }
    
    interface IPay {
    	// 支付
    	void pay();
    }
    
    class WxPay implements IPay {
    
    	@Override
    	public void pay() {
    		// TODO Auto-generated method stub
    		System.out.println("使用微信支付");
    	}
    	
    }
    
    class AliPay implements IPay {
    
    	@Override
    	public void pay() {
    		// TODO Auto-generated method stub
    		System.out.println("使用支付宝支付");
    	}
    	
    }
    
    // 简单工厂设计模式
    // 体现多态思想
    // 工厂类,根据不同的状态,返回不同的支付对象
    class PayFactory {
    	// 1 表示支付宝 2表示微信
    	public static IPay createPay(int payType) {
    		if(payType == PayConstants.PAY_TYPE_ALI) {
    			IPay aliPay = new AliPay();
    			return aliPay;
    			// return new AliPay();
    		} else if(payType == PayConstants.PAY_TYPE_WX) {
    			IPay wxPay = new WxPay();
    			return wxPay;
    			// return new WxPay();
    		} else {
    			System.out.println("参数有误");
    			return null;
    		}
    	}
    }
    // 常量类
    class PayConstants {
    	// 支付宝支付
    	public static final int PAY_TYPE_ALI = 1;
    	
    	// 微信支付
    	public static final int PAY_TYPE_WX = 2;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71

    微信支付

    支付宝支付

    不会写两个不同的方法:

    payWithWx()

    payWithAlipay()

    payWithPaypal()

    payWIthIosNeigou()

    内部类 了解

    ​ 成员内部类、静态内部类、局部内部类 了解

    匿名内部类 掌握

    基本语法:

    new 接口或者抽象类() {

    ​ 重写抽象方法

    };

    很多情况下,将匿名内部类作为参数使用

    package com.qfedu.anymous2;
    
    public class App {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    
    		Outman outman = new Outman();
    		Animal dog = new Animal() {
    			
    			@Override
    			public void beaten() {
    				// TODO Auto-generated method stub
    				System.out.println("wang");
    			}
    		};
    		outman.beatAnimal(dog);
    		
    		// 匿名内部类作为参数进行传递
    		outman.beatAnimal(new Animal() {
    			
    			@Override
    			public void beaten() {
    				// TODO Auto-generated method stub
    				System.out.println("miao");
    			}
    		});
    		
    		outman.beatAnimal(new Animal() {
    			
    			@Override
    			public void beaten() {
    				// TODO Auto-generated method stub
    				System.out.println("嗷呜");
    			}
    		});
    		
    	}
    
    }
    
    abstract class Animal {
    	public abstract void beaten();
    }
    
    class Outman {
    	
    	public void beatAnimal(Animal animal) {
    		animal.beaten();
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
  • 相关阅读:
    linux设置ip地址与换源
    提问为什么hive中ads层建表一直出错
    WDC西部数据闪存业务救赎之路,会成功吗?
    大数据与人工智能的交融:向量数据库在具体应用案例中的探索
    制作炫酷个人网页:用 HTML 和 CSS3 展现你的风格
    JVM 内存设置大小(Xms Xmx PermSize MaxPermSize 区别)
    R语言使用dplyr包的arrange函数进行dataframe排序、arrange函数基于一个字段(变量)进行升序排序实战(默认升序)
    java计算机毕业设计ssm高校工资管理系统
    【MySQL功法】第2话 · 数据库与数据表的基本操作
    【无标题】
  • 原文地址:https://blog.csdn.net/weixin_51423778/article/details/126908292