This article describes the Java dynamic method scheduling. Share to everyone for your reference, specific as follows:
Dynamic method Scheduling:
1. A non-static method that accesses a reference variable, and the runtime binds to the method of the object that is actually referenced.
2. A static method that accesses a reference variable, which is bound to the method of the declared class.
3. Access the member variables of a reference variable, including static and instance variables, and the runtime is bound to the member variables of the declared class.
3rd, pay special attention, I never noticed before.
1. Non-static methods:
public class Person {public
String name;
public void GetInfo () {
System.out.println ("parent class");
}
The public class Student extends the person {public
void GetInfo () {///method overrides
Super.getinfo ();//Invoke Method System.out of the parent class
. println ("Subclass");
}
public static void Main (string[] args) {person
s = new Student ();
Person T = new person ();
s = t; The object type of S is the parent class, that is, the person class
s.getinfo ();
}
The result of the run is: parent class
2. Static method:
public class Person {public
String name;
public static void GetInfo () {
System.out.println ("parent class");
}
public class Student extends person {
publics static void GetInfo () {//Method overrides
System.out.println ("subclass");
} Public
static void Main (string[] args) {person
s = new Student ();
S.getinfo (); Equivalent to Person.getinfo ();
}
The result of the run is: parent class
3. Member variables
public class Erson {public
String name = ' father ';
public void GetInfo () {
System.out.println ("parent class");
}
public class Student extends person {public
String name = "Son";
public void GetInfo () {//Method overrides
Super.getinfo ();//Calling Method System.out.println of the parent class
("subclass");
}
public static void Main (string[] args) {person
s = new Student ();
Person T = new person ();
s = t;
System.out.println (s.name);
}
Run Result: Fanther
The same is true of changing member variables to static types.
In addition, for the following two variables
Students = new Student ();
Person T = new Student ();
However, there is a difference between the two, when the subclass student has its own personality method (which is not in the parent class), such as having a method
Then only S can call this Goschool method
and T cannot call
I hope this article will help you with Java programming.