Java向下转型的条件判断

博客通过一个示例介绍了 Java 中的向上转型和向下转型。向上转型时父类引用指向子类实例,可调用子类重写及父类派生方法,不能调用子类独有方法,静态方法只能用父类原有;向下转型子类引用指向父类实例,需强转,可调用子类独有方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们来看下面一个例子:

animal:

package com.imooc.animal;

//抽象类:不允许实例化,可以通过向上转型,指向子类实例
public abstract class Animal {
	//属性:昵称、年龄
	private String name;
	private int month;
	
	public Animal(){
		
	}
	
	public Animal(String name,int month){
		this.name=name;
		this.month=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;
	}
	
	//方法:吃东西
	//抽象方法:不允许包含方法体;子类中需要重写父类的抽象方法,否则,子类也是抽象类
	//static final private不能与abstract并存
	public abstract void eat();
	
	public static void say(){
		System.out.println("animal相互打招呼");
	}
}

Dog:

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("狗喜欢吃骨头~");
		
	}
}

Cat:

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.setMonth(month);
        this.weight=weight;
    }

    public double getWeight()
    {
        return weight;
    }

    public void setWeight(double weight)
    {
        this.weight = weight;
    }


    //方法:跑动
    public void run()
    {
        System.out.println("小猫喜欢跑来跑去");
    }


    //方法:吃东西(重写父类方法)
    @Override
    public void eat()
    {
        System.out.println("猫喜欢吃小鱼干~");
    }

    public static void say()
    {
        System.out.println("小猫通过碰胡须打招呼");
    }

    public void playBall()
    {
        // TODO Auto-generated method stub
        System.out.println("小猫喜欢玩球");
    }
}

Test:

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) {
	        TODO Auto-generated method stub
		Animal one=new Animal();
		/*向上转型、隐式转型、自动转型
		父类引用指向子类实例,可以调用子类重写父类的方法以及父类派生的方法,无法调用子类
独有方法
		注意:父类中的静态方法无法被子类重写,所以向上转型之后,只能调用到父类原有的静态
方法
		小类转型为大类
		*/
		Animal two=new Cat();
		two.say();
		Cat cat=(Cat)two;
		cat.say();

		Cat cat=new Cat();
		two=cat;
		Animal three=new Dog();//3
		
		one.eat();
		two.eat();
		two.setMonth(2);
		two.getMonth();
		two.run();
		three.eat();
		System.out.println("=================");
		/*向下转型、强制类型转换
		 * 子类引用指向父类对象,此处必须进行强转,可以调用子类特有的方法
		 * 必须满足转型条件才能进行强转
		 * instanceof运算符:返回true/false
		 */
		if(two instanceof Cat){
			Cat temp=(Cat)two;
			temp.eat();
			temp.run();
			temp.getMonth();
			System.out.println("two可以转换为Cat类型");
		}
	
		if(two instanceof Dog){
			Dog temp2=(Dog)two;
			temp2.eat();
			temp2.sleep();
			temp2.getSex();
			System.out.println("two可以转换为Dog类型");
		}

		if(two instanceof Animal){
			System.out.println("Animal");
		}
		
		if(two instanceof Object){
			System.out.println("Object");/		}
		
	}
	
	
	
	
	

}

 

我们可以看到:

向上转型时时父类引用指向子类实例,可以调用子类重写父类以及父类派生的方法,无法调用子类独有办法,父类中的静态方法无法被子类重写,向上转型后只能调用父类原有的静态方法

向下转型时子类引用指向父类实例,必须用强转,可以调用子类独有的方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值