Detailed explanation of Java code block

1, construction method

1.1 concept

  • Constructor is a special method, which is a class with the same name and the same name as the class type of the return value types.
  • Creating an object is accomplished by the constructor.
  • Its main function is to complete the creation of objects or object initialization.
  • When a class instance of a new object will automatically call the constructor.
  • Constructor and other methods as may be overloaded (the same method name different parameter list +).

1.2 form

  • There can be no arguments can also participate

Modifier class name (parameter list) {
Code} ......


Constructor to create an object:

public class Test1_Constructor {
    public static void main(String[] args) {
       //创建对象时  会  自动  调用构造方法
       //3、new Person();  会自动调用无参构造
        Person p = new Person();
       //4、new Person("rose");  会自动调用含参构造 
       Person p2 = new Person("rose");
    }
}

//创建Person类            
class Person{
    //修饰符 类名( 参数列表 ){ 代码 }
    //1、显示的提供无参构造方法    --  无参的构造方法默认就会存在 !!
    //5、当程序中,只提供含参构造时,无参构造会被覆盖就没有了!!
    public Person() {
       System.out.println("无参构造...");
    }
    //构造方法是一个特殊的方法:特殊在1、没有返回值  2、方法名是类名
    //2、构造方法也是可以进行重载的:方法名相同  参数列表不同
    public Person(String name) {
       System.out.println("含参构造..."+name);
    }
}

Constructors assignment:

public class Test2_Constructor2 {
    public static void main(String[] args) {
       //创建对象 测试
       //1、会自动调用 无参构造
       Person2 p = new Person2();
       //2、会自动调用 含参构造
       Person2 p2 = new Person2("jack");
   }
}
//创建Person2类
class Person2{
    //4、给私有属性赋值的两个方式:通过对象调用对应的setXxx()   或者  创建对象时利用构造方法赋值
    private String name;    
    public Person2() {
       System.out.println("无参构造");
    }
    public Person2(String n) {
       //3、拿着创建对象时传递过来的参数,给成员变量name赋值
       name = n ; 
       System.out.println(name);
    }
}

2, the configuration of the local code blocks and code block

2.1 configured block {}

  • 1, inside the class the method outside the block.
  • 2, the code typically used for extracting the common construction method.
  • 3, the configuration of the former method will be called a code block configuration of each call new new new
  • 4, in the loaded first constructor

public class Test3_Block {
    public static void main(String[] args) {      
       //创建Person3对象测试
       //2、在执行构造方法前  ,如果 有构造代码块,,就会先执行构造代码块再执行构造方法
       Person3 p = new Person3();   //先执行了构造代码块  再执行了 无参构造方法
       Person3 p2 = new Person3("jack");   //先执行了构造代码块   再执行了  含参构造方法
    }
}
//创建Person3类
class Person3{
    //1、构造代码块:位置是在类里方法外
    //3、 构造代码块  的功能 :是提取构造方法中的 共性!!!
    String country ;
    {
           country = "中国人";//构造代码块
    }
    //无参构造  
    public Person3() {
       System.out.println("无参构造"+country);
    }
    //含参构造
    public Person3(String n) {
       System.out.println("含参构造"+n+country);
    }    
}

Partial block 2.2

  • 1, in which the code blocks of
  • 2, typically control variables for the scope, a failure on the brackets
  • 3, the smaller the better range of variables, member variables have thread-safety issues

 public class Test4_Block2 {
    public static void main(String[] args) {
       //创建对象测试
       Person4 p = new Person4();//自动调用无参构造
       p.eat();
    }
}
//创建Person4类
class Person4{
    //默认就会存在无参构造   
    //方法
    public void eat() {
       //1、局部代码块:位置是在方法里  +  触发时间是当方法被调用时  + 意义是用来控制变量的作用范围
       {
           int i = 10;
           System.out.println(i);
       }
       System.out.println("正在吃饭");
//     System.out.println(i);   //报错,原因是已经超出了变量i的作用范围
    }
}
Published 36 original articles · won praise 13 · views 1077

Guess you like

Origin blog.csdn.net/weixin_44598691/article/details/104705134