Java:多态 之 向下转型+Instanceof关键字
2017-10-21 20:04
465 查看
Animal.java:
Cat.java:
test.java:
执行情况
:
![](https://img-blog.csdn.net/20171021200515616)
为解决上面问题:
引入instanceof 关键字:instanceof是Java、php的一个二元操作符(运算符),和==,>,<是同一类东西。由于它是由字母组成的,所以也是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类型");
}
}
}
执行情况:
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是Java、php的一个二元操作符(运算符),和==,>,<是同一类东西。由于它是由字母组成的,所以也是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类型");
}
}
}
执行情况:
相关文章推荐
- 2.25 Java基础总结 ①多态②向上向下转型③instanceof
- 向上转型向下转型,多态,instanceof关键字
- java的多态(向上转型、向下转型)
- Java 多态部分 向上转型 向下转型
- 黑马程序员_Java基础_03面向对象思想(对象转型注意事项、多态、抽象类、final关键字等)
- Java多态(利用向上向下转型和反射机理)
- Java学习之面向对象(1.Object类,(1)toString方法,(2)equals方法,2.对象转型(casting),3.动态绑定与多态,4.抽象类(abstract),5.Final关键字)
- Java多态 向上转型 向下转型
- Java多态的“向上转型”和“向下转型”
- JAVA笔记-多态的意义(向上转型与向下转型)
- Java自学笔记(第八天)-多态-instanceof关键字-equals方法-toString方法-object类-class类-
- JAVA笔记6__抽象类/接口/多态/instanceof关键字、父类设计法则
- java多态向上转型和向下转型
- 重新认识java(五) ---- 面向对象之多态(向上转型与向下转型)
- java多态使用、向上转型、向下转型
- java多态向上、向下转型问题
- Java多态(利用向上向下转型和反射机理)
- instanceof关键字(向下转型的验证)
- Java中多态以及向上转型和向下转型
- JAVA_多态性演示a____自动向上转型、向下转型、检查对象是否为某个类型(instanceof)