three ways to summarize Java reuse and parameter transfer:
(1) calling a function directly in a class:
1PackageTest1;23PublicClassA4{ 5 public static void GetDate () 6 { 7 System.out.println (" Morning Dream Childe "); } 9 public static void10 11 getDate (); 12 }13}
This method should note that the GETDATE () of the call in the main function must be declared as static (that is, static).
(2) A call in a class in the same package (no parameter):
1package Test1; 2 3 public class b 4 { 5 public int GetDate () 6 { 7 int b=1; 8 return B; 9 }10}
1PackageTest1;23PublicClass C 4 { 5 public static void< Span style= "color: #000000;" > main (string[] args) 6 7 b b1 = new B (); // makes B1 work like Class B, which is the reference to the object, called the object. 8 int B2 = B1.getdate (); System.out.println (B2); }11}
It is worth mentioning that, with b B1 = new B (), no Static in Class B (spit groove: How cool, but also save the memory of O (* ̄▽ ̄*) O. What?! You don't know how to save memory? , explained below):
Static means that the memory is not freed until the program has finished executing. If you use new to allocate space, as in the example above. Benefit: The Java compiler will have a garbage collector to automatically free up memory when B1 is not available.
(2) calls (with parameters) in the same class as the package:
1Package test1; 3 public class D 4 { 5 public int getDate ( Int D1) { 7 int d = D1; 8 return D; 9 }10}
1PackageTest1;23PublicClass e 4 { 5 6 public static void Main (string[] args) 7 { 8 D da = new D (); 9 int D1 = da.getdate (1) ; 10 System.out.println (D1); }12 13}
The function of the parameters: the same type of data of the main function can be taken to the function of the call to deal with, the result is obtained.
Error-prone point: 1) as above example int d = d1; The statement is easy to ignore int;
2) The above example System.out.println (D1), may be wrong to write System.out.println (DA), this time the output is [email protected], that is, the address;
PS: I small white one, the first knowledge of Java, the above is just a few of their own groping out of the road, estimated that there is inappropriate, for reference only. No, exactly, for beginners ' reference only.
Java multiplexing and parameter transfer