Java from getting started to giving up 06 (what is object-oriented?)

What is object-oriented?

Process oriented

	The process oriented method of solving problems is to follow the steps we have analyzed, step by step, follow the principle of top-down and gradual refinement, and focus on the process and steps of solving problems.

object-oriented

	Java Language is an object-oriented programming language, and object-oriented is a programming language. In object-oriented reality, object generally refers to all transactions. For each transaction, it has its own attributes and behaviors; The object-oriented idea is to abstract the attributes and behavior characteristics of things and describe them as the design idea of computer events in the process of computer programming with reference to real transactions. Different from the process oriented idea, object-oriented emphasizes that the functions that need to be achieved can be achieved by calling the behavior of objects, and there is no need to implement them step by step.

What is a class?

**Class: * * a collection with the same attributes and behaviors. It is an abstract concept and does not point to specific transactions.

  • Attribute: describes the characteristics of the object, which is embodied as member variables in the program;
  • Behavior: the action or state of an object, which is embodied as a method in a program.
    Class definition:
    [modifier] class name{
    / / attribute (member variable)
    / / behavior (method)
    }
    Example: define a car class
public class Car {
    //Define attributes
    String brand;//brand
    String color;//colour
    int seat;//Seating capacity

    //Define method (behavior)
    void go(){
        System.out.println("Driving...");
    }

    void stop(){
        System.out.println("Stop...");
    }

    void fly(){
        System.out.println("Flying...");
    }
}

What is an object?

**Object: a concrete instance of * * class, a concrete embodiment of a class of things.

  • Create object:
    Class name object name = new class name ([parameter]);
  • Read write properties:
    Object name. Attribute name
  1. Call method
    Object name. Method name ([parameter]);
public class CarTest {
    public static void main(String[] args) {
        //Create an object of Car (instantiation)
        Car car = new Car();
        //Set the properties of the object
        car.brand = "Chang'an";
        car.color = "white";
        car.seat = 5;
        System.out.println(car.brand + car.color + car.seat);

        //Execute the object behavior (call the method of the object)
        car.go();;
        car.stop();
        car.fly();
    }
}

Relationship between classes and objects

  • Class is a collection of objects, which is abstract;
  • Objects are concrete instances of classes and are concrete;
  • The process from class to object is the instantiation of class.
    In Java, the new keyword is used to create objects.

Differences between member variables and local variables

Member variables

  • Member variables refer to variables defined in classes, also known as object attributes. Similar to arrays, they also have default values, so member variables can be used directly without initialization.
	//Define attributes
    String brand;//brand
    String color;//colour
    int seat;//Seating capacity
  • Member variables can be divided into class variables (decorated with static) and instance variables (not decorated with static).
  • The scope of action of class variables is the same as the life cycle of a class, that is, class variables are valid during the existence of a class.
  • Instance variables exist since the instance is created. When the instance is destroyed, the instance variables will also be destroyed.
  • Scope: the scope of the member variable is the entire class.

Access method of member variables:

  • As long as the class exists, the program can access the class variables
    Class. Class variable
  • As long as the instance exists, the program can access the instance variables
    Instance. Instance variables
  • Class variables can also be accessed by instances of classes
    Instance. Class variable

Default value of member variable:

data typeDefault value
plastic0
float 0.0
Boolean typefalse
characterInvisible characters ('\u0000')
reference typenull

local variable

  • Variables defined in methods are called local variables.
  • Local variables can be divided into:
    1. Formal parameter
    2. Method local variable
    3. Code block local variables
  • Local variables have no default values, and all other variables except formal parameters must be explicitly initialized.
  • The scope of a local variable is valid within the brace in which it is located.
  • After the method is executed, the local variables will disappear.

Example:

  1. Create a Student class
    Attribute: name, age, gender
    Behavior: study, exam, rest
package com.hqyj;

public class Student {
    String name;
    int age;
    char sex;

    void study(){
        System.out.println("I am learning...");
    }
    void exam(){
        System.out.println("In the exam...");
    }
    void rest(){
        System.out.println("Resting...");
    }
}


public class StudentTest {
    public static void main(String[] args) {
        Student student = new Student();
        student.name = "Zhang San";
        student.age = 22;
        student.sex = 'female';

        student.study();
        student.rest();
        student.exam();
    }
}
  1. Randomly generate a number (1~100), guess and enter a number to judge whether you guessed right. If you guessed right, end. If you guessed wrong, prompt and guess again.
import java.util.Scanner;

public class GuessNum {
    /**
     * Randomly generate a number (1~100), guess and enter a number to judge whether you are right. If you are right, it ends,
     * If you guess wrong, prompt and guess again.
     */
    int initNum;//Randomly generated numbers
    int givenNum;//Player guessed number

    /**
     * Generate random numbers from 1 to 100
     * random Generated is a decimal of type 0~1 double
     */
    void generateNum(){
        double random = Math.random();
        initNum = (int) (random * 100);
    }

    /**
     * Receive the number entered by the player
     */
    void acceptInput(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter the number you guessed:");
        givenNum = scanner.nextInt();
    }

    /**
     * Judge whether the number guessed by the player is correct
     * @return If you guess correctly, it will return false; if you guess incorrectly, it will return true
     */
    boolean checkNum(){
        boolean b = true;
        if (givenNum == initNum){
            System.out.println("Congratulations, you guessed right!");
            b = false;
        }else if (givenNum > initNum){
            System.out.println("Your guess is too big, please guess again!");
        }else{
            System.out.println("Your guess is small, please guess again!");
        }
        return b;
    }
}

//.......................................

public class PlayGuessNum {
    public static void main(String[] args) {
        GuessNum guessNum = new GuessNum();
        //Generate random numbers
        guessNum.generateNum();
        do {
            //User guessing
            guessNum.acceptInput();
        }while (guessNum.checkNum());//Judge whether you guessed right
    }
}

The difference between member variables and local variables:
3. Member variables are part of a class and exist with the creation of a class or instance; The local variables disappear automatically with the method call;
4. Member variables can be assigned no initial value, and will be automatically assigned the initial value with the default value of the type (member variables modified by final need to be assigned the initial value at the time of definition); The local variable must be assigned a value to the defined variable before use.

Tags: Java programming language

Posted by the_ut_tick on Sat, 30 Jul 2022 22:17:10 +0300