java 父类方法的覆盖,多态的向下转型示例

package priv;
/*
 本方法实现的是:
 子类方法对父类方法的覆盖。
 多态,要用子类特有的方法要向下转型
 */
public class Father {

	public static void main(String[] args) 
	{
		Father1 a=new Son1();//多态
		a.test1();
		a.test2();
		Son1 b=(Son1)a;//向下转型
		b.test3();
	}

}
class Father1
{
	void test1() 
	{
		System.out.println("方法一");
	}
	void test2() 
	{
		System.out.println("方法二");
	}
}
class Son1 extends Father1
{
	void test2() 
	{
		System.out.println("方法三");
	}
	void test3() 
	{
		System.out.println("方法四");
	}
}

猜你喜欢

转载自blog.csdn.net/qq_41901915/article/details/81172524
今日推荐