classes and objects
Classes and objects need to be understood slowly and step by step, which is a very abstract concept
[Objective of this program]
-
Master the definition of classes and the instantiation of objects
-
Master the use of member variables and member methods in classes
-
Master the entire initialization process of the object
-
Master Package Characteristics
-
Mastering Code Blocks
-
mastering inner classes
Everything in Java is an object.
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. noodle
Object-oriented is a kind of thinking for solving problems, which mainly relies on the interaction between objects to complete one thing. Using object-oriented thinking to deal with programs is more in line with human
Our understanding of things is very friendly to the design, expansion and maintenance of large programs.
1.2 Object-oriented and process-oriented
example one
- process-oriented laundry
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're going to wash your shoes in the future, that's another way.
Writing code in this way will be more troublesome for future expansion or maintenance.
- object-oriented laundry
Process in an object-oriented manner, do not pay attention to the process of washing clothes, how the specific washing machine washes clothes, how to dry them, the user does not need to close
Heart, you only need to put the clothes into the washing machine, pour in the washing powder, and turn on the switch, which 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.
Example two
2. Class definition and use
Object-oriented programming focuses on objects, and objects are real-life entities, such as washing machines. But the washing machine computer doesn't know it, and the developer needs to tell the computer what a washing machine is.
The left side of the picture above is a simple description of the washing machine. This process is called abstracting the washing machine object (entity) (re-cognition of a complex thing), but these simplified abstract results cannot be recognized by the computer. Developers can use some An object-oriented programming language for description, such as: Java language.
2.1 Simple understanding class
As we said before, a class can be understood as a structure (even I think it is an advanced version of the structure. In C language, a structure is a collection of data variables, so I now think of a class as a collection of variables + methods)
Like a washing machine, a secretary, and a mobile phone, these are all objects, and an object is a complex collection.
2.2 Class definition format
The class keyword needs to be used when defining a class in java. The specific syntax is as follows
// create class class ClassName{ field; // field (property) or member variable method; // behavior or member method }
class is the keyword to define the class, ClassName is the name of the class, the class name is named in big camel case, and {} is the body 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.
define a person object
class Person { public String name; public int age; public void eat() { System.out.println("Have a meal"); } public void sleep() { System.out.println("sleep"); } }
define a washing machine
class WashMachine { public String brand; // brand public String type; // model public double weight;// weight public double length; // long public double width; // Width public double height; // high public String color; // color public void washClothes(){ // do the laundry System.out.println("laundry function"); } public void dryClothes(){ // dehydration System.out.println("dehydration function"); } public void setTime(){ // timing System.out.println("Timing function"); } }
Classes are defined in the computer using the Java language, and a .class file is formed after javac compilation, which can be recognized by the computer on the basis of the JVM.
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
In the project project, only one class is written in a Java file
2.3 Classroom exercises
define a dog class
class PetDog { public String name;//name public String color;//color // dog attributes public void barks() { System.out.println(name + ": want want want~~~"); } // dog behavior public void wag() { System.out.println(name + ": wagging tail~~~"); } }
Precautions:
-
Generally, only one class is defined in a file.
-
The class where the main method is located generally needs to be modified by public (Note: Eclipse will find the main method in the class modified by public by default)
-
The public modified class must be the same as the file name
-
Do not easily modify the name of the public modified class. If you want to modify it, modify it through the development tool
When there is only one class in a file, the class name will be automatically modified, but if there are too many classes, the class name must be changed to the renamed file name.
- There can only be one public modified class in a Java file
To get an object, you must first define the class. After the class is defined, the process of converting the class into an object is called the instantiation of the class.
A class is a user-defined type.
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. They are all classes (a newly defined type). With these custom types, these classes can be used to define instances (or 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.
Instantiation method
//Instantiate a Person object through the keyword new Person per = new Person();
public static void main(String[] args) { //Instantiate a Person object Person person = new Person(); person.name = "lisi"; person.age = 40; System.out.println(person.name +" "+person.age); } //------------- //The result of running the compiler is //lisi 40
public class Main{ public static void main(String[] args) { PetDog dogh = new PetDog(); //Instantiate an object via new dogh.name = "A Huang"; dogh.color = "black and yellow"; dogh.barks(); dogh.wag(); PetDog dogs = new PetDog(); dogs.name = "A Huang"; dogs.color = "black and yellow"; dogs.barks(); dogs.wag(); } } //--------------- //The result of the operation is //Wangwangwang~~~ //tail wagging~~~ //Wangwangwang~~~ //tail wagging~~~
Precautions
- 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 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 and methods.
-
A class can instantiate multiple objects, and the instantiated objects occupy actual physical space and store class member variables
-
The class is just a design and does not take up physical space. Only the instantiated objects can actually store data and occupy physical space
The memory usage of class instantiation:
Person per = new Person();
First of all, per is a reference class variable, which stores the address and stores it on the stack.
The class contains member variables and member methods, member variables are stored in the heap area, and member methods are in another area of memory (method area), but only when the method is called will it occupy memory
Replenish
If the sleep function is called before the name is defined, then the print out is null sleep.
Today we will first talk about the definition of classes and the instantiation of objects. Because there are many classes and objects, we will explain the content in three parts.