Java笔记丨17 多态及虚方法的调用

多态

多态是指一个程序中相同的名字表示不同的含义情况

多态有两种情形

    编译时多态:

        重载(多个同名的不同方法)

        如:p.sayHello(); p.sayHello(“Wang”);

    运行时多态:

        覆盖(子类对父类的方法重载)

动态绑定——虚方法调用

在调用方法时,程序会正确地调用子类对象的方法

多态的特点大大提高了程序的抽象程度和简洁性

 

上溯造型

把派生类型当做基本类型处理

Person p=new Student();

void fun(Person p){…} fun(new Person());

 

虚方法调用

用虚方法调用,可以实现运行时的多态

子类重载了父类的方法时,运行时

系统根据调用该方法的实例类型来决定使用哪个方法调用

所有的非final方法都会自动地进行动态绑定

示例:TestVirtualInvoke.java

class TestVirtualInvoke

{

       static void doStuff( Shape s ){

              s.draw();

       }



       public static void main( String [] args ){

              Circle c = new Circle();

              Triangle t = new Triangle();

              Line l = new Line();

              doStuff(c);

              doStuff(t);

              doStuff(l);

       }

}

class Shape

{

       void draw(){ System.out.println("Shape Drawing"); }

}

class Circle extends Shape

{

       void draw(){ System.out.println("Draw Circle"); }

}



class Triangle extends Shape

{

       void draw(){ System.out.println("Draw Three Lines"); }

}



class Line extends Shape

{

       void draw(){ System.out.println("Draw Line"); }

}

动态类型确定

变量instanceof类型

结果是boolean值

示例:InstanceOf.java

class InstanceOf

{

       public static void main(String[] args)

       {

              Object [] things = new Object[3];

              things[0] = new Integer(4);

              things[1] = new Double(3.14);

              things[2] = new String("2.09");

              double s = 0;

              for( int i=0; i<things.length; i++ ){

                     if( things[i] instanceof Integer )

                            s += ((Integer)things[i]).intValue();

                     else if( things[i] instanceof Double )

                            s += ((Double)things[i]).doubleValue();

              }

              System.out.println("sum=" + s);

       }

}

三种非虚的方法

Java中,普通的方法是虚方法

static,private与虚方法编译后用的指令是不同的

static的方法,以声明的类型为准,与实例类型无关

private方法子类看不见,也不会被虚化

final方法子类不能覆盖,不存在虚化问题

对比TestStaticInvoke.java

class TestStaticInvoke

{

       static void doStuff( Shape s ){

              s.draw();

       }

       public static void main( String [] args ){

              Circle c = new Circle();

              Triangle t = new Triangle();

              Line l = new Line();

              doStuff(c);

              doStuff(t);

              doStuff(l);

             

              Shape s = new Circle();

              doStuff(s);

              s.draw();

             

              Circle c2 = new Circle();

              c2.draw();

       }

}

class Shape

{

       static void draw(){ System.out.println("Shape Drawing"); }

}

class Circle extends Shape

{

       static void draw(){ System.out.println("Draw Circle"); }

}



class Triangle extends Shape

{

       static void draw(){ System.out.println("Draw Three Lines"); }

}



class Line extends Shape

{

       static void draw(){ System.out.println("Draw Line"); }

}

结果:

Shape Drawing

Shape Drawing

Shape Drawing

Shape Drawing

Shape Drawing

Draw Circle

猜你喜欢

转载自blog.csdn.net/qq_42968048/article/details/84575437
今日推荐