标签:int 多态 System 编译 引用 Human 类型 运行 out
编译时多态
/**
* 重载的好处是,用起来爽
*/
public class Dog {
public int add( int a , int b) {
System.out.println( "int add( int , int )" );
return a + b ;
}
public int add( int a , int b , int c ) {
System.out.println( "int add( int , int , int)" );
return a + b + c ;
}
public long add( long a , long b ) {
System.out.println( "long add( long , long )" );
return a + b ;
}
public static void main(String[] args) {
Dog d = new Dog();
d.add( 100 , 200 );
d.add( 100L , 200 );
}
}
运行时多态
/**
* 1、体验运行时多态:
* 在运行期间,引用变量 引用了 (指向了) 哪一个子类类型的实例,将来调用方法时就调用哪个实例的方法
*
* 2、编译时类型决定了可以通过 引用变量 来访问哪些字段、调用哪些方法,
* 因为子类重写了父类中的方法,因此父类类型的引用变量可以调用的方法在子类实例中是存在的
*
* 3、了解 动态绑定
*/
public class HumanTest {
public static void main(String[] args) {
Human h = null ; // 声明一个 Human 类型的引用变量 h 并为其赋值为 null
System.out.println( "h=> " + h );
h = new Human(); // Human 类型的引用变量 引用了 (指向了) 一个 Human 实例
System.out.println( "h=> " + h );
h.eat( "鸡腿" );
// 父类类型的引用变量 引用了 (指向了) 子类类型的对象
h = new Sinaean(); // Human 类型的引用变量 引用了 (指向了) 一个 Sinaean 实例
System.out.println( "h=> " + h );
h.eat( "米饭" );
// 父类类型的引用变量 引用了 (指向了) 子类类型的对象
h = new British(); // Human 类型的引用变量 引用了 (指向了) 一个 British 实例
System.out.println( "h=> " + h );
h.eat( "包子" );
// 父类类型的引用变量 引用了 (指向了) 子类类型的对象
h = new India(); // Human 类型的引用变量 引用了 (指向了) 一个 India 实例
System.out.println( "h=> " + h );
h.eat( "火锅" );
}
}
引用类型的强制类型转换
/**
* 引用类型的强制类型转换
*/
public class HumanTest3 {
public static void main(String[] args) {
// 父类类型的引用变量 指向 子类类型的对象
Object o = new Sinaean();
// o.eat( "藜蒿炒腊肉" ); //【错误】变量 o 的编译时类型是 Object ,其中并没有声明 eat 方法
if( o instanceof Human ) {
Human h = (Human) o ;
h.eat( "藜蒿炒腊肉" ); // 变量 h 的编译时类型是 Human ,其中是包含被调用的 eat 方法
System.out.println( o == h ); // true
}
if( o instanceof Sinaean ) {
Sinaean s = (Sinaean) o ;
s.eat( "宫爆鸡丁" );
s.taiChi(); // 变量 s 的编译时类型是 Sinaean ,其中是包含被调用的 taiChi 方法
System.out.println( o == s ); // true
}
}
}
获取运用时类型
/** * 1、编译时类型【 设计时类型 ,是设计类时为变量指定的类型 】 * 2、运行时类型 可以通过 对象的 getClass() 方法来获取 */
// 变量 h 所指向的对象在堆内存中,该对象的类型可以通过 getClass() 来获取
Class<?> c = h.getClass();
System.out.println( c.getName() );
标签:int,多态,System,编译,引用,Human,类型,运行,out 来源: https://www.cnblogs.com/wxlmdx/p/12968274.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。