Getting started with object orientation (classes and objects)

day1 - Introduction to object orientation (classes and objects)

Knowledge points 1 - classes and objects

  • java program: requirement: print all elements in the array in the format: [element 1, element 2, element 3, element,..., element n]

    public class Test {
        public static void main(String[] args) {
            /*
                Process oriented programming idea
                    - The emphasis is on the process, which must be clear about each step, and then realize it step by step
    
                Object oriented programming idea
                    - The emphasis is on the object, which realizes the function by calling the behavior of the object, rather than operating and realizing it step by step.
             */
            // Requirement: print all elements in the array in the format: [element 1, element 2, element 3, element,..., element n]
            // 1 define an array and initialize the elements in the array
            int[] arr = {10, 20, 30, 40, 50};
    
            // Process oriented:
            // 2. Loop through the array
            for (int i = 0; i < arr.length; i++) {
                // 3. In the loop, get the traversed elements
                int e = arr[i];
                // 4. Judge the element:
                if (i == 0) {
                    // 4.1 if the element is the first element, the print format: [+ element + comma space, no line break
                    System.out.print("[" + e + ", ");
                } else if (i == arr.length - 1) {
                    // 4.2 if this element is the last element, print format: element +]
                    System.out.println(e + "]");
                } else {
                    // 4.3 if the element is an intermediate element, the printing format is: element + comma space, no line break
                    System.out.print(e + ", ");
                }
            }
    
            System.out.println("==================================");
    
            // object-oriented:
            // jdk api has an Arrays class toString() method, which can help us print all the elements in the array in this format
            System.out.println(Arrays.toString(arr));
        }
    }
    

Summary

  • Process oriented: a programming idea
  • Object oriented: a programming idea
  • difference:
    • Process oriented: focus on the process. You must be clear about each step and realize it step by step
    • Object oriented: focus on the object, do not need to know every step, just use the object call behavior to complete the requirements

Knowledge points – 5 Class definition [application]

Composition of review class

Class consists of two parts: attribute and behavior

  • Attribute: the state information of this kind of things, which is reflected in the class through member variables (variables outside the methods in the class)
  • Behavior: the functions of this kind of things are reflected in the class through member methods (compared with the previous methods, just remove the static keyword)
Class definition steps

① Define the class ② write the member variable of the class ③ write the member method of the class

Class definition format
public class Class name {// Define a class
	// Class: attribute (member variable), behavior (member method)
    // Define member variables
    Data type variable name 1;
    Data type variable name 2;
    ...
        
    // Define member methods
    method;  Remove static
}

Knowledge points – 6 Object creation and use

Summary

	Format for creating objects:
        Class name object name = new Class name();
    Use object:
        Accessing member variables of a class:
                Gets the value of the member variable: Object name.Member variable name
                Assign values to member variables:   Object name.Member variable name = value;

        Accessing member methods of a class:
            Member method has no return value:  Object name.Member method(Argument);
            Member method has return value:
                             Object name.Member method(Argument);  Direct call
                             Data type variable name = Object name.Member method(Argument); Assignment call
                             System.out.println(Object name.Member method(Argument));Output call

Knowledge points – 8 Single object memory map

public class Student {
    // Member variables: properties
    /**
     * full name
     */
    String name;
    /**
     * Age
     */
    int age;

    // Member method: behavior
    /**
     * Function of learning
     */
    public void study(){
        System.out.println("The students are studying Java...");
    }

    /**
     * Function of doing homework
     */
    public void doHomeWork(){
        System.out.println("The students are doing their homework and typing the code...");
    }
}

public class Test {
    public static void main(String[] args) {
        // Create Student object
        Student stu = new Student();
        System.out.println(stu);// Hexadecimal address value

        // Accessing member variables
        stu.name = "Bingbing";
        stu.age = 18;
        System.out.println(stu.name+","+stu.age);

        // Access member method
        stu.study();
        stu.doHomeWork();
    }
}

[the external chain picture transfer fails, and the source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-cp4j9k0i-1643877681370) (IMG \ image-2020090512118328. PNG)]

Summary

  • As long as you create an object, you will open up a space in the heap (for new, you will open up a new space in the heap)
  • As long as the method is called, a space will be opened up in the stack area to execute the method

Knowledge points – 9 Multiple object memory diagram [understanding]

explain

View program cases
public class Student {
    // Member variables: properties
    /**
     * full name
     */
    String name;
    /**
     * Age
     */
    int age;

    // Member method: behavior
    /**
     * Function of learning
     */
    public void study(){
        System.out.println("The students are studying Java...");
    }

    /**
     * Function of doing homework
     */
    public void doHomeWork(){
        System.out.println("The students are doing their homework and typing the code...");
    }
}

public class Test {
    public static void main(String[] args) {
        // Create Student object shift+f6+fn batch modify name
        Student stu1 = new Student();
        System.out.println(stu1);// Hexadecimal address value

        // Accessing member variables
        stu1.name = "Bingbing";
        stu1.age = 18;
        System.out.println(stu1.name+","+stu1.age);// Bingbing, 18

        // Access member method
        stu1.study();
        stu1.doHomeWork();

        System.out.println("==========================");

        Student stu2 = new Student();

        System.out.println(stu2.name+","+stu2.age);// null,0
        stu2.study();

    }
}

Draw memory map

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-zmzcao4l-164387768137) (IMG \ image-20200905123805520. PNG)]

Summary

  • Multiple objects have different memory partitions in heap memory. Member variables are stored in the memory area of their respective objects, and member methods are shared by multiple objects
  • Any new will open up a new space in the heap area
  • Objects and relationships between objects are independent of each other

Knowledge points – 10 Multiple variables point to the same object memory graph [understand]

explain

View program cases
public class Student {
    // Member variables: properties
    /**
     * full name
     */
    String name;
    /**
     * Age
     */
    int age;

    // Member method: behavior
    /**
     * Function of learning
     */
    public void study(){
        System.out.println("The students are studying Java...");
    }

    /**
     * Function of doing homework
     */
    public void doHomeWork(){
        System.out.println("The students are doing their homework and typing the code...");
    }
}
public class Test {
    public static void main(String[] args) {
        // Create Student object
        Student stu1 = new Student();

        // Accessing member variables of student objects
        stu1.name = "Bingbing";
        stu1.age = 18;
        System.out.println(stu1.name + "," + stu1.age);// Bingbing, 18

        // Member methods for accessing student objects
        stu1.study();

        System.out.println("============================");
        // Define a variable of student type and assign the previously created Student object to the variable
        Student stu2 = stu1;

        // Then use the new variable to access the member variable
        System.out.println(stu2.name + "," + stu2.age);// Bingbing, 18
        // Then use the new variable to access the member method
        stu2.study();
    }
}
Draw memory map

[the external link image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-jbClykuq-1643877681374)(img\image-20200905144926634.png)]

Summary

  • When the references of multiple objects point to the same memory space (the address value recorded by the variable is the same)
  • As long as any object modifies the data in memory, then, no matter which object is used for data acquisition, it is the modified data.
  • Reference types pass address values

Knowledge points – 11 Differences between member variables and local variables [understanding]

explain

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-TRQUa1zF-1643877681374)(img82716017361.png)]

  • Different positions in the class: member variable (outside the method in the class) local variable (inside the method or on the method declaration)
  • Different locations in memory: member variables (heap memory) local variables (stack memory)
  • Different life cycles: member variables (exist with the existence of the object and disappear with the disappearance of the object) local variables (exist with the call of the method and disappear with the completion of the call of the method)
  • Different initialization values: member variable (with default initialization value) local variable (without default initialization value, it must be defined before assignment)
public class Car {
    String color;// Member variable

    // Member method
    public void drive(){
        int speed = 80;
        System.out.println("The car is running at"+speed+"Drive at a high speed...");
    }
}


public class Test {

    /*
        Differences between member variables and local variables:
            The positions of definitions are different: member variables are defined outside the method in the class, and local variables are defined in the method
            The location in memory is different: the member variable is in the heap area, and the local variable is in the stack area
            Different life cycles:
                Member variables exist with the creation of objects and are destroyed with the destruction of objects
                Local variables exist as the method is called and are destroyed as the method is called
            The default values are different:
                Member variables have default values
                Local variables have no default value and cannot be used directly without assignment
     */
    public static void main(String[] args) {
         // Create Car object
        Car car = new Car();
        // Call method
        car.drive();
    }
}

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-6HxA6ecT-1643877681375)(img\image-20200806121538241.png)]

Knowledge points – 2 encapsulation

Knowledge points – 2.1 private keyword

  • Overview: private is a permission modifier, which represents the minimum permission.
  • characteristic:
    • You can modify member variables and member methods.
    • Member variables and member methods modified by private can only be accessed in this class.
Use format of private
// private keyword modifies a member variable
private Data type variable name;

// private keyword modify member method
private Return value type method name(parameter list){
    code
}
case
public class Student {
    /**
     * full name
     */
    private String name;
    /**
     * Age
     */
    private int age;

    private void study(){
        // private can only be accessed in this class
        System.out.println("I am learning java");
    }

    public void show(){
        // public can be accessed
        System.out.println(name+","+age);
    }
}

public class Test {
    public static void main(String[] args) {
        /*
            private keyword:
                Overview: it is a permission modifier, the smallest permission
                characteristic:
                    1.private You can modify member variables and member methods
                    2.Member variables and member methods modified by private can only be accessed in this class.
                use:
                    Modify member variable format: private data type variable name;
                    Decorated member method format: private return value type method name (formal parameter list) {method body}
         */
        // Create Student class object
        Student stu1 = new Student();

        // Direct access to stu1's member variables
        //stu1.name = "Bingbing"// An error is reported during compilation because you do not have access rights
        //stu1.age = 18;//  An error is reported during compilation because you do not have access rights

        // Direct access to stu1's member methods
        //stu1.study();//  An error is reported during compilation because you do not have access rights

    }
}

Summary

- private Meaning of: private Is a permission modifier,Indicates the minimum permission
    - private Use of: Modify member variables and member methods
        Modify the format of member variables:  private Data type variable name;
		Format for decorating member methods:  private Return value type method name(parameter list){...}

- characteristic: cover private Modified member variable or member method,Can only be accessed in this class

Knowledge points – 2.2 why should attributes be encapsulated

explain

Why encapsulate attributes
public class Student {
    /**
     * full name
     */
    String name;
    /**
     * Age
     */
     int age;
}

public class Test {
    public static void main(String[] args) {
        /*
            Why encapsulate attributes:
                Assigning values to attributes by directly accessing member variables through object names will have potential data security risks. How should we solve them?
                Solution: do not let the outside world directly access member variables (that is, encapsulate / hide attributes)
            To hide member variables:
                1.Use the private keyword to decorate member variables
                2.Provide public access methods:
                    Public method for assigning values to member variables (set method)
                    Public method to get the value of member variable (get method)
         */
        // Create Student object
        Student stu1 = new Student();

        // Accessing member variables
        stu1.name = "Bingbing";
        // Assigning values to attributes by directly accessing member variables through object names will have potential data security risks. How should we solve them?
        stu1.age = -18;
        System.out.println(stu1.name + "," + stu1.age);// Bingbing, - 18

    }
}
  • Assigning values to attributes by directly accessing member variables through object names will have potential data security risks. How should we solve them?
  • Solution: do not let the outside world directly access member variables (that is, encapsulate attributes)
To encapsulate attributes
  1. Modify member variables with private

  2. For the member variable to be accessed, provide the corresponding getXxx method (get the value of the attribute) and setXxx method (assign a value to the attribute).

Summary

  • Use private to modify the member variable to hide the member variable from being directly accessed by the outside world
  • Provide a public access method (set\get method) for the member variable modified by private

Knowledge points – 2.3 set and get methods

  • For data security, data verification can be performed in the set method
Introduction to set and get methods
  • Because the attribute is decorated with the private keyword and cannot be accessed directly in other classes, we have to provide public access methods. We call this method set and get methods

    • Get method: provide "get variable name ()" method, which is used to obtain the value of member variable. The method is decorated with public
    • Set method: provide "set variable name (parameter)" method, which is used to set the value of member variable. The method is decorated with public
Writing of set and get methods
public class Student {
    /**
     * full name
     */
    private String name;
    /**
     * Age
     */
     private int age;

     // Method provided to assign value to member variable - set method
    public void setName(String s){
        name = s;
    }

    public void setAge(int a){
        if (a < 0 || a > 150){
            age = -1;
            System.out.println("Your data is illegal!");
        }else{
            age = a;
        }
    }
     // Provides a method to get the value of a member variable - get method
    public String getName(){
        return name;
    }

    public int getAge(){
        return age;
    }
}

public class Test {
    public static void main(String[] args) {
        /*
            Assigning values to attributes by directly accessing member variables through object names will have potential data security risks. How should we solve them?
            Solution: use private decoration and provide public access methods
         */
        // Create Student object
        Student stu1 = new Student();

        // Accessing member variables
        // How to hide attributes
        stu1.setName("Bingbing");
        stu1.setAge(-18);
        System.out.println(stu1.getName()+","+stu1.getAge());// Bingbing, - 1

        // There is no way to hide properties
        //stu1.name = "Bingbing";
        //stu1.age = -18;
        //System.out.println(stu1.name + "," + stu1.age);//  Bingbing, - 18
    }
}

Summary

  • Encapsulation of member variables:
    • Modify member variables with private
    • Provide public access methods (set assignment method, get value method)

Knowledge points – 2.4 this keyword

explain

problem

We found that the formal parameter name in setXxx method does not meet the requirement of knowing the meaning by name. If the modification is consistent with the member variable name, is it known by name? The code is as follows:

public class Student {
  private String name;
  private int age;

  public void setName(String name) {
    name = name;
  }

  public void setAge(int age) {
    age = age;
  }
}

After modification and testing, we found a new problem. The assignment of member variables failed. In other words, after modifying the formal parameter variable name of setXxx(), the method does not assign a value to the member variable! This is because the formal parameter variable name is the same as the member variable name, resulting in the member variable name being hidden, and the variable name in the method cannot access the member variable, so the assignment fails. Therefore, we can only use this keyword to solve the problem of duplicate names.

Meaning and use of this
  • This meaning: This represents the reference of the current calling method, which object calls the method of this, and this represents which object.

  • this keyword is mainly used to distinguish local variables and member variables with the same name

    • If the formal parameter of the method has the same name as the member variable, the variable without this modifier refers to the formal parameter, not the member variable
    • The formal parameter of the method does not have the same name as the member variable. The variable without this modifier refers to the member variable
  • Format of this:

    this.Member variable name
    
  • Use the variable in this modification method to solve the problem that the member variable is hidden. The code is as follows:

    public class Student {
        /**
         * full name
         */
        private String name;
        /**
         * Age
         */
         private int age;
    
         // Method provided to assign value to member variable - set method
        public void setName(String name){
            this.name = name;
        }
    
        public void setAge(int age){
            if (age < 0 || age > 150){
                this.age = -1;
                System.out.println("Your data is illegal!");
            }else{
                this.age = age;
            }
        }
         // Provides a method to get the value of a member variable - get method
        public String getName(){
            return name;
        }
    
        public int getAge(){
            return age;
        }
    }
    
    public class Test {
        public static void main(String[] args) {
            /*
                Problem 1: the formal parameter name of the set method cannot be well-known (does not conform to the identifier naming specification)
                Modify parameter name to conform to specification 1: solve
                Question 2: after changing the formal parameter name of the set method to conform to the naming specification, it is found that the set method cannot assign a value to the member variable
                Solution 2: use this keyword to distinguish member variables and local variables with the same name
                    Format: this Member variable name
                    this Who: This indicates which object calls the method of this
    
               Conclusion:
                    1.If there is a local variable with the same name as the member variable in the member method, you need to use the this keyword to distinguish it
                    2.If there is no local variable with the same name as the member variable in the member method, you do not need to use the this keyword to distinguish (you can use the member variable directly)
             */
            // Create Student object
            Student stu1 = new Student();
    
            // Accessing member variables
            // How to hide attributes
            stu1.setName("Bingbing");
            stu1.setAge(-18);
            System.out.println(stu1.getName()+","+stu1.getAge());// Bingbing, - 1
    
            Student stu2 = new Student();
            stu2.setName("empty");
    
        }
    }
    
    

    Tip: when there is only one variable name in the method, the default is to use this modifier, which can be omitted.

Summary

 this keyword:
	1.effect: Used to distinguish between member variables and local variables with the same name
	2.format: this.Member variable name
    3.this meaning:Represents the current object
	  Current object: Who calls this Method where,Who is the current object
      conclusion:Which object calls this Method where,this Which object does it represent

Knowledge points – 2.5 this memory principle

code
public class Test {
    public static void main(String[] args) {
        /*
            Problem 1: the formal parameter name of the set method cannot be well-known (does not conform to the identifier naming specification)
            Solution 1: modify the formal parameter name to conform to the naming specification
            Question 2: after changing the formal parameter name of the set method to conform to the naming specification, it is found that the set method cannot assign a value to the member variable
            Solution 2: use this keyword to distinguish member variables and local variables with the same name
                Format: this Member variable name
                this Who: This indicates which object calls the method of this

           Conclusion:
                1.If there is a local variable with the same name as the member variable in the member method, you need to use the this keyword to distinguish it
                2.If there is no local variable with the same name as the member variable in the member method, you do not need to use the this keyword to distinguish (you can use the member variable directly)
         */
        // Create Student object
        Student stu1 = new Student();

        // Accessing member variables
        // How to hide attributes
        stu1.setName("Bingbing");
        stu1.setAge(-18);
        System.out.println(stu1.getName()+","+stu1.getAge());// Bingbing, - 1

        Student stu2 = new Student();
        stu2.setName("empty");
        System.out.println(stu2.getName()+","+stu2.getAge());// Empty, 0

    }
}

public class Student {
    /**
     * full name
     */
    private String name;
    /**
     * Age
     */
     private int age;

     // Method provided to assign value to member variable - set method
    public void setName(String name){
        this.name = name;
    }

    public void setAge(int age){
        if (age < 0 || age > 150){
            this.age = -1;
            System.out.println("Your data is illegal!");
        }else{
            this.age = age;
        }
    }
     // Provides a method to get the value of a member variable - get method
    public String getName(){
        return name;
    }

    public int getAge(){
        return age;
    }
}

[the external chain picture transfer fails, and the source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-Cw2oAtyH-1643877681376)(img\image-20200905162906642.png)]

Summary

  • This indicates the current object (which object calls the method where this is located, this indicates which object)

Knowledge points – 2.6 package overview

Explanation:

Packaging overview
  • Is one of the three characteristics of object-oriented (encapsulation, inheritance, polymorphism)
  • It is the simulation of the objective world by object-oriented programming language. In the objective world, the member variables are hidden inside the object, and the outside world can not be operated directly
Encapsulation principle
  • Some information of the class is hidden inside the class, which is not allowed to be directly accessed by external programs. Instead, the operation and access of hidden information are realized through the methods provided by the class
  • For example, the member variable is decorated with private and the corresponding getXxx()/setXxx() method is provided
Packaging benefits
  • The method is used to control the operation of member variables, which improves the security of the code
  • The code is encapsulated by method, which improves the reusability of the code

Knowledge points – 3 Construction method

Knowledge points – 3.1 overview of construction method

Overview of construction method
  • Construction method is a special method, which mainly completes the creation of objects and the initialization of object data
Definition of construction method
  • format

    // Empty parameter construction method
     Modifier class name(){
        
    }
    
    // Parametric construction method
     Modifier class name(parameter list){
    	// Method body
    }
    
    
    
  • characteristic:

    • The method name is the same as the class name of the constructor
    • There is no need to construct a type or even return a value
  • Example code:

public class Student {
    /**
     * full name
     */
    private String name;
    /**
     * Age
     */
    private int age;

    // Construction method
    public Student(){
        System.out.println("Empty parameter method");
    }

    public Student(String name,int age){
        this.name = name;
        this.age = age;
    }

    public String getName(){
        return name;
    }

    public int getAge(){
        return age;
    }
}

public class Test {
    public static void main(String[] args) {
        /*
            Construction method:
                Overview: construction method is a special method, which is mainly used to create objects and assign values to attributes
                definition:
                    Nonparametric construction method:
                        Permission modifier class name (){
                        }
                    Parametric construction method:
                        Permission modifier class name (formal parameter list){
                            Assign values to attributes
                        }
                characteristic:
                    1.The constructor has no return value type and can't even write void
                    2.The name of the constructor is the class name
                    3.Call the constructor through new
                Use: call through new
         */
        // Create an object by calling the null parameter constructor
        Student stu1 = new Student();
        System.out.println(stu1.getName()+","+stu1.getAge());// null,0

        // Create an object by calling a parameterized constructor
        Student stu2 = new Student("Bingbing",18);
        System.out.println(stu2.getName()+","+stu2.getAge());// Bingbing, 18

    }
}

Summary

 Overview of construction method
        - Construction method is a special method,It mainly completes the creation of objects and the initialization of object properties

 Definition of construction method
        - format:
            Empty parameter construction method
                Modifier class name(){

                }

            Parametric construction method
                Modifier class name(parameter){
                    Method body(Assign values to attributes)
                }
        - characteristic:
            1.The method name of the constructor is consistent with the class name
            2.Construct has no return value,even void none
            
  Call construction method: adopt new To call

Knowledge points – 3.2 precautions for construction method

explain

  • Creation of construction method

    • If no construction method is defined, the system will give a default parameterless construction method
    • If the construction method is defined, the system will no longer provide the default construction method
  • Construction methods can be overloaded, and parameters can be defined or not.

  • Sample code

public class Student {
    /**
     * full name
     */
    private String name;
    /**
     * age
     */
    private int age;

    // Empty parameter construction method
    public Student(){

    }
    // Parametric construction method (full parameter construction method)
    public Student(String name,int age){
        this.name = name;
        this.age = age;
    }

    // Parametric construction method
    public Student(String name){
        this.name = name;
    }

    // Parametric construction method
    public Student(int age){
        this.age = age;
    }

    public void setAge(int age){
        this.age = age;
    }

    public int getAge(){
        return age;
    }
}

public class Test {
    public static void main(String[] args) {
        /*
            Precautions for construction method:
                1.The constructor has no return value and can't even write void
                2.The constructor name is consistent with the class name
                3.If a class does not define a constructor, the system will automatically generate a null parameter constructor
                4.If a class defines a constructor, the system will not automatically generate a null parameter constructor
                5.Construction methods can be overloaded
                6.The constructor can only assign a value to an attribute once, while the set method can assign a value to an attribute countless times
                  Because the constructor method is called, a new object is created

         */
         //Call the null parameter constructor to create an object
        Student stu1 = new Student();

        // Creating objects through parametric construction
        Student stu2 = new Student("Bingbing",18);
        Student stu3 = new Student("Bingbing",18);

        System.out.println(stu2.getAge());// 18
        // Assign a value to the attribute through the set method
        stu2.setAge(19);
        System.out.println(stu2.getAge());// 19
        stu2.setAge(20);
        System.out.println(stu2.getAge());// 20

    }
}

Summary

Precautions for construction method:
    - Creation of construction method
      - If no construction method is defined, the system will give a default parameterless construction method
      - If the construction method is defined, the system will no longer provide the default construction method
	- A constructor can only assign a value to an attribute once,Assignment cannot be repeated,Yes, who has set Method to assign values to attributes repeatedly
    - Construction methods can be overloaded, and parameters can be defined or not.
    - When defining the construction method,Do not write the return value,even void None of them
    - When defining the construction method,The constructor name and class name must be consistent

Knowledge points – 3.3 standard production

explain

Composition of standard classes

JavaBean is a standard specification of classes written in Java language. Classes that conform to JavaBean are required to be public, the attributes are decorated with private, and have parameterless construction methods, providing set and get methods for operating member variables.

public class ClassName{
  //Member variable private
  //Construction method
  //Nonparametric construction method [required]
  //Full parameter construction method [suggestion]
  //getXxx()
  //setXxx()
  //Member method	
}

Knowledge points – 4 API

Concept of API
  • What is API

    API (Application Programming Interface): application programming interface. Java API is a programmer's dictionary and a description document of the classes provided to us in JDK. These classes encapsulate the underlying code implementation. We don't need to care about how these classes are implemented. We just need to learn how to use these classes. So we can learn the classes provided by Java and know how to use them by querying the API.

    • API is actually the documentation of the core class library in jdk
    • For the core class library in jdk, you only need to know how to use it, and you don't need to care about how it is implemented
API usage steps
  1. Open the API help document.
  2. Click display to find the index and see the input box.
  3. Who are you looking for? Enter in the input box and press enter.
  4. Look at the bag. java.lang classes do not need to import packages, others do.
  5. See the explanation and description of the class.
  6. Look at the construction method.
  7. Look at member methods.
Demonstrate the use of API s
  • Open help document

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-3TEbWzcz-1643877681376)(img/01.png)]

  • Locate the input box in the index tab

    [the transfer of external chain pictures fails. The source station may have anti-theft chain mechanism. It is recommended to save the pictures and upload them directly (img-NNDjrmrz-1643877681377)(img/02.png)]

  • Enter Random in the input box

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-Oapy6cqx-1643877681378)(img/03.png)]

  • See which package the class is under

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (IMG movqsxgp-1643877681378) (IMG / 04. PNG)]

  • Look at the description of the class

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-xVmteenc-1643877681379)(img/05.png)]

  • Look at the construction method

[the transfer of external chain pictures fails. The source station may have anti-theft chain mechanism. It is recommended to save the pictures and upload them directly (img-GVDiKC1H-1643877681380)(img/06.png)]

  • Look at member methods

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-Qf4cSop3-1643877681380)(img/07.png)]

public class Test {
    public static void main(String[] args) {
        /*
            api Steps to use:
                1.Open api document
                2.Click display
                3.Click the index and enter the class \ interface to be searched in the input box
                4.Check the package of the class if it is in Java Lang packages do not need to be imported, and others need to be imported
                5.View the explanation of the class
                6.View the construction method of the class
                7.View member methods of a class

            Example: Scanner class
                1.View the package of class java Util Guide Package
                2.The explanation of the view class is a text scanner that can scan basic types of data and strings
                3.View the construction method of the class Scanner(InputStream source)
                4.View member methods of a class
                     byte nextByte()
                     short nextShort()
                     short nextInt()
                     Long nextLong()
                    boolean nextBoolean()
                     double nextDouble()
                    float nextFloat()

                     String nextLine()  You can get a line of string, space, enter and tab
                     String next()      Can get a single string, space, enter, tab key can not get

         */
        Scanner sc = new Scanner(System.in);
    }
}

summary

Must practice:
	1.Class definition
    2.Object creation and use
    3.Memory map of object-----The suggestion is well understood
    4.Production standard
        
- Be able to know the relationship between classes and objects
    A class is an abstraction of an object,An object is an instance of a class
    Objects are created from classes,There are as many objects as there are in the class
    Objects and objects are independent of each other
    
- Be able to complete the definition and use of classes
    format:
		public class Class name{
            Member variable
            Member method
        }
	create object: adopt new Call construction method to create object
	Use of objects:
		Accessing member variables:Object name.Member variable name
        Access member method:
			No return value method: Object name.Method name(Argument);
			Method with return value:
					Direct call: Object name.Method name(Argument);
					Assignment call: Data type variable name =  Object name.Method name(Argument);
					Output call: System.out.println( Object name.Method name(Argument));

- Be able to know the initialization process of the object in memory
    Draw a picture
- Be able to know the difference between local variables and member variables
    Different location,Different locations in memory,Different life cycles,The default value is different
    Different location: Member variable in class,Outside method;Local variables in methods
    Different locations in memory: Member variables in heap; Local variables in stack area
    Different life cycles; 
			Member variables exist with the creation of objects,Destroy as objects are destroyed
            Local variables exist with method calls,Destroy as the method is executed
    The default value is different: Member variables have default values,Local variables have no default values
        
- Can know private Characteristics of keywords
     The modified member variable or member method can only be accessed in this class
        
- Can know this Role of keywords
     Distinguish between member variables and local variables with the same name
        
- Be able to know the format and precautions of the construction method
     1.Constructor has no return value,even void Can't write
     2.The constructor name is consistent with the class name
     3.Construction methods can be overloaded
     4.Construction method through new To call
     5.A constructor can only assign a value to an attribute once
     6.If there is no construction method defined in a class,The system will generate null parameter construction method by default,
		If a constructor is defined in a class,The system will not generate null parameter construction method by default,
- Be able to write and test a standard class code
    Member variable--private
    Empty parameter construction method
    Full parameter construction method(proposal)
    set\get method
    Member method
- Be able to know the use steps of help documents
   open api
   Click display
   Click index,Enter what you want to find in the input box
   View package
   View the explanation of the class
   View the construction method of the class
   View member methods of a class

Keywords: Java Back-end

Added by xtian on Thu, 03 Feb 2022 12:14:19 +0200