Usage
1. When the program is compiled, it actually calls the eat method of the parent class, but at runtime, it runs the eat method of the subclass. Binding occurred during runtime.
2. Use the preamble, first transform upward, and call the overridden method of the parent class and subclass with the same name through the parent class reference
Instance
package chapeter04; class Test { public Test() { } public void setName(String n) { this.name=n; System.out.println("在父类中"); } public String getName() { return this.name; } private String name; } public class Sample4_12 extends Test { public void setArea(String a) { this.area=a; } public String getArea() { return this.area; } public static void main(String[] args) { // TODO Auto-generated method stub Sample4_12 child = new Sample4_12(); Test test []=new Test[2]; test[0]=child; test[0].setName("silence"); test[1]=new Test(); } private String area; }
The above is the detailed content of How to use java dynamic binding method. For more information, please follow other related articles on the PHP Chinese website!