您的位置:首页 > 编程语言 > Java开发

Java:多态 之 向下转型+Instanceof关键字

2017-10-21 20:04 465 查看
Animal.java:

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);
}

//eat()
public void eat(){
System.out.println("动物都又吃东西的能力!");
}

//getter/setter
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;
}

}


Cat.java:

package com.imooc.animal;

public class Cat extends Animal {

private String name;
private int month;
private double weight;

public Cat(){

}

public Cat(String name,int month,double weight){
super(name,month);//super父类属性构造方法赋值
setWeight(weight);
}

//run
public void run(){
System.out.println("猫会跑");
}

//方法重写
@Override
public void eat() {
System.out.println("猫吃鱼!");
}

//getter/setter
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 double getWeight() {
return weight;
}

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

}


test.java:

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();//1普通创建实例
one.eat();

Animal two=new Cat();//2.向上转型
two.eat();
System.out.println("=====================");

/*
* 向下转型(强制转型)
* 子类引用指向父类的实例(对象),此处必须今习惯强制类型转换
* 该实例可以调用子类特有的方法
* 必须满足转型条件才能转换()
*/
Cat temp=(Cat)two;//3.向下转型,原本two是Animal类型,强制类型转换为Cat
temp.eat();
temp.run();
temp.setMonth(3);
System.out.println("年龄:"+temp.getMonth());

/*
Dog temp2=(Dog)two;
temp2.eat();
temp2.sleep();
temp2.getSex();
以上的操作虽然系统不会报错,但是运行抛出异常,原因:two在创建时究其根本是指向Cat(),所以上面类缩小到cat可以但是转成dog就不行
*/
}

}


执行情况



为解决上面问题:

引入instanceof 关键字:instanceof是Javaphp的一个二元操作符(运算符),和==,>,<是同一类东西。由于它是由字母组成的,所以也是Java的保留关键字。它的作用是判断其左边对象是否为其右边类的实例,返回boolean类型的数据。可以用来判断继承中的子类的实例是否为父类的实现。

instanceof一般放在类型转换的前面,合理规避异常产生!

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();//1普通创建实例
one.eat();

Animal two=new Cat();//2.向上转型
two.eat();
System.out.println("=====================");

/*
* 向下转型(强制转型)
* 子类引用指向父类的实例(对象),此处必须今习惯强制类型转换
* 该实例可以调用子类特有的方法
* 必须满足转型条件才能转换(),子类间不能随意强制转换,但是子类引用指向父类实例,可以强制转换
* instanceof运算符:返回true/false,
*/

if(two instanceof Cat){
Cat temp=(Cat)two;//3.向下转型,原本two是Animal类型,强制类型转换为Cat
temp.eat();
temp.run();
temp.setMonth(3);
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("two可以转换为Animal类型");
}

if(two instanceof Object){
System.out.println("two可以转换为Object类型");
}

}

}


执行情况:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: