Anonymous class object and a variable number of formal parameters of the method

 

Anonymous class object:

When only need to call the object a class, consider creating a class of objects using anonymous way

Features, objects created by anonymous class can be called only once

new Circle().show();

new Circle.setRatius(2.4);

 

 

A variable number of formal parameters of the method:

/*
 Form a variable number of parameters of the method
 1. Format: the method of parameter: the name of the data type parameter...
 2. The method of parameter variable number between the constituting method of the same name overloading
 3. The variable number of shape parameters when calling number from zero to infinity can be
 4. The method of use of a variable parameter of the plurality of the parameter used is the same as array
 5. If there is a variable number of parameter method, the method must be declared in the last parameter
 6. In one approach, only a maximum of a variable number of parameters.
 */
public class TestArgs {
    public static void main(String[] args) {
        TestArgs t = new TestArgs();
        t.sayHello();
        t.sayHello(new String[] { "hello china", "say anhi" });
        t.sayHello("hello china", "say anhi");
    }

    // the following three methods constitute heavy-duty 
    public  void the sayHello () {
        System.out.println("hello ");
    }

    public void sayHello(String str1) {
        System.out.println("hello" + str1);
    }

    public void sayHello(String... args) {
        for (int i = 0; i < args.length; i++) {
            System.out.println(args[i]);
        }
    }

    /*
     * Public void sayHello1 (String [] args) {// is consistent with the above method for (int i = 0; i <
     * args.length; i++) { System.out.println(args[i]); } }
     */

    public  void the sayHello ( int I, ... String args) { // deformable parameters have to write back 
        System.out.println (I);
         for ( int J = 0; J <args.length; J ++ ) {
            System.out.println(args[j]);
        }
    }

}

 

Guess you like

Origin www.cnblogs.com/afangfang/p/12481859.html