JAVA基础之多态

一、定义

​ 允许不同类的对象对同一个消息做出不同的响应

比如你是一个酒神,对酒情有独钟。某日回家发现桌上有几个杯子里面都装了白酒,从外面看我们是不可能知道这是些什么酒,只有喝了之后才能够猜出来是何种酒。你一喝,这是剑南春、再喝这是五粮液、再喝这是酒鬼酒….在这里我们可以描述成如下:

  酒 a = 剑南春

  酒 b = 五粮液

  酒 c = 酒鬼酒

  …

  这里所表现的的就是多态。剑南春、五粮液、酒鬼酒都是酒的子类,我们只是通过酒这一个父类就能够引用不同的子类
  这就是多态——我们只有在运行的时候才会知道引用变量所指向的具体实例对象。

二、必要条件

1. 满足继承关系
2. 父类引用指向子类对象

三、向上转型和向下转型

  • 父类(Animal类)

    package com.imooc.animal;
    
    public class Animal {
    	private String name;
    	private int month;
    	
    	public Animal() {
    		 
    	}
    	
    	public Animal(String name,int month) {
    		this.setName(name);
    		this.setMonth(month);
    	}
    	
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public int getMonth() {
    		return month;
    	}
    
    	public void setMonth(int month) {
    		this.month = month;
    	}
    
    	public void eat() {
    		System.out.println("动物都有吃东西的能力");
    	}
    }
    
    
  • 子类(Cat和Dog)

    package com.imooc.animal;
    
    public class Cat extends Animal{
    	private double weight;
    	public Cat() {
    		
    	}
    	public Cat(String name,int month,double weight) {
    		super(name,month);
    		this.setWeight(weight);
    	}
    	public double getWeight() {
    		return weight;
    	}
    	public void setWeight(double weight) {
    		this.weight = weight;
    	}
    	
    	public void run() {
    		System.out.println("小猫快乐的奔跑");
    	}
    	
    	/**
    	 * 重写eat方法
    	 */
    	@Override
    	public void eat() {
    		System.out.println("猫吃鱼");
    	}
    	
    	
    }
    
    
    package com.imooc.animal;
    
    public class Dog extends Animal {
    
    	private String sex;
    	
    	public Dog() {
    		
    	}
    	public Dog(String name,int month,String sex) {
    		this.setMonth(month);
    		this.setName(name);
    		this.setSex(sex);
    	}
    	public String getSex() {
    		return sex;
    	}
    
    	public void setSex(String sex) {
    		this.sex = sex;
    	}
    
    	
    	public void sleep() {
    		System.out.println("狗在睡觉");
    	}
    	
    	@Override
    	public void eat() {
    		System.out.println("狗吃肉");
    	}
    }
    
    
  • 主函数(向上转型和向下转型)

    package com.imooc.test;
    
    import com.imooc.animal.Animal;
    import com.imooc.animal.Cat;
    import com.imooc.animal.Dog;
    
    public class Test {
    
    	public static void main(String[] args) {
    		Animal one=new Animal();
    		/*
    		 * 向上转型(隐式转型,自动转型)
    		 小类转大类
    		 * 父类引用指向子类实例
    		 * 可以调用子类重写父类的方法和父类原本的方法
    		 * 但是无法调用子类独有的方法
    		 * 下面就是多态实例(Animal类呈现出猫和狗的多态形式)
    		 * */
    		Animal two=new Cat();
    		Animal three=new Dog();
    		Animal test=new Animal();
    		
    		one.eat();
    		two.eat();
    		three.eat();
    		System.out.println("-------------------");
    		/*向下转换(强制类型转换)
    		 * 大类转小类
    		 * 子类指向 父类的对象
    		 * 转换后可以调用子类特有的对象
    		 * 必须满足某种条件才能转换
    		 * 下面能转换是因为,two原本就是指向Cat类,向上转换之后向下转换回来
    		 * */
    		Cat four=(Cat)two;
    		four.eat();
    		four.run();
    		System.out.println("-------------------");
    		/*
    		 * 下列无法转换
    		 * 因为test原本指向Animal类,无法强转为Cat类
    		 * 可以借用instanceof来判断
    		 * */
    		Cat five=(Cat)test;
    		five.eat();
    		five.run();
    		
    		
    	}
    
    }
    
    

四、instanceof

A instanceof B;//判断A是否是B类的实例化对象,返回Boolean值
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值