Java-simple understanding of classes and objects

1. Preliminary understanding of object-oriented

        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

                Take laundry at home as an example. Before the advent of automatic washing machines, clothes were washed by hand. You must be clear about each step and the next process. The laundry process is shown in the illustration

                 Pay attention to the laundry process, and there are many links in it. Different clothes have different washing methods, time lengths, and dehydration methods. Writing code in this way will make future expansion and maintenance more troublesome. After the advent of automatic washing machines, people no longer need to pay attention to the process. Just put the clothes into the washing machine, pour in the washing powder, adjust the mode (washing method, time length, dehydration method) and finally start it.

                 In the process of washing clothes, there are four objects in total: people, clothes, washing powder, and washing machine. The washing process: the person puts clothes into the washing machine, pours washing powder, starts the washing machine, and the washing machine can complete the washing process and dehydrate. The whole process is completed by the interaction between the four objects: people, clothes, washing powder, and washing machine. People don't need to care about how the washing machine washes clothes or dehydrates 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 distinction between good and bad, and there are special application scenarios .

 2. Class definition and use

        Object-oriented programming focuses on objects, and objects are real-life entities, such as washing machines. However, the washing machine computer does not know it, and the developer needs to tell the computer what a washing machine is, such as the brand, model, size, weight, washing power, dehydration power, washing capacity, dehydration capacity, washing mode, material, etc. of the washing machine. These describe the washing machine It is to abstract the washing machine object (entity) , but these simplified abstract results cannot be recognized by the computer. Developers can use some object-oriented programming language to describe, such as: Java language.

        2.1 Know the class

                A class is used to describe an entity (object), mainly describing which attributes (appearance, size, etc.) and functions (what to do) the entity (object) has. After the description is completed, the computer can recognize it.

                For example: washing machine is a brand, which can be regarded as a class

                Attributes: brand, model, weight, size, color, etc.

                Function: laundry, timing, dehydration, etc.

        2.2 Define the format of the class

                Classes are defined in java using the class keyword.

            //Create class

           class ClassName{

                  field; //property or field or member variable

                  method; //function or behavior or member method

           }

                For example: define the washing machine class

      class WashMechine{
             //Member variables
             public String brand;//brand
             public String type;//model
             public String color;//color
             public double length;//长
             public double width;//宽
             public double height;//高
             //method
             public void Washclothes(){
                 system.out.println("Laundry function");
             }
             public void  Dryclothes(){
                 System.out.println("Dehydration function");
             }
             public  void Settime(){
                 System.out.println("timing function");
             }
        }

                Note: Class names are defined in camel case, members are preceded by public, and methods do not contain the static keyword .

        2.3 Exercises

                1. Define a dog class

               class Doge {   

                        //property or member variable    

                        public String name;//name    

                        public String color;//color

                        //method

                        //cry

                        public void barks(){

                                System.out.println(name+": woof woof~~~");

                        }

                        //Wag the tail

                        public void wag(){

                                System.out.println(name+":Wag tail~~~");

                        }

                   2. Define a student class

        

        class  Students{
             //property or member variable    

            public String id;//Student number
            public String name;//name
            public String sex;//gender

            public int age;//age
            public String address;//Address
            //method

            //Attend class
            public void Doclass(){
                System.out.println("Attend to class on time, don't be late, if you ask for leave, please make up the class in time");
            }
            //write homework
            public void Dohomework(){
                System.out.println("Check and complete the homework in the educational affairs system, you must write");
            }
            //take an exam
            public void Exam(){
                System.out.println("Examination subjects, time, place, seat number");
            }
        }

                Note: Generally, only one class is defined in a file; the class where the main method is located generally needs to be modified by public, and Eclipse will find the main method in the class modified by public by default; the class modified by public must have the same name as the file; do not modify it easily The name of the public modified class, if you want to modify it, modify it through the development tool.

Third, the instantiation of the class

        3.1 What is instantiation of a class

                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: Doge class and Students class. They are all classes (a newly defined type), and with these custom types, these classes can be used to define instances (objects).
                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.

           Instantiate the class in main.

 public class Doge {
    public String name;
    public String color;
    public void barks(){
        System.out.println(name+": woof woof~~~");
    }
    public void wag(){
        System.out.println(name+":Wag tail~~~");
    }
    public static void main(String[] args) {
        Doge doge1=new Doge();//Use new to create instantiated objects for classes
        doge1.name="A Huang";
        doge1.color="black yellow";
        doge1.barks();
        doge1.wag();
        System.out.println("==========");
        Doge doge2=new Doge();
        doge2.name="Saihu";
        doge2.color="brown yellow";
        doge2.barks();
        doge2. wag();
    }
}

                 Note: The new keyword is used to create an instance of an object; use "." to access the properties and methods of the object;; the same class can create multiple instances .

        3.2 Description of classes and objects

                A class is just a model, which is used to describe an entity and defines which members the class has; a class is a custom type that can be used to define variables ; a class can instantiate multiple objects, instantiate The created object occupies the actual physical space and stores class member variables ; as an analogy: instantiating an object from a class is like building a house using architectural design drawings in reality, and a class is like a design drawing, only designing what is needed, but There is no physical building, and the same class is just a design. Only the instantiated objects can actually store data and occupy physical space.

 Four, this reference

        4.1 Why is there a this reference

                example

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) {                 // construct three date type objects d1 d2 d3                 Date d1 = new Date();                 Date d2 = new Date();                 Date d3 = new Date();                 // Yes d1, d2, d3 date setting                 d1.setDay(2019,3,22);                 d2.setDay(2020,3,22);






                d3.setDay(2021,3,22);
                // Print the contents of the date
                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. 1. If the
                formal parameter name is accidentally the same as the member variable name

            public void setDay(int year, int month, int day){
                        year = year;
                        month = month;
                        day = day;
              }

                Who is assigning values ​​to whom in the function body at this time? Is member variable to member variable? Or member variables to parameters? Or is it a parameter to a member variable? , who assigns the value to whom, I don't know.

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

                 This reference is involved here.

        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);
                }
        }

        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.
 

Guess you like

Origin blog.csdn.net/qq_64668629/article/details/132006191