[JavaSE] Preliminary understanding of classes and objects

【Objective of this program】

1. Master the definition of classes and the instantiation of objects

2. Master the use of member variables and member methods in the class

3. Master the entire initialization process of the object

Table of contents

1. Object-oriented preliminary cognition

2. Class definition and use

3. Class instantiation

4. this reference 


1. Object-oriented preliminary cognition

1.1 What is object-oriented

Java is a pure object-oriented language (Object Oriented Program, OOP for short), in the object-oriented world, everything is an object. Object-oriented is an idea of ​​solving problems, mainly relying on the interaction between objects to complete one thing. Using object-oriented thinking to deal with programs is more in line with people's cognition of things, and it is very friendly to the design, expansion and maintenance of large programs.

1.2 Object-oriented and process-oriented

1. Traditional laundry process

 

The traditional way: the focus is on the process of washing clothes, and it may not work if one link is missing.

Moreover, different clothes have different washing methods, time lengths, and wringing methods, which make it more troublesome to deal with. If you want to wash your shoes in the future, that's another way to put them. Writing code in this way will be more troublesome for future expansion or maintenance.

2. Modern Laundry Process

There are four objects in total: Person. Clothes, washing powder, washing machine,
the whole process of washing clothes: a person puts clothes into the washing machine, pours washing powder, starts the washing machine, and the washing machine will complete the
washing process and spin dry.
The whole process is mainly: people, clothes, washing powder, and washing machines are interactively completed, and people don't need to know
how the new washing machine washes clothes and how it dries them.

Process in an object-oriented manner, and do not pay attention to the process of washing clothes. The user does not need to care about how the washing machine washes the clothes and how to dry them. The user only needs to put the clothes into the washing machine, pour in the washing powder, and turn on the switch. That is, it is done through the interaction between objects.

Note: Process-oriented and object-oriented are not a language, but a method to solve problems. There is no such thing as good or bad, and both have their own specific application scenarios.

2. Class definition and use

2.1 Class definition format

The class keyword needs to be used when defining a class in java . The specific syntax is as follows

 class is the keyword to define the class, ClassName is the name of the class, and {} is the subject of the class.

The things contained in a class are called members of the class. Attributes are mainly used to describe classes, which are called class member attributes or class member variables. The method mainly explains what functions the class has, and is called the member method of the class.

Next, let's define a class that describes a person:

class Person{
    //属性
    public String name;
    public int age;
    //行为方法
    public void sleep(){

    }
}

The above defines a class named Person, which has the following properties and behavior methods:

Attributes:

  • name: A string variable to store the person's name.
  • age: Integer variable used to store the age of the person.

Behavior method:

  • sleep(): Indicates human sleep behavior, and specific implementation logic can be written in the method.

Precautions

Note that the class name uses the big hump definition

The wording before members is unified as public, which will be explained in detail later

The method written here does not contain the static keyword. It will be explained in detail later


2.2 Exercises

define a student class

public class Student{
    public String name;
    public String gender;
    public short age;
    public double score;
    public void DoClass(){}
    public void DoHomework(){}
    public void Exam(){}
}

Precautions:

1. Generally, only one class is defined in a file

2. The class where the main method is located generally needs to be decorated with public (Note: Eclipse will find the main method in the public-modified class by default)

3. The public modified class must be the same as the file name

4. Do not easily modify the name of the public modified class. If you want to modify it, modify it through the development tool

3. Class instantiation

3.1 What is instantiation

Defining a class is equivalent to defining a new type in the computer , similar to int and double, except that int and double are built-in types of the java language, and the class is a new type defined by the user , such as the above: PetDog class and Student class.

The process of creating an object with a class type is called the instantiation of the class . In java, the new keyword is used to instantiate the object with the class name.

public class Main{
    public static void main(String[] args) {
        PetDog dogh = new PetDog(); //通过new实例化对象
        dogh.name = "阿黄";
        dogh.color = "黑黄";
        dogh.barks();
        dogh.wag();
        PetDog dogs = new PetDog();
        dogs.name = "阿黄";
        dogs.color = "黑黄";
        dogs.barks();
        dogs.wag();
    }
}

Precautions

The new keyword is used to create an instance of an object.

Use . to access properties and methods in an object.

Multiple instances of the same class can be created. 

 3.2 Description of classes and objects

1. A class is just a model, which is used to describe an entity and defines which members the class has.

2. A class is a custom type that can be used to define variables.

3. A class can instantiate multiple objects, and the instantiated objects occupy the actual physical space to store class member variables

4. Let’s use an analogy. Instantiating an object from a class is like building a house using architectural design drawings in reality. Objects can actually store data and occupy physical space.

 

4. this reference

4.1 Why is there a this reference

public class Date {
    public int year;
    public int month;
    public int day;
    public void setDay(int y, int m, int d){
        year = y;
        month = m;
        day = d;
    }
    public void printDate(){
        System.out.println(year + "/" + month + "/" + day);
    }
    public static void main(String[] args) {
// 构造三个日期类型的对象 d1 d2 d3
        Date d1 = new Date();
        Date d2 = new Date();
        Date d3 = new Date();
// 对d1,d2,d3的日期设置
        d1.setDay(2020,9,15);
        d2.setDay(2020,9,16);
        d3.setDay(2020,9,17);
// 打印日期中的内容
        d1.printDate();
        d2.printDate();
        d3.printDate();
    }
}

The above code defines a date class, and then creates three objects in the main method, and sets and prints the objects through the member methods in the Date class. The overall logic of the code is very simple without any problems.

But after careful consideration, there are two questions:

1. The formal parameter name is accidentally the same as the member variable name:

 Who assigns values ​​to whom in the function body? Member variable to member variable? Parameter to parameter? Parameters to member variables? Member variable parameters? I guess I can't figure it out.

2. The three objects are calling the setDate and printDate functions, but there is no description about the objects in these two functions. How do the setDate and printDate functions know which object's data is printed?

 All let this reference to unveil this layer of mystery.

4.2 What is this reference

The this reference points to the current object (the object that calls the member method when the member method is running), and all member variable operations in the member method are accessed through this reference . It's just that all operations are transparent to the user, that is, the user does not need to pass it, and the compiler completes it automatically.

public class Date {
    public int year;
    public int month;
    public int day;
    public void setDay(int year, int month, int day){
        this.year = year;
        this.month = month;
        this.day = day;
    }
    public void printDate(){
        System.out.println(this.year + "/" + this.month + "/" + this.day);
    }
}

Note: this refers to the object that calls the member method.

4.3 Features of this reference

1. The type of this: the corresponding class type reference, that is, which object is called is the reference type of that object.

2. This can only be used in "member methods".

3. In the "member method", this can only refer to the current object and cannot refer to other objects.

4. this is the first hidden parameter of the "member method", and the compiler will automatically pass it. When the member method is executed, the compiler will be responsible for passing the reference of the object calling the member method to the member method, and this is responsible for receiving it.

Simple demonstration at the code level ---> Note: The Date class on the right side of the figure below can also be compiled.

 

Guess you like

Origin blog.csdn.net/m0_73648729/article/details/132051190