Home  >  Article  >  Java  >  Overloading and rewriting example code of java functions

Overloading and rewriting example code of java functions

高洛峰
高洛峰Original
2017-01-21 16:57:301543browse

In Java, polymorphism is divided into two types: compile-time polymorphism (overloading) and run-time polymorphism (rewriting). Compile-time polymorphism is also called front binding, and run-time polymorphism is also called post-binding.

The following is an example:

public class OverloadAndOverwrite { 
  public static void main(String[] args) { 
    A a1 = new A(); 
    A a2 = new B(); 
    B b = new B(); 
    C c = new C(); 
    D d = new D(); 
    System.out.print("a1.print(a1): "); 
    a1.print(a1);//输出A and A 
    System.out.print("a1.print(b): "); 
    a1.print(b);//输出A and A:原因是因为A中不存在参数为B的方法,因此会调用参数为A的方法,因为B是继承自A的 
    System.out.print("a1.print(c): "); 
    a1.print(c);//输出A and A:原因是因为A中不存在参数为C的方法,因此会调用参数为A的方法,因为C是继承自B的,B是继承自A的 
    System.out.print("a1.print(d): "); 
    a1.print(d);//输出A and D:原因是因为A中存在参数为D的方法,因此会调用参数为D的方法 
    System.out.print("a2.print(b): "); 
    a2.print(b);//输出B and A:原因在于首先入口是A,首先查看A中是否有参数为B的print方法,发现没有那就寻找有没有参数为A的方法,因为B是继承自A的,发现存在这样的方法,那么再次查看B中有没有重写这个方法,发现有重新,直接调用B中这个重写的方法 
    System.out.print("a2.print(c): "); 
    a2.print(c);//输出B and A:原因在于首先入口是A,首先查看A中是否有参数为C的print方法,发现没有那就寻找有没有参数为B的方法,因为C是继承自B的,发现也不存在这样的方法,那就寻找存在参数为A的print方法,因为B继承自A,发现存在这样的方法,那么再次查看B中有没有重写这个方法,发现有重新,直接调用B中这个重写的方法 
    System.out.print("a2.print(d): "); 
    a2.print(d);//输出 A and D:原因在于入口是A,查看A中存在参数为D的方法,再次查看B中没有重写这个方法,因此输出A中这个方法的结果即可; 
    System.out.print("a2.print(a2): "); 
    a2.print(a2);//输出B and A;原因在于a2的类型是A,因此会调用A里面参数为A的print方法,但是a2右边new的是B,所以因为B中有参数为A的方法,因此采用的是B里面的这个方法 
    System.out.print("b.print(b): "); 
    b.print(b);//输出B and B;原因:入口是B,因此查看B中存不存在参数为B的print函数,存在则直接输出; 
    System.out.print("b.print(c): "); 
    b.print(c);//输出B and B;原因:入口是B,因此查看B中存不存在参数为C的print函数,发现不存在,则查看存不存在参数为B的print函数,发现存在,并且C中并没有重写该方法,则直接输出;有一点需要注意的是还需要查看一下A中是否存在参数为C的print方法,因为B继承自A,有的话会及成果来这个方法,这样的话输出的结果将变为A and C 
    System.out.print("b.print(d): "); 
    b.print(d);//输出A and D;原因:入口是B,虽然B中不存在参数为D的print函数,但是B继承自A,A中是存在参数为D的print函数的,因此输出的是A中参数为D的结果; 
  } 
} 
class A 
{ 
  public void print(A a) 
  { 
    System.out.println("A and A"); 
  } 
  public void print(D d) 
  { 
    System.out.println("A and D"); 
  } 
// public void print(C c) 
// { 
//   System.out.println("A and C"); 
// } 
} 
class B extends A 
{ 
  public void print(B b) 
  { 
    System.out.println("B and B"); 
  } 
  public void print(A a) 
  { 
    System.out.println("B and A"); 
  } 
} 
class C extends B{} 
class D extends C{}

What needs to be explained here is:

For A a2 = new B();

If printed separately If a2 is output, the printed result is B@ (hash code), not A@ (hash code), but this does not mean that the type of a2 is B type, because when we call a2.print(a2) in the above program ; when the output result is B and A instead of A and A (if you assume that a2 is of type B, you should call the print method with parameter B in class A, because there is no such method, then the second best option is to call The method with parameter A should output A and A, because B is a subclass of A).

The above is an example code analysis of rewriting and overloading. I hope it will be helpful to students learning Java.

For more articles related to Java function overloading and rewriting example codes, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn