Public classtest{Private intAge//This is a member variable PublicTest (intAge) {//This is the construction method This. Age =Age ;} Public voidSetage (intAge) {//This is the member method This. Age =Age ;} Public Static intGetage () {//This is the global method, with static, the member method becomes the global methodreturn This. Age;}}
Member methods have nothing to do with constructing methods, except that the member method must be accessed with the instantiation object of the class, while the global method is accessed directly by the class, and the constructor is initialized when the object is instantiated.
Explain the construction method first:
Test T = new Test (34);
Here the new Test (34) parentheses represent the construction method, the general construction method is the default, you can not write, if the above construction method does not write, here should instantiate the object:
Test T = new Test ();
Member Methods:
Test T = new Test (34);
T.setage (35);
Invoking the normal method with an instantiated object
Normal member variables:
Test T = new Test (34);
Because the variable age adds private, it cannot be called: T.age = 35; This is wrong and can only be modified by means of the method.
Global method:
Test.getage ();
Call it directly, of course, if not instantiated, Test.getage () here is 0
Three methods in a Class 1 global Method 2 member Method 3 construction Method (GO)