Basics of Java classes and objects

JAVA object-oriented foundation-classes and objects

1. The basic concepts of classes and objects
1. What is a class? What is an object?
Class: is the abstract
object of the object: is the instance of the class

For example: men and women are classes; XX's clothes, XX's hands represent objects.
2. What are the components of the class?
In life: When we have a class, we must define clear characteristics and characteristics, and behaviors for this class.
In the program: when there are classes in the program, there are also characteristics and methods, but the name is changed, called attributes and methods.
Second, the creation of classes and objects
1. Create a
class keyword to define the class
Class class name { //members in the class } and create a property in it, so that you can call later 2. Create an object Use the keyword new to create an object ① Class name object name; Person tan object name = new class name (); Tan = new the Person (); ② class name object name = new class name (); Person Tan = new the Person (); object access class attributes and assignment III The members of the class (encapsulation) 1. Attribute: the common characteristic feature is the variable declared directly in the class 2. Method: the common behavior is the method declared in the class 3. Construction method: the main purpose is to initialize the class Attributes and characteristics: ① Same name as the class ② No return value and cannot write Void ③ Automatically called when the object is created Note: Person() is actually a method named after the class, which exists by default at the beginning of the class, and this method is the only one The difference is that there is no return parameter, this method is called "construction method"


















The role of the construction method is to create an object, the purpose is to initialize. (Person represents the class name)
Fourth, the overload
of the construction method 1, the construction method
①, the construction method without parameters.
public class name (){ } ②, a parameterized construction method. public class name (parameter) { this.name=name; //Assign the name value to the name attribute. } (this represents the current class, the first name represents the name attribute, and the second name represents the value) 2. There can be multiple methods in a class with the same method name but different parameter lists. This is called method overloading. 3. When there is no construction method in the class, jvm will automatically create a construction method without parameters; but if there is a construction method with parameters in the class, jvm will not go to the construction method of the constructor parameter. (Jvm Java virtual machine) Fifth, the This keyword refers to the object of the current class. That is to say, which object calls the method, then this in this method refers to the usage of that object This ① this. is used to call the members of the current object (methods or properties can be used) ② this() is used to call the current object Other construction methods (can only be written in the construction method, can only be written in the first line) Six, static keyword 1, if a member is declared as static, it can be accessed before any object of its class is created , Without having to refer to any object 2, the most common example of a static member is main(), because main() must be called when the program starts to execute, so it is declared as static















3. There can be no non-static methods inside static methods, and static methods can be used in non-static methods.
4, static used in front of the class modifier represents the class as a static class, cannot be instantiated
static used in front of the variable to represent the variable as static, you must use the class name. Variables can be used to access variables but not instantiated objects to access
5, static is used in methods The former representative method is static, and must use the class name. The method name can be accessed, and the instantiated object cannot be used to access.
6. The declaration as static has the following restrictions:
they can only call other static methods.
They can only access static data.
They cannot refer to this or super in any way.
Note: If a method is defined as static, it can be called by class name.method name().
Seven, mind map
Insert picture description here

Guess you like

Origin blog.csdn.net/tan1024/article/details/110008187