Classes and objects one (how classes are defined and objects are instantiated)

1. Preliminary understanding of the object

1.1 What is Object Orientation

image.png
Java is a pure object-oriented language (Object Oriented Program, inheriting OOP). In the object-oriented world, everything is an object. Object-oriented is a kind of problem-solving idea, which mainly relies on the interaction between objects to complete one thing . Using object-oriented thinking to involve programs is more in line with people's cognition of things, and is very friendly to the design, expansion and maintenance of large programs.

1.2 Object-oriented and process-oriented

Taking an example of buying a
image.png
mobile phone, if we use an object-oriented way to deal with the matter of buying a mobile phone, we don’t need to pay attention to the process of buying a mobile phone, how to buy a specific mobile phone, and how to get it. Just grab your phone, and these processes are done through interactions between objects .
⚠️: Process-oriented and object-oriented are not a language, but a method of solving problems, there is no good or bad

2. Definition and use of classes

Object-oriented programming focuses on objects

2.1 Simple knowledge of classes

**Class is used to describe an entity (object), ** mainly describes which attributes (appearance, function) and which functions (used for) of the entity (object), after the description is completed, the computer will can be identified. For example, the type of girl I like is: the gender is female, the height is greater than 0, and she can eat. This is a type, so I can instantiate a specific object through this class from God's perspective, which is on the heap. In real life, I find objects and create objects based on this class.
So in the Java language, how to define the above washing machine class?

2.2 Class Definition Format

image.png
The class keyword is used when defining a class in Java

//创建类
class ClassName{
    
    
    field;//字段(属性)或者成员变量
    method;//行为或者成员方法
}

class is the keyword that defines the class, ClassName is the name of the class, and the content in {} is the body of the class

Content contained in a class becomes a member of the class.
Content contained in a class becomes a member of the class. Attributes are mainly used to describe classes and are called class member attributes or class member variables. The method mainly describes what functions the class has and becomes a member method of the class

class MyGirlFriend{
    
    
    public String name;
    public String sex;//性别
    public int age;
    public int height;
    public String favouriteFood;
    public int hairLength;
    
    public void study(){
    
    
        System.out.println(this.name+"正在学习")}
}

The washing machine class is completed in the computer definition by using Java language, and .class is formed after javac compilation, which can be recognized by the computer on the basis of JVM.
⚠️Notes:

  • Note that the class name should be defined in big camel case
  • The writing method before the member is unified as public, and I will explain in detail why this is done later.
  • **The method written here does not have the static keyword.** It will be explained in detail later

⚠️Notes:

  1. Usually only one class is defined in a file
  2. The class where the main method is located generally needs to be modified with public ( Note : Eclipse will find the main method in the public modified class by default)
  3. The class modified by public must have the same name as the file
  4. Do not easily modify the name of the public-decorated class.

3. Instantiation of the class

3.1 What is instantiation?

Defining a class is equivalent to defining a new type in the computer, similar to int double, but int and double are built-in types that come with Java, and the class is a user-defined new type, such as MyGirlFriend , is a class (a newly defined type) with these custom types, you can use these classes to define instances (or objects).
image.png
Here we implement a date class

/**
 * @Author 12629
 * @Date 2022/3/25 19:43
 * @Description: 实现一个日期类
 */
public class MyDate {
    
    

    public int year;
    public int month;
    public int day;

    /**
     * 设置日期:
     */
    public void setDate(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);
    }

    public static void main(String[] args) {
    
    

        // this.printDate();

        MyDate myDate = new MyDate();

        myDate.setDate(2022,3,25);

        myDate.printDate();

        MyDate myDate2 = new MyDate();

        myDate2.setDate(2022,3,28);
        myDate2.printDate();

    }
}

⚠️Notes:

  • The new keyword is used to create an instance of an object
  • Use . to access properties and methods in objects
  • Multiple instances of the same class can be created

3.2 Key Notes

  1. A class is just a model -like thing that describes an entity and defines which members the class has.
  2. A class is a custom type
  3. A class can instantiate multiple objects. The instantiated objects occupy the actual physical space and store class member variables .

image.png

  1. Example: A class instantiating an object is like building a house using an architectural plan in reality, and a class is like a plan
  2. When a member variable is not assigned an initial value, each member variable is its corresponding 0 value, the reference type corresponds to null, bollean corresponds to false, and char corresponds to '\u0000', which is a space

Summarize

image.png
image.png
Hope it helps you,
thanks for reading~

Guess you like

Origin blog.csdn.net/qq_63511424/article/details/123796962