preface

Everybody is good! I’m xiao Sheng! I want to be an engineer of Java back-end development in the future. Recently, I want to further study Java, mainly to consolidate the foundation, so I want to talk about Java with you. At the same time, I once again declare that the following content is my summary through learning the dark horse program ape and other videos, but are pure hand and add personal summary!

Java is a kind of object-oriented Programming language, Object-oriented Programming, Object-oriented Programming is a widely used computer Programming language, with cross-platform, object-oriented, generic Programming characteristics, widely used in enterprise Web application development and mobile application development.

Graph LR (Java01) -- - > B (classes and objects) A - > C (object memory photo) A - - > D (member variables and local variables), A -- -- > E (package) A > F (method)

Classes and objects

1.1 What is an object? What are attributes of objects? What is the behavior of an object?

An object is a classThe instanceThere are states and behaviors.

Note: Java objects are not girlfriends

1.2 What is Object facing? How to build object-oriented thinking?

Basic concepts of object orientation

Its essence is the abstract thinking process reflected by the model and the object-oriented method (Baidu Encyclopedia) is a programming thinking, but also a way to think about problems

How do you build object-oriented thinking?

1, first whole, then part 2, first abstract, then concrete 3, what can be done, how to do

Three core features of object orientation

1. Manageability: it can combine functions with data for convenient management. 2. Expansibility: It separates object-oriented design from mode-based design and facilitates software modification. 3. Reusability: It is the core idea of object-oriented software development and improves the development efficiency. Object-oriented programming features of abstraction, inheritance, encapsulation and polymorphism all revolve around this core.

1.3 What is a Class?

Class is aThe template, which describes the behavior and state of a class of objects.

1.4 What is the relationship between classes and objects?

Class and object are the collective names of two computer languages based on computers. Objects are abstractions of objective things, and classes are abstractions of objects. The relationship is that an object is an instance of a class, and a class is a template for an object.

1.5 Definition of class?

package javase_phone_object;
/* * Class definition * class definition steps * Define class * Write class member variables (properties) * Write class member methods (behavior) * example Phone * class name: Phone * * Member variable * brand * Size * Price * * Member method * chat * pay * shopping * */
public class Phone {
    // Member variables
    String brand = null;
    int size;
    int price;

    // Member methods
    public void chat(a){
        / / the method body
        System.out.println("-- In conversation ~~");
    }
    public void pay(a){
        System.out.println("-- Heartache ~~");
    }
    public void shopping(a){
        System.out.println("-- in consumption..."); }}Copy the code

1.6 Object Usage

Create an object

Object name = new Class name ();

Using the object

Format object name variable name object name. The method name ()

package javase_phone_object;

/* * Create an object * format: class name Object name = new class name (); * Use object * format * object name. Variable name * object name. Method name () */
public class PhoneDemo {
    public static void main(String[] args) {
        // Create an object
        Phone P = new Phone();
        // Use member variables
        System.out.println("brand\t"+P.brand);
        System.out.println("price\t"+P.price);
        System.out.println("size\t"+P.size);

        P.brand = "Huawei P30pro";
        P.price = 5499;
        P.size = 6.47; / /"

        System.out.println("brand\t"+P.brand);
        System.out.println("price\t"+P.price);
        System.out.println("size\t"+P.size);

        // Use the member methodP.chat(); P.pay(); P.shopping(); }}Copy the code

Case: animal

What do animals have in common?

Requirements: First define an animal class, then define an animal test class, where the object completes the member variables and member methods

Create a class

package javase_animal_object;
/ / create the class
public class Animal {
    // Member variables
    String name;
    String color;
    int age;

    // Member methods
    public void eat(a){
        System.out.println("--- eating ---");
    }

    public void run(a){
        System.out.println("--- runing ---");
    }

    public void sleep(a){
        System.out.println("--- sleeping ---"); }}Copy the code

Test class (main function required)

package javase_animal_object;
// Test class (need main function)
public class AnimalDemo {
    public static void main(String[] args) {
        // Create an object
        Animal animal = new Animal();

        // Use member variables
        animal.age = 10;
        animal.name = "The elephant";
        animal.color = "white";
        System.out.println(animal.name);
        System.out.println(animal.age);
        System.out.println(animal.color);

        // Use the member methodanimal.eat(); animal.run(); animal.sleep(); }}Copy the code

2. Object memory map

2.1 Object Memory Map (Single Object)

2.2 Object Memory Map (Multiple Objects)

Similar to the memory map above for a single object

2.3 Object Memory Diagram (Multiple Objects Point to the Same Object)

Member variables and local variables

3.1 What are Member variables and local variables?

Member variables, also known as attributes, are declared in a class. A member variable has a default value if it is not assigned. The default value varies depending on the data type. Local variables outside a method in a class are declared in a method or in a code block. Local variables have no default values, which means they have to be declared, then assigned, and then used

3.2 Differences between member variables and local variables

Four, encapsulation

4.1 Private Keyword

  • Is a permission modifier
  • You can decorate members (member variables and member methods)
  • Members are protected from being used by other classes only within this class

Member variables decorated with private are encapsulated if they need to be used by other classes

  • Provide a “get/set variable name ()” method to get the value of a member variable, using public
  • Provide the “show variable name ()” method to display the value of a member variable, using public

4.2 Use of private

package javase_car_object;
/ / test class
public class CarDemo {
    public static void main(String[] args) {
        // Create an object
        Car car = new Car();

        car.getData("The mass"."white".120000.0); car.show(); }}Copy the code
package javase_car_object;
/ / car class
public class Car {
    // Member variables
    private String brand;
    private String color;
    private Double price;
    
    // Member methods
    public void getData(String bra,String col,Double pri){
        brand = bra;
        color = col;
        price = pri;
        System.out.println("-- Data acquisition success --");
    }
    
    public void show(a){
        System.out.println("Car brand :"+brand+"\t Car Color :"+color+"\t Car Price :"+price); }}Copy the code

4.3 This keyword

The this keyword optionally refers to the instance variable of the current class. If there is more than one sense between an instance variable and a parameter, the this keyword can be used to explicitly specify the variable to resolve the problem of more than one sense. When do YOU use this? Resolve local variables to hide member variables

4.4 packaging

1. Overview of encapsulation

  • Is one of the three characteristics of object orientation (encapsulation, inheritance, polymorphism)
  • Object oriented programming language is the simulation of the objective world, in the objective world member variables are hidden inside the object, the outside is unable to operate

2. Encapsulation principle

Some information of the class is hidden inside the class, and external programs are not allowed to access it directly. Instead, the method provided by the class is used to realize the operation of hidden information and access member variable private, providing corresponding methods

3. Encapsulate benefits

  • Methods to control the operation of member variables improve code security
  • The code is encapsulated by methods, which improves the reusability of the code

5. Construction method

5.1 Overview of construction methods

A constructor is a special method used to create an object

/ / format
public classThe name of the class{modifier class name (parameter) {}}Copy the code

New Demo() in the following code; Call the constructor

public class Demo(a){
	public static void main(String[]args){
		Demo demo = newDemo(); }}Copy the code

5.2 Implementation of construction method

Initializes the form of each parameter

// This is not the case
new Car();
new Car("The mass"."white".120000.0);
Copy the code
public Car(String brand,String color,Double price){
     this.brand = brand;
     this.color = color;
     this.price = price;
     System.out.println("-- Data acquisition success --");
 }
Copy the code

The test class

package javase_car_object;
/ / test class
public class CarDemo {
    public static void main(String[] args) {
        // Create an object
        Car car = new Car("The mass"."white".120000.0);
        car.show();
        Car car2 = new Car();
        car2.getData("BMW"."bule".320000.0); car2.show(); }}Copy the code

The Car class

package javase_car_object;
/ / car class
public class Car {
    // Member variables
    private String brand;
    private String color;
    private Double price;
    public  Car(a){};
    public Car(String brand,String color,Double price){
        this.brand = brand;
        this.color = color;
        this.price = price;
        System.out.println("-- Data acquisition success --");
    }

    // Member methods
    public void getData(String brand,String color,Double price){
        this.brand = brand;
        this.color = color;
        this.price = price;
        System.out.println("-- Data acquisition success --");
    }

    public void show(a){
        System.out.println("Car brand :"+brand+"\t Car Color :"+color+"\t Car Price :"+price); }}Copy the code

5.3 Precautions for construction methods

  • 1. Constructor creation

    If you do not give a constructor to a class, the system will default to a no-argument constructor. If you give a constructor with/without, the system will no longer give a no-argument constructor

  1. Recommended usage

    Write the no-argument constructor by hand, whether used or not