JavaSE notes _day06_ construction method, static, Arrays tool class

JavaSE notes _day06_ construction method, static, Arrays tool class

1. Construction method

1. The concept of construction method (constructor, constructor, constructor)

Person p = new Person();//() Person  

  • Function: Assign values to the member variables of the object. When we create the object through the keyword new, we call the constructor once. The internal logic of the constructor is to override the default value of the member variable. After the object is created, the member variable in the object is Worthwhile. 
  •  Format: Modifier construction method name (parameter) { 
                           //The main logic is to assign values to the member variables of the object

                    } 

  • Precautions: 
         1. Modifier, public is used in most cases. 
         2. The construction method has no return value, even void is not written. 
         3. The name of the constructor must be exactly the same as the name of the class. (File name = class name = construction method name) 
         4. When is the construction method called and executed? 
          (1) The construction method cannot be actively invoked, and the construction method can only be actively invoked and run by the JVM when the object is created.  
          (2) The construction method can only be executed once every time an object is created. 
          (3) When creating a Person object, () means that the parameterless structure of the Person class is called. 
          (4) If you do not write any construction method, the compiler will give you a construction method with null parameters by default. public Student(){} 
          (5) Once at least one constructor with parameters is written, the compiler will no longer give it away.

         5. The construction method can be overloaded, namely no-parameter construction and full-parameter construction

             Method overloading: In the same class, the method name is the same, the parameter list of the method is different (the data type is different, the data type order is different, the number of data types is different), and it has nothing to do with the return value of the method.

public Person(){  		public Person(String name ,int age){
    sout(" ");	    sout(" ");
}				    this.name = name;
				    this.age = age;
				}  

 
public class TestPerson {//Person 
    public static void main(String[] args) {
       /* Person () Person */
        Person p1 = new Person();//
        System.out.println(p1.getName()+"-----"+p1.getAge());//-----20
        Person p2 = new Person(" ",21);//
        System.out.println(p2.getName()+"-----"+p2.getAge());//-----21
    }
}
public class Person {//
    private String name;//
    private int age;//
   /* set get */
    public void setName(String name){
        this.name = name;
    }
    public void setAge(int age){
        this.age = age;
    }
    public String getName(){
        return name;
    }
    public int getAge(){
        return age;
    }
    public Person(){//
        System.out.println(" ");
        name = " ";//  == this.name = " ";
        age = 20;
    }
    public Person(String name,int age){//, 
        System.out.println(" ");
        this.name = name;
        this.age = age;
    }
} 

2. Comparison of construction method and set method assignment

  • 1. Both the construction method and the set method are used to assign values to the member variables of the object 
  • 2. The construction method is relatively simple to write to the member method, but it can only be called once, and can only be assigned once to the object. Later, if you want to pass the second assignment through the construction method, you can t do it, and the flexibility is not enough. 
  • 3. Set method assignment, after the object is created successfully, call the set method for assignment, each time the set method is called, the member can be assigned, which is more flexible 
  •  Summary: When creating an object, by the way, it is concise to assign parameters to member variables, and use the set method to assign values twice

3. The running process of the created object in the memory (assign default value <display assignment <construction method parameter assignment)

public class TestPerson {//Person 
    public static void main(String[] args) {
       /* Person () Person */
        Person p1 = new Person();//
        System.out.println(p1.getName()+"-----"+p1.getAge());//-----0 null----0   >  
        Person p2 = new Person(" ",20);
        System.out.println(p2.getName()+"-----"+p2.getAge());//----20 ----20   >  
    }
}
public class Person {
    private String name = " ";//
    private int age;//new Person() ,JVM 
   /* set get */
    public void setName(String name){
        this.name = name;
    }
    public void setAge(int age){
        this.age = age;
    }
    public String getName(){
        return name;
    }
    public int getAge(){
        return age;
    }
    public Person(){//
        System.out.println(" ");
    }
    public Person(String name,int age){//, 
        System.out.println(" ");
        this.name = name;
        this.age = age;
    }
} 

4. The use of this keyword in the construction method

  • Usage scenarios of this keyword: 
      1. Distinguish between member variables and local variables-this. Member variables, local variables 
      2. Constructor call construction method-this (construction method parameter);
      Description: this (construction method parameter); must be placed in the first line

2. Static

1. Static introduction

  • Example: Define a student object: name, age, student number, dormitory number. For name, age, student number, each object has its own independent data. But for the dormitory number, it should be that multiple objects share a piece of data. (The dormitory number is saved in the class, so all objects of this class share the same one) 
  • If a member variable uses the static keyword, then the variable no longer belongs to the object itself, but belongs to the class it belongs to, and multiple objects share the same data.

2. The static keyword modifies member variables

public class Demo01StaticField {
    public static void main(String[] args) {
        Student one = new Student(" ",19);
        one.room = "3 101";
        System.out.println("   "+ one.getId()+"    "+ one.getName() + "    "+ one.getAge()+"   "+one.room);
        Student two = new Student(" ",18);
        System.out.println("   "+two.getId()+"    "+ two.getName() + "    "+ two.getAge()+"   "+two.room);
        one.room = "3 201";
        System.out.println("   "+two.getId()+"    "+ two.getName() + "    "+ two.getAge()+"   "+two.room);//... "3 201"
    }																													//
}
    public class Student {
        private int id;
        private String name;
	private int age;
	/* static */
	static String room;//room Student 
	private static int idCounter = 0;//new ++
	public Student() {
	    this.id =  ++idCounter;
	}
	public Student( String name, int age) {
	    this.id = ++idCounter;
	    this.name = name;
	    this.age = age;
	}
	public int getId() {
	    return id;
	}
	public void setId(int id) {
	    this.id = id;
	}
	public String getName() {
	    return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
	}
        public static String getRoom() {
	    return room;
        }
        public static void setRoom(String room) {
            Student.room = room;
        }
} 

3. Static characteristics

  • Static is shared. Static belongs to a class and does not belong to any object. 
  • When does the static state enter the memory? 
       When the .class file of a class is loaded in memory, all static modified members (member variables, member methods) in the class are entered into memory along with the loading of the class and stored in the same memory space as the .class file In it, it is called the method area (data sharing). 
  • Static takes precedence over the existence of objects 
  • Statically decorated members can be shared by all objects
  • Static calling method: 

    //.  ( ) 
    System.out.println(Student.room);//3 201
    //( )	
    System.out.println(one.room);//3 201 

4. The static keyword modifies member methods

public class MyClass {
    int num;//
    static int numStatic;//
    public void method(){//
    System.out.println(" ");
    System.out.println(num);//
    System.out.println(numStatic);//
}
public static void methodStatic(){//    //static  
    System.out.println(" ");   // 
    System.out.println(numStatic);//   
   /*System.out.println(num); */
   /*System.out.println(this); */
    }
}
public class Demo02StaticMethod {
    public static void main(String[] args) {
       //
        MyClass obj = new MyClass();
       //static 
        obj.method();//. 
       //
        obj.methodStatic();//  javac . 
        MyClass.methodStatic();//   . 

       //
        Demo02StaticMethod.myMethod();
        myMethod();//
   }
    public static void myMethod(){
        System.out.println(" ");
    }
}			 

5. Static precautions

  • Static cannot directly access non-static 
      Reason: because there are some static content in the memory [first] and some non-static content [after]
  •  The this keyword (reference to this class of object) cannot be used in static methods 
       Reason: this represents the current object, and whoever calls the method is the current object.
  •  If there is no static keyword, then the object must be created first, and then the object can be used. 
       If you have the static keyword, you don't need to create an object, you can use it directly through the class name.
  • summary: 
      Whether it is a member variable or a member method. If there is static, it is recommended to use the class name to call. 
      Static variable: class name. Static variable 
      static method: class name. Static method ()

6. The difference between static variables and non-static variables

1. 
    (1) 
    (2) 
2. 
    (1) .class 
    (2) 
3. 
    (1) ( )
    (2) ( )
4. 
    (1) . ( )  . ( )
    (2) .  

7. A standard class

  • All members must be modified with the private keyword 
  • Write a pair of Getter/Setter for each member variable
  • Write a parameterless construction method 
  • Write a full-parameter construction method 
  • The standard class is also called Java Bean 

3. Help documents and tools

1.Arrays tools

Arrays JDK src.zip 
 java.util.Arrays
Arrays Arrays 
1.sort(int[] arr): 
2.toString(int[] arr): ( [ 1 2 3...])
3.binarySearch(int[] arr,int key): key arr 

import java.util.Arrays;
public class ArraysDemo {//Arrays 
    public static void main(String[] args) {
    int[]  arr ={1,2,56,2,7,6,89,74,3};
    Arrays.sort(arr);
    System.out.println(Arrays.toString(arr));
    System.out.println(Arrays.binarySearch(arr,74) );
    }
} 

2. Generate help documents

/**
    @author  
    @version JDK 
    @param  
    @return  
*/
 
/**
    * @author  
    * @version 1.0
**/
public class DocComment {
    public static void main(String[] args) {
					
    }
/**
*  
* @param x,int 
* @param y,int 
* @return x+y, 
* */
    public static int sum(int x,int y ){
        return x + y;
    }
}