尝试创建一个父类,在父类中创建两个方法,在子类中创建覆盖第二个方法,为子类创建一个对象,将向上转型到基类并调用这个方法

class Testmain{
    public static void method1(Testmain q){
        System.out.println(1);
    }
    public static void method2(){
        System.out.println(2);
    }
}
public class Test extends Testmain {
    public static void method2(){
        System.out.println(3);
    }
    public static void main(String args[]){
    Test p=new Test();    
    p.method2();method2是本类的构造方法,p是本类的实例化对象,那么method2也是对象p的成员方法
   method2();//如果改成method2(p)则会报错
   method1(p); //利用向上转型,调用父类方法,对象是子类对象
} }

 

posted on 2015-06-03 20:55  chamie  阅读(186)  评论(0)    收藏  举报