Today, I have read about the transformation of objects and learned a little bit more clearly:
First, create a person class. The Code is as follows:
Package COM. test; public class person {string name; string age; void introduce () {system. out. println ("My name is" + name + "My age is" + age );}}
The student code is as follows:
Package COM. test; public class student extends person {string address; void introduce () {super. introduce (); system. out. println ("My home lives" + address);} void Study () {system. out. println ("I'm learning ");}}
The code for the test class is as follows:
Package COM. test; public class test {/*** @ Param ARGs */public static void main (string [] ARGs) {student = new student (); student. name = "James"; student. age = "20"; student. address = "Guangzhou"; person = student; person. introduce ();}}
The running result is as follows:
My name is Michael. My age is 20.
My family lives in Guangzhou
//////////////////////////////////////// //////////////////////////////////////// //////////////////////////
The above results are summarized as follows:
1. If we call person. Address, an error will be reported, because: the members (variables, and functions) that a reference can call depend on the type of the reference!
2. In the test above, we call person. Introduce (); the result of this method is: My family lives in Guangzhou.
This indicates that the introduce () method called by person is in students!
So we can understand which methods can be called by a reference depends on the object pointed to by this reference!