Application of Java methods

Source: Internet
Author: User
Tags modifier

I. Definition of a method

methods are part of a class, is an ordered combination of steps to solve a class of problems, contained in classes or objects, created in a program, referenced elsewhere

Syntax of the method:

Modifier returns a value type method name ( parameter type argument name ...). ){ ... Method body ...  return value; }

The method can be divided into four classes depending on whether the method has parameters or not, and whether it has a return value:

    • No parameter no return value method
    • Non-parametric return value method
    • No return value method with parameter
    • Return value method with a parameter
 PackageCom.yyx.pratice; Public classJavapratice { Public Static voidMain (string[] args) {noparamvalue (); String aStr= "parameter", BSTR = "no return value";        Novalue (ASTR, BSTR);        System.out.println (Noparam ()); System.out.println (Paramvalue ("Parameter has a return value")); }     Public Static voidNoparamvalue () {System.out.println ("No parameter no return value"); }    /*** Parameter no return value * *@paramASTR *@parambStr*/     Public Static voidNovalue (String aStr, String bStr) {System.out.println (aStr+bStr); }    /*** No parameter has return value * *@return     */     Public Staticstring Noparam () {string str= "No parameter has return value"; returnstr; }    /*** with parameter, return value * *@paramSTR *@return     */     Public Staticstring Paramvalue (String str) {returnstr; }}

Second, the method of overloading

Features of method overloading:

    • In the same class
    • Method names must be the same
    • The parameter list of the method is different (the number of arguments is different or the parameter type is different)
    • the overload of a method has no relation to the return value type of the method   
 PackageCom.yyx.pratice; Public classJavapratice { Public Static voidMain (string[] args) {intA = 1, b = 2, c = 3; Doublem = 2.1, n = 3.2;        Addnumber (A, b);        Addnumber (M, n);    System.out.println (Addnumber (A, B, c)); }     Public Static voidAddnumber (intAintb) {System.out.println (a+b); }     Public Static voidAddnumber (DoubleADoubleb) {System.out.println (a+b); }     Public Static intAddnumber (intAintBintc) {returnA + B +C; }}

third, the method of rewriting    

methods that inherit from a base class can be overridden in a subclass as needed

Rules for overriding:

    • The return value type method name (parameter list) that requires a subclass method is the same as the method of the parent class
    • The modifier of a subclass method cannot be less than the modifier of the parent class method
    • If the parent method throws an exception, the subclass method throws an exception type that cannot be greater than the parent class.
    • The method of the child parent class must be either static or non-static
 PackageCom.yyx.pratice; Public classJavapratice { Public Static voidMain (string[] args) {testextend testextend=NewTestextend (); Testextend.addnumber (5, 6); Testextend.printword ("Test override Method"); }}classTestinheritance { Public voidPrintword (String str) {System.out.println (str); }}classTestextendextendsTestinheritanceImplementstestimplements {@Override Public voidPrintword (String str) {System.out.println ("Sub-class to add a sentence"); } @Override Public voidAddnumber (intMintN) {System.out.println (M+N); }}Interfacetestimplements {voidAddnumber (intMintn);}

Iv. parameters of the method

1. Formal parameters and arguments

parameter: The argument in the method's parentheses when the method is declared; argument: The value of the actual passed-in parameter when the method is called

Parameter passing mechanism:

    • Parameters are basic data types: Pass values of arguments to variables of the base data type of the formal parameter, that is, pass the value
    • Formal parameters are reference data types: The value of the argument's reference type variable (the first address value of the object entity corresponding to the heap space) is passed to the reference type variable of the formal parameter, that is, the delivery address
 PackageCom.yyx.pratice; Public classTestValue { Public Static voidMain (String argv[]) {TestValue T=NewTestValue ();    T.first (); }     Public voidFirst () {inti = 5; Value v=NewValue (); V.I= 25;        Second (v, i);    System.out.println (V.I); }     Public voidSecond (Value V,inti) {i= 0; V.I= 20; Value Val=NewValue (); V=Val; System.out.println (V.I+ " " +i); }}classValue {inti = 15;}

2. Variable parameters

Methods for changing the number of formal parameters:

    • 1. Format: For the parameter of the method: Data Type ... Formal parameter name
    • 2. The method of a variable number of parameters forms an overload between the method with the same name
    • 3. Variable number of formal parameters at the time of invocation, the number starts from 0, to infinity can be
    • 4. Using a method with variable multiple parameters is consistent with the method's parameters using arrays
    • 5. If there is a variable number of parameters in the method, then be sure to declare at the end of the method parameter
    • 6. In one method, declare up to a variable number of parameters
 Public classTestargs { Public Static voidMain (string[] args) {Testargs T=NewTestargs (); //T.sayhello (New string[]{"Hello China", "Hello Beijing"});T.sayhello ("Hello China", "Hello Beijing"); T.sayhello (5, "Hello China", "Hello Beijing"); }    //method of parameter with variable number     Public voidSayHello (String ... args) { for(inti = 0; i < args.length; i++) {System.out.println (Args[i]+ "$"); }    }     Public voidSayHello (intI, String ... args)        {System.out.println (i);  for(intj = 0; J < Args.length; J + +) {System.out.println (Args[j]+ "$"); }    }    /*** methods using variable multiple parameters are consistent with the method's parameters using arrays*/    //Public void SayHello (string[] args) {//for (int i = 0; i < args.length; i++) {//System.out.println (Args[i]); // }    // }}

Application of Java 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.