1.foreach Cycle
For (type variable name: Array or collection) {
Output operation
}
2. Variable parameters: Automatically pass in any number of parameters as required, that is, variable parameters.
Syntax: return value type method name (data type ... Parameter name) {
}
Cases:
1UblicclassPractice14 {2 3 /**4 * @paramargs5 */6 Public Static voidMain (string[] args) {7 //TODO auto-generated Method Stub8 /*string[] name={"Xiao Ming", "Wax Gourd", "Xiao Gang", "Shian"};9 For (String n:name) {Ten System.out.println (n); One }*/ APrint ("Xiao Ming", "Wax Gourd", "Xiao Gang", "Shian"); - - } the //variable parameters (as arrays when used), variable parameters can only have one - Public Static voidPrint (string...params) { - //foreach Loop - for(String s:params) { + System.out.println (s); - } + } A}
3. Code block
Common code blocks: code blocks that are written directly in the method
Construct a code block: a block of code defined in a class. Constructs a block of code that executes first, in other words, when a new object is
Static code block: a block of code that uses static declarations in a class
Cases:
1 Public classPractice14 {2 Static{3SYSTEM.OUT.PRINTLN ("Static Fast");4 }5 {6SYSTEM.OUT.PRINTLN ("Fast construction"); 7 }8 9 Public Static voidMain (string[] args) {Ten //TODO auto-generated Method Stub OnePractice14 test1=NewPractice14 (); APractice14 test2=NewPractice14 (); - - } the -}
This example outputs a static fast, two-time construction block
Java Object-oriented _ enhanced for variable parameters and code blocks