JAVA object-oriented and other basic concepts


JAVA object-oriented -- basic concepts of classes and objects
References: Niuke network , this content is only a summary of my learning knowledge.

What is an object

One. Two ideas:

  1. Process oriented: analyze the steps to solve the problem, then use functions to realize each step one by one, and call these functions again.
  2. Object oriented: analyze which objects / things / objects should be included in the system and the relationship between them, and then describe these objects and relationships with specific method.

II. How to use object-oriented thinking

Three. Relationship between classes and objects

  1. Class: abstract, not concrete instance. Information used to describe a batch of objects
  2. Object: specifically, it is an instance created by the JVM according to the class description. This process is called instantiation.
  3. Example: person: class perth: object
  4. Class is a user-defined data type. Creating objects through this class is actually initializing variables of this type.
    User defined data types cannot be referenced directly. They can only be initialized with new.

How to define classes

1. How to describe objects

Attributes: static characteristics of the object; It is described by variables. Such variables are called member variables.
Behavior: dynamic characteristics of objects; Described by methods, such variables are called member methods.

  1. Define member variables
    Syntax: modifier data type variable name = initial value; Default values will be given if not initialized
  2. Define member methods
    Syntax: modifier return type method name (parameter list) {...} note: member methods cannot be modified by static keyword. No return type is void. There is no need to return

2. Member variables are not initialized

When a member variable is printed directly without initialization (no assignment), the JVM will give it a default value. The default values of different types are different.
example:

Game.java:

package nowcoder3.part02;
/*
* Detailed explanation of member method
* */
public class Game {
    public static void main(String[] args) {


        Car c = new Car();
        System.out.println(c.brand);
        System.out.println(c.color);
        System.out.println(c.maxSpeed);
    }
}

Car.java:

package nowcoder3.part02;

public class Car {
    //Member variable
    String brand;
    String color;
    int maxSpeed;
    }

Operation results:

Default values for different types:

3. How to define a class:

Actual:
Game.java:

package nowcoder3.part01;

//Games

public class Game {
    public static void main(String[] args) {
        //Create a racing car
        Car c = new Car();
        //Accessing member variables
        c.brand="bmw";
        c.color="black";
        System.out.println(c.brand);
        System.out.println(c.color);
        System.out.println(c.maxSpeed);
        //Access member method
        c.run();

        //Create another object
        Car r = new Car();
        r.brand = "Mercedes-Benz car";
        r.color = "green";
        System.out.println(r.brand);
        System.out.println(r.color);
        System.out.println(r.maxSpeed);
        r.run();

        //Print objects directly
        System.out.println(c);
        System.out.println(r);
    }
}

Car.java:

package nowcoder3.part01;
//Racing class
public class Car {
    //Member variable
    String brand;
    String color;
    int maxSpeed = 500;
    //Member method
    void run(){
        System.out.println("Running...");
    }
}

Operation results:


Because the hash code values (addresses) of the two objects are different, they are two objects.

Detailed explanation of member method

  1. Member methods can directly access all member variables of the current class;
  2. Member methods can directly access all member methods of the current class;
  3. Member method parameters can be basic data types or reference data types, including user-defined types.

example:
Game.java:

package nowcoder3.part02;
/*
* Detailed explanation of member method
* */
public class Game {
    public static void main(String[] args) {

        Car c1 = new Car();
        c1.brand="bmw";
        c1.color="black";
        c1.maxSpeed=666;
        c1.run();

        Car c2 = new Car();
        c2.brand="Benz";
        c2.color="gules";
        c2.maxSpeed=999;
        c2.run();

    }
}

Car.java:

package nowcoder3.part02;

public class Car {
    //Member variable
    String brand;
    String color;
    int maxSpeed;
    //travel
    void run(){
        //Variables declared within a method are local variables and can only be accessed within a method
        //Call member variable
        System.out.println(color + brand +"Speed is" + maxSpeed);
        //Call member method
        startup();
        speedup();
        stop();
    }


    //start-up
    void startup(){
        System.out.println(brand+"start-up!");
    }
    //accelerate
    void speedup(){
        System.out.println(brand+"Speed up!");
    }
    //brake
    void stop(){
        System.out.println(brand+"Brake!");
    }
}

Operation results:

JVM memory model

When the JVM starts, it will apply for a piece of memory from the system, which is divided into several sub areas to store different forms of memory. data

1. Reactor:

Heap is used to store data of reference type;
These data are out of order with each other
The data in the heap can be used repeatedly;
The JVM will regularly clean up the garbage data in the heap;

2. Stack:

The stack stores data in a method unit, which is called a method stack frame;
The data stored in the stack is orderly and follows the rule of first in and last out;
After the method call, the method stack frame it occupies will be released immediately;

Note: the order of out stack and in stack is different

3. Relationship between stack and heap:

Object creation process

Understand through examples:

public static void main(String[] args) {
  
        Car c = new Car();
        c.brand="bmw";
        c.color="black";
        c.maxSpeed=500;
        c.run();
        }

1. Create object:

Car c = new Car();
Step 3 main() stack - > create object - > assign value

2. Access member variables

c.brand="bmw";
c.color="black";
c.maxSpeed=500;

3. Call member method: c.run();

Keywords: Java Back-end

Added by stokie-rich on Mon, 08 Nov 2021 01:27:59 +0200