Summary of the second week

1, Array;

  1. Copy: system arraycopy(a,1,b,0,4);
                int[ ] b = Arrays.copyOf(a,6);
  2. Sorting: arrays sort(arr); // Ascending order (from small to large)

2, Method:

  1.  
    1. Encapsulate a specific business logic function
    2. Be as independent as possible and do only one thing in one way
    3. Method can be called repeatedly
    4. Reducing code duplication is conducive to code reuse and code maintenance
  2. Definition method: five elements
    1. Modifier return value type method name (parameter list){

      Method body ------------ specific business logic function implementation

      ​ }

  3. Call method:

    1. No return value: method name (pass parameter with parameter);

    2. With return value: data type variable = method name (with parameter passed to parameter);

  4. return:

    1. Return value// 1) End the execution of the method 2) return the result to the caller

      ---------- used in methods with return values

    2. return; //1) End method execution

      ------------------ used in methods without return value

  5. Supplement:

    1. Formal parameter: formal parameter. The parameter when defining a method is a formal parameter
      Argument: actual parameter. The parameter when calling the method is an argument

3, Classes and objects

  1. What is class? What is an object?
    1. Real life is composed of many objects. Classes are extracted based on objects
    2. Object: a single individual / thing that really exists in software.
      Class: category / type, representing a class individual.
    3. Class is the model of object, and object is the concrete real column of class.
    4. Class can contain:
      1. Properties / characteristics of objects ---------------------- member variables
      2. Properties / characteristics of objects ---------------------- member variables
    5. A class can create multiple objects.
  2. How to create a class? How to create objects? How do I access members?
    public class Student { //The Student class is a reference type we make ourselves
        //Member variable
        String name;
        int age;
        String address;
        //method
        void study(){
            System.out.println(name+"I'm learning...");
        }
        void sayHi(){
            System.out.println("Hello, my name is"+name+",this year"+age+"Years old, live at home"+address);
        }
    }
    
    public class StudentTest {
        public static void main(String[] args){
            //Create a student object
            Student zs = new Student();
            //Assign values to member variables
            zs.name = "zhangsan";
            zs.age = 25;
            zs.address = "Langfang, Hebei";
            //Method call
            zs.study();
            zs.sayHi();
    
            Student ls = new Student();
            ls.name = "lisi";
            ls.age = 24;
            ls.address = "Jiamusi, Heilongjiang";
            ls.study();
            ls.sayHi();
    
            //1) Created a student object
            //2) Assign default values to all member variables
            Student ww = new Student();
            ww.study();
            ww.sayHi();
    
        }
    }
    

  3. Overload / overloading of methods -- more convenient for users to access
    1. Occurs in the same class, with the same method name and different parameter lists
    2. The compiler will automatically bind the method according to its signature at compile time
      //Overloaded demo
      public class OverloadDemo {
          public static void main(String[] args) {
              Aoo o = new Aoo();
              o.show(); //The compiler automatically binds a method based on its signature
              o.show("zhangsan");
              o.show(25);
              o.show("zhangsan",25);
              o.show(25,"zhangsan");
          }
      }
      
      class Aoo{
          void show(){}
          void show(String name){}
          void show(int age){}
          void show(String name,int age){}
          void show(int age,String name){}
          //int show(){ return 1;} // Compilation error, overload is independent of return value type
          //void show(String address) {} / / compilation error. Overloading is independent of parameter name
      }
      

Supplement:

  1. High quality code: ------------------ future goals, new year
    1. Good reusability, expansibility, maintainability, portability, robustness, readability and efficiency
  2. Default rule:
    byte,short,int,long,char-------------0
    float,double-------------------------0.0
    boolean------------------------------false
     reference type------------------------------null  
    
  3. //To access an object, you need to reference zs
                quote
     Data type reference type variable points to       object
    Student     zs         =    new Student(); 
    
  4. Method signature: method name + parameter list

Keywords: Java

Added by amandas on Tue, 08 Mar 2022 13:17:38 +0200