Java Functions (methods)

Source: Internet
Author: User
Tags define function

function: in order to improve the reusability of code, it can be defined as a separate function, the function of which is the function in Java. function is one of the embodiment.

The Java function is written as:

Modifier returns the type function name (parameter list ... {     method, function body     return value;}
When the function does not have a specific return value, the return value type returned is used with the voidKeyword representation. If a function's return value type is void, the return statement can be omitted, and the system will automatically add it to you. the function of return:The End Function. End Function. Attention: function can only be called in functions, cannot define function

overloading is defined as: In a class, if there are two or more than two functions with the same name, as long as they have the number of arguments, or the type of the parameter is different, it can be called the function overload.

How to differentiate overloads: when the function has the same name, only the argument list is viewed. And the return value type is okay.

The main function in Java:

in Java, the main () method is the entry method for a Java application. The Java Virtual machine finds the running program that needs to be started by using the main method, and checks whether the class that contains the main function is loaded by the Java virtual machine. If there is no load, the class is loaded and all other related classes are loaded. Therefore, when the program is running, the first method to execute is the main () method. Typically, if you want to run a method of a class, you must first instantiate an object of the class, and then run the method by using the object name. Method Name (), but because main is the entry for the program, this time the class has not instantiated the object, so the main method is declared as static, This method can be called directly from the class name. Method Name ().

Java allows classes to be non-public keyword constraints, although the definition of a class can only be restricted to public or unrestricted keywords (default).

Since the JVM is running this Java application, the main method is called first, and the object of the class is not instantiated, but is called directly by the class name and therefore needs to be restricted to public static.

For the main method in Java, the JVM has a limit and cannot have a return value, so the return value type is void. The main method also has an input parameter, type string[], this is the Java specification, the main () method must have a parameter, the class must be string[], as for the name of the string array, this can be set by itself, according to the habit,    The name of this string array is generally consistent with the Mian parameter name in the Sun Java specification paradigm, named args. The function of the string parameter array in the main () method is to receive command line input parameters, separated by a space between the parameters of the command line. The compilation call procedure for the Java class:
d:\study\basetest\src>javac Testmain.java D:\Study\basetest\src123   123

value passing and reference passing for function parameters:Value passing: Changing the value of a variable in memory refers to passing: Assigning a variable address to another value function parameter passing is actually an assignment process, the base type passes the numeric value, and the reference type passes the memory address of the reference object. Another point to pay special attention to is that the parameters of the function are actually local variables inside the function. Do not confuse with external variables constructors (class-instantiated initialization functions):1. The constructor must have the same name as the class (if there are multiple classes in a source file, the constructor must have the same name as the public Class) 2. Each class can have more than one constructor 3. The constructor can have 0, one, or 1 arguments 4. The constructor does not return a value of 5. The constructor is always called with the new operation Inheritance and constructors: the statement using Super to call the parent class constructor must be the first statement of the subclass constructor if the subclass constructor does not explicitly call the constructor of the parent class, the default (no parameter) constructor for the parent class is automatically called. If the parent class does not have a constructor with no arguments, and the constructor of the parent class is not explicitly called in the constructor of the subclass, the Java compiler will report an example of the error: A.java
 Public class a{   public  A () {      System. out. println (" a parameterless constructor called ");   }     Public A (String mess) {      System. out. println (" the argument constructor for a was called \ n"+ "the contents of the          parameter are: "+Mess);}   }

B.java

 Public class b extends a{   public  B () {         System. out. println (" a parameterless constructor called B ");   }     Public B (String mess) {      super (mess);      System. out. println (" the argument constructor for b is called \ n"+         " the contents of the parameter are: "+Mess);}   }

Test.java

 Public class test{   publicstaticvoid  main (String [] args) {       B b_01=New  B ();       b b_02=new B (" hello ");}   }

Output Result:

Java callback function:

The callback function in the C language explains: What is a function of the callback function (Callback functions)? function is used to be called, we call the method of the function has two kinds of:l         direct call: In function A's function body by writing function B's function name to call it, The code that corresponds to function b in memory is executed. Here, A is called the "calling function" (Caller), and b is called the called function (Callee). l         Indirect invocation: The function name of function B does not appear in function A's function body, but instead uses the function pointer p to function b to execute the code fragment that is part of function B in memory- -Sounds cool, doesn't it? Indirect calls are really troublesome compared to direct calls, so why use indirect calls? The reason is very simple-directly call the function name into the function body, after the compiler so a compilation, the certainty, a is destined to call the B, such a program can only follow the programmer in advance design process execution, too stiff. At this point, the great flexibility of the indirect invocation is revealed. Think of it, if P is a parameter of function A (the argument is a variable, it can be changed!) ), then the end user of the program can completely change the point of p by manipulating it-thus, a has a chance to invoke a different function when the function is called by P, which makes the program more practical and extensible.

In Windows, the programmer wants the system DLL to call a method written by itself, and then uses the interface of the callback function (CALLBACK) in the DLL to write the program, which is called the callback. When you invoke an interface, you need to strictly follow the defined parameters and method calls, and you need to handle the async of the function, which will cause the program to crash. This explanation seems to be more difficult to understand, here is a simple example, programmer a wrote a program (program a), which reserved a callback function interface, and encapsulated the program. Programmer B wants A to invoke a method in its own program B, so he callbacks his method in B through the interface in a. Objective to achieve. In C + +, to use a callback function, the dropped function needs to tell the caller their own pointer address, but there is no pointer in Java, what to do? We can use interfaces (interface) to implement the definition of callback functions normally the developer uses the already defined API, which is called call. But sometimes this does not meet the requirements, it is necessary for the programmer to register their own programs, and then let the prior definition of a lot of APIs at the appropriate time to invoke the method of registration, this is called callback.

usage Tip: Set an interface that declares the method we want to invoke in the interface.

Registering a newly defined callback interface in another method

Cases:

Programmer a wrote a program (program a), which reserved a callback function interface, and encapsulated the program. Programmer B wants A to invoke a method in its own program B, so he callbacks his method in B through the interface in a. Objective to achieve. In C + +, to use a callback function, the dropped function needs to tell the caller their own pointer address, but there is no pointer in Java, what to do? We can implement the definition callback function through the interface (interface).
Let's say I'm programmer a, and here's my program A:

public class Caller  {public      mycallinterface MC;        public void Setcallfuc (Mycallinterface mc)      {         this.mc= mc;      }        public void Call () {         this.mc.method ();      }  }  

I also need to define an interface so that programmer B implements the interface based on my definition of programming.

Public interface Mycallinterface  {public      void method ();      

So, programmer B only needs to implement this interface to achieve the purpose of the callback:

public class B implements Mycallinterface  {public      void method ()      {         System.out.println ("callback");      } Public        static void Main (String args[])      {         Caller call = new Caller ();         CALL.SETCALLFUC (New B ());         Call.call ();      }  

Summary: B called a call to B method to implement the Mycallinterface interface

Java Functions (methods)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.