JaveSE -- object oriented programming

Posted by Avochelm on Wed, 19 Jan 2022 03:00:33 +0100

I What is object oriented

Birds of a feather flock together, the thinking mode of classification. When thinking about problems, we will first solve what classifications are needed, and then think about these classifications separately. Finally, process oriented thinking is carried out on the details under a certain classification.

For describing complex things, in order to grasp them macroscopically and analyze them reasonably as a whole, we need to use object-oriented thinking to analyze the whole system. But when it comes to micro operation, it still needs process oriented thinking to deal with it.

OOP – object oriented programming

Its essence is to organize code in the form of classes and data in the form of objects.

Three characteristics: encapsulation, inheritance and polymorphism.

From the perspective of epistemology, there are objects before classes. Objects are concrete things. Class is abstract, which is the abstraction of objects; From the point of view of the object, the object has the template before the code.

II Review method

1. Definition of method

The syntax is as follows:

    Modifier return value type method name(...){
        Method body
        return Return value
    }

return represents the end of the method, and break represents jumping out of the swit ch or ending the loop!

Method name: hump principle + see the name and know the meaning

Parameter list: parameter type + parameter name

Exception thrown: questions, explained later

2. Method call

  1. Static method - static
    Can be called directly in other classes.

Static methods are loaded with classes

public class Student {

    public static void say(){
        System.out.println("The student spoke");
    }
}

public class Demo02 {
    public static void main(String[] args) {

        Student.say();
    }
}
  1. Non static method
    Instantiation is required before calling in other classes.

    Non static methods exist only after instantiation

public class Student {

    public void say(){
        System.out.println("The student spoke");
    }
}

public class Demo02 {
    public static void main(String[] args) {

        Student student = new Student();//instantiation 
        student.say();
    }
}

3. Value transfer and reference transfer

  1. pass by value
 public static void main(String[] args) {

        int a =1;
        System.out.println(a);//1
        change(a);
        //This method assigns the value of the original variable a to the formal parameter in the method, and then changes the value of the formal parameter in the method to 10 without changing the value of the original variable a
        System.out.println(a);//1
    }

    public static void change(int a){
        a = 10;
    }
  1. Reference passing

    public class Demo03 {
        public static void main(String[] args) {
    
            Person person = new Person();//instantiation 
            System.out.println(person.name);//null
    
            change(person);
      //Person is a real person, and its name is its attribute. Change its name to Liu Xin in the change method, which is a unique attribute of person
            System.out.println(person.name);//Liu Xin
        }
    
        public static void change(Person person){
            person.name = "Liu Xin";
        }
    }
    
    class Person{
        String name;
    }
    

III Creation of classes and objects

  1. Class is an abstract data type. It is an overall description / definition of a class of things, but it can not represent a specific thing.
  2. An object is a concrete instance of an abstract concept: it is a concrete instance rather than an abstract concept that can embody characteristics and functions.

1. Create and initialize objects

Use the new keyword to create an object. When using the new keyword to create an object, in addition to allocating memory space, it will also initialize the created object by default and call the constructor in the class. The syntax is as follows:

className objectName = new class();

Class = attribute + method

Class is abstract and needs to be instantiated. After instantiation, a class will return its own object!

2. Detailed explanation of constructor

Even if a class writes nothing, it will have a constructor.

Constructors in classes, also known as construction methods (must be mastered), must be called when creating objects. And the constructor has the following two characteristics:

  1. Must be the same as the class name
  2. There must be no return type and void cannot be written

Alt+Insert auto generate constructor!

public abstract class Application {

    public static void main(String[] args) {
        //new instantiates an object
        Person p1 = new Person();
        Person p2 = new Person("meteor");

        System.out.println(p1.name);//liuxin
        System.out.println(p2.name);//meteor
    }
}

public class Person {
    //Even if a class writes nothing, it will have a constructor.
    //Display definition constructor
    
    String name;
    //Instantiation initial value
    //1. Using the new keyword is essentially calling the constructor
    //2. Used to initialize values
    public Person(){
        this.name = "liuxin";
    }
    //Parameterized Construction: once a parameterized construction is defined, the definition must be displayed if there is no parameter
    public Person(String name){
        this.name = name;
    }
}

3. Memory analysis

Classes and methods define the method area stored in the heap, and store the constant pool and the properties corresponding to the class in the method area.

The static method area will store static methods and data types and load them together with classes. Objects can directly call methods in the static method area.

When new an object, first define a reference variable name, which will be stored in the stack; At the same time, allocate an address in the heap to call the corresponding class.

When you assign the characteristics of an object to an object, you make changes to the object properties in the heap.

 Pet dog = new Pet();
//dog is the name of the reference variable, and its properties are initialization values
//dog.name:null;
//dog.age:0;
//dog.shout();
 dog.name = "Wangcai";
 dog.age = 3;
 dog.shout();
//Define the dog attribute

Topics: Java Back-end