Object oriented programming (unfinished)

Keywords: Java Algorithm

1. What is object-oriented

(1) Process oriented & object oriented
Process oriented thinking: the steps are clear and simple, which is suitable for dealing with some relatively simple problems (linear thinking)
Object oriented thinking: birds of a feather flock together and classify; Suitable for dealing with complex problems and problems requiring multi person cooperation;
Attribute + method = class
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. However, specific to micro operation, it still needs process oriented thinking to deal with it.

**(2) * * the core idea of Java is object-oriented programming (OOP). OO represents object orientation.
The essence of object-oriented programming: organize code in the way of class and organize (encapsulate) data in the way of object

(3) Object oriented features
abstract

Three characteristics:
encapsulation
inherit
polymorphic

  • From the perspective of cognition: there are classes after existing objects. Object: concrete things. Class: it is abstract

  • From the perspective of code operation: there are classes before objects. A class is a template for an object. (if you don't have a partner, you just new one)

2. Review the definition and invocation of methods

  1. Definition of method:
    Modifier
    Return type
    The difference between break and return
    break: jump out of switch, end the loop and return: end the method and return a result (may or may not be empty)
    Method name: pay attention to the specification, see the meaning of the name
    Parameter list: (parameter type, parameter name)... Variable parameter
    Exception throw
  2. Method call:
    (1) Static and non static methods
    The method modified with the static keyword is a static method, which is called with * * class name. Method name () *.
public class Demo01 {
    public static void main(String[] args) {
        Student.speak();
    }
}

public class Student {
//Static method
    public static void speak(){
        System.out.println("People talk");
    }
}

The method not decorated with the static keyword is a non static method. Instantiate this class through the new keyword and call it with * * object name. Method name () * * again.

public class Demo01 {
    public static void main(String[] args) {
        //Instantiate this class
        //Object type object name = object value, but here the object value becomes the process of instantiating an object
        Student student = new Student();
        student.speak();
    }
}
public class Student {
//Non static method
    public void speak(){
        System.out.println("People talk");
    }
}

Note: a static method cannot call a non static method.

  //Load with class
    public static void a(){
       // b();  report errors
    }
    //Object does not exist until it is instantiated
    public  void b(){

    }

(2) Formal and argument

public class Demo03 {
    public static void main(String[] args) {
        //The types of actual parameters and formal parameters should correspond!
        int add = Demo03.add(1, 2);
        System.out.println(add);
    }
    public static int add(int a, int b) {
        return a + b;
    }
}

(3) Value passing and reference passing
(4) this keyword

this.name = name;//this. Represents the of the current class, and name represents the passed in

3. Creation of classes and objects

  • Class is an abstract data type, which is the overall description / definition of a certain kind of things, but it can not represent a specific thing
  • Objects are concrete instances of abstract concepts

(1) Creating and initializing objects
Create an object using the new keyword

public class Demo04 {
    public static void main(String[] args) {
        Student s1 = new Student();
        Student s2 = new Student();
        s1.name = "tutu";
        s1.age = 2;
        s2.name = "aoao";
        s2.age = 1;
        System.out.println(s1.name + "," + s1.age);
        System.out.println(s2.name + "," + s2.age);
    }
}
public class Student {
 String  name;
 int age;
}

(2) Constructor
The constructor in the class also becomes a constructor, which must be called when creating an object. And the constructor has the following characteristics:

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

A class will have a parameterless constructor by default. Once a parameterized construct is defined, a nonparametric construct must display the definition.

public class Student {
 String  name;
 int age;
    //Using the new keyword is essentially calling the constructor
    //The constructor is used to initialize the value. The initialization value of the parameterless constructor is null
    public Student() {
    }
    //Parametric constructor
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

Alt+insert will be generated automatically

4. Three object-oriented features

5. Abstract classes and interfaces

6. Internal classes and OOP practice

Posted by Wakab on Sun, 21 Nov 2021 00:41:04 -0800