Cock wire from flying to architect (object-oriented) - A lump Code

I. Introduction

In Java, we want to solve some problem that requires a lot of code to solve, this time, we need to use "{}" will organize our code into the code together, the "{}" enclose the code is called code block.

17390640-a08f562162f7d694.png

II. Knowledge Point presentation

1, a partial code block

2, the configuration of the code block

3, static code block

III. Class corresponding to the video documentation

1, a partial code block

Common code block is defined directly in the process or statement to "{}" designated area code, then only need to focus on different methods and classes are scope manner delimitation code blocks, Such as:

class Demo{

public static void main(String[] args) {

{

int x = 1;

System.out.println ( "normal block" + x);

}

int x = 99;

System.out.println ( "addition block" + x);

}

}

result:

Common code block 1

Addition block 99

17390640-df06aad27704f8ae.png

2, the configuration of the code block

Block of code is defined directly in the position of members of the class, they have priority in the constructor, the code block is configured for initializing operation performing all objects are required to create each object will execute a code block configuration.

public class Person {

private String name;

private int age;

static{

System.out.println ( "static block of code is executed");

}

{

System.out.println ( "configured to execute the code block");

}

Person(){

System.out.println ( "Person constructor parameter without performing");

}

Person(int age){

this.age = age;

System.out.println ( "constructor Person (age) performing parameter");

}

}

class PersonDemo{

public static void main(String[] args) {

Person p = new Person();

Person p1 = new Person(23);

}

}

3, static code block

Static block of code is defined in the position of a member, using a modified static block.

Features: it takes precedence over the main method of performing priority code block configured to perform, when executed in any form, for the first time to the class.

No matter how many objects the class is created, static code block is executed only once.

It can be used for static variables, which is used to initialize the class.

public class Person {

private String name;

private int age;

// static code block

static{

System.out.println ( "static block of code is executed");

}

}

public class Person {

private String name;

// block of code known as a member of the object code block structure level code block will be executed once each time you create an object

{

System.out.println ( "I is a structural block");

}

// class level static code block of code blocks, create an object only if the first run, after you create the object is not implemented

static{

System.out.println ( "static block");

}

public Person(String name) {

super();

this.name = name;

}

public Person() {

super();

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

public class Test {

public static void main(String[] args) {

int a = 10;

// local variable scope defined range block

{

System.out.println(a);

int b = 20;

System.out.println(b);

}

//  System.out.println(b);

Person p = new Person();

Person p2 = new Person("刘备");

Person p3 = new Person("刘备");

Person p4 = new Person("刘备");

Person p5 = new Person ( "Bei");

}

}

Reproduced in: https: //www.jianshu.com/p/5df5e0856816

Guess you like

Origin blog.csdn.net/weixin_33916256/article/details/91063259