今天下午小龙在练习instanceof用法时突然想到java的upcating 和 downcasting 好像与此有关联,于是自己写了一个相关的案例,在此与众位看到的亲们分享一下,希望能帮到正在学习的伙伴们。
package com.instanceoftest;
/** java向上转型和向下转型,以及用instanceof方法来防止向下转型错误
* 接口说明
*interface方式的实现中,Demo只能够有静态的不能被修改的数据成员(也就是必须是static final 的,不过在interface中一般不定义数据成员),
*所有的成员方法都是abstract的。从某种意义上说,interface是一种特殊形式的 abstract class。
*
* @author mengfl
*
*/
interface Animal{
public static final String color = "blue";
//abstract void voice();
abstract void eat();
}
class FlyAnimal implements Animal{
public void eat() {
// TODO Auto-generated method stub <

本文通过案例探讨了Java中的向上转型(upcasting)和向下转型(downcasting)概念,结合`instanceof`关键字,帮助读者理解这两种转型的区别和应用场景。
和向下转型(downcasting)区别举例详解&spm=1001.2101.3001.5002&articleId=41833939&d=1&t=3&u=32a02edb4e4d498f8176fa6839e1eba9)
1214

被折叠的 条评论
为什么被折叠?



