Catalog
1.3 Three main object-oriented features:
1.4 The saying that everything is an object when facing an object
1.5 Each object-oriented overall process takes three steps
1.6 Classes and Objects Their Relationships
Three Call Methods for 2.2.3 Constructor
2.2.4 Explanation of the existence of parametric and parametric constructors
Purpose of the 2.2.5 constructor:
2.27 Modifiers that can change variable permissions
1 Overview
1.1 Process Oriented (OPO)
Emphasize what to do, emphasizing functional behavior
1.2 Object Oriented (OOP)
It is an idea that emphasizes the way of thinking that human beings use in their daily logical thinking, such as abstraction, separation, inheritance, aggregation, polymorphism, etc.
1.3 Three main object-oriented features:
-
encapsulation
-
inherit
-
polymorphic
1.4 The saying that everything is an object when facing an object
Because in Java, we can encapsulate functions, structures, and so on into classes, and implement specific functions by instantiating classes. It involves the interaction between front-end web page data and back-end database. When interacting, they all have corresponding classes and objects, which correspond to events in real life one by one.
1.5 Each object-oriented overall process takes three steps
-
Create Class
-
Class instantiation, that is, creating objects
-
Call class method properties for operation
1.6 Classes and Objects Their Relationships
Classes are abstractions of things that have a series of common properties in life
A class is a collection of objects with common attributes and behaviors
The relationship between a class and an object is a high-level summary of the common characteristics of the object, which is a concrete and real entity.
That is, the class we often compare is a template, a drawing, and an object is a made entity.
1.7 Instance Description
Our behavioral activities in life can be divided into one person (someone) or something (bicycle), what attributes are there, what is done
Programs are designed to simulate the human world and help people deal with things, such as
1. Xiao Ming goes out by bike
real life | Program |
---|---|
One person, transportation | Class name: Human, Transportation |
Xiao Ming, Bicycle | Object: Xiao Ming, Bike |
Cycling out to play | Method: Go out and play |
Xiao Ming, 2 or 12, goes to fly kites
real life | Program |
---|---|
People, toys (kites can also be used, Xiao Ming's kite belongs to one of the kites) | Class name: human, toy (kites can also be used, Xiaoming's kite belongs to one of the kites) |
12 years old | Xiao Ming's attributes: 12 years old |
Xiao Ming, Kite | Object: Xiao Ming, Kite |
Fly a kite | Method: Fly a kite |
Composition of 2 classes
2.1 Attributes:
2.1.1 Meaning
Member variables, whether or not the initial value is OK, are defined outside the methods in the class.
2.1.2 Writing Format
//Variable Type Variable Name String name;
2.2 Behavior:
2.2.1 Meaning
Includes construction methods and custom methods, which in general must be written and will not error if not written, since Virtual Opportunities provide a parameterless construction method by default.
2.2.2 Writing Format
//Parameterless constructor public Class name(){} //No void, no formal parameters //Parametric constructor public Class name(Variable Type Variable Name){ //Like parameterless constructors, there can be no void, and there can be multiple formal parameters. Arguments = Formal parameters; } //A parameterless constructor and a parameterized constructor constitute an overload //Custom methods public void method(){ //Method name and return value, return value type depends on you }
Three Call Methods for 2.2.3 Constructor
// Method 1, also known as anonymous call; new Animal("Lin Qingxia").show(); // Method 2. Normally call a class without parameters, create an object, and set member variables in the class through the setter method. Animal an = new Animal(); an.setName("Lin Qingxia"); an.show(); // Method 3. Create objects by calling classes with parameters and assign values directly. Animal an1 = new Animal("Lin Qingxia"); an1.show();
2.2.4 Explanation of the existence of parametric and parametric constructors
-
There is no constructor in the class, and the virtual machine will automatically provide a parameterless constructor to use
-
A parameterized constructor is provided in the class, and no parameterized constructor is automatically provided by the system
Purpose of the 2.2.5 constructor:
-
Class initialization is done by calling the constructor, that is, when the class is initialized, the code in the constructor executes.
-
Create Object with
2.2.6 Note:
-
A parameter cannot be added to a parameterless constructor.
-
The name of the constructor and the class name should be highly consistent or an error will be invalidated.
-
public can be removed, but it is not recommended. Once removed, it can only be called in this package. There will be an explanation in the permission modifier later
-
The construction method name here must be the same as the class name, and different words will be considered writing methods, so it is important to determine whether void has no parameter return value or a certain type of parameter return value.
2.27 Modifiers that can change variable permissions
-
After final modification, the variable becomes a constant, which cannot be modified elsewhere and is written in full capitals
-
After the static modification, the variable becomes a common property, which is accessed by the variable for each object in the class.
-
The this keyword is used inside a method, and when local and member variables are repeated, the this modifier is a member variable
public static void main(String[] args) { StaticTest.setSchool("Henan University"); StaticTest test = new StaticTest("Zhang San"); StaticTest test2 = new StaticTest("Li Si"); System.out.println(test.getName()+"..."+ StaticTest.getSchool()); System.out.println(test2.getName()+"..."+ StaticTest.getSchool()); //The output is Zhang San...Henan University // Li Si...Henan University } public class StaticTest { private static String school; //school is decorated with static, and the get method for each object yields the same result school results before this change private String name; public StaticTest() { } public StaticTest(String name) { this.name = name; } public static String getSchool() { return school; } public static void setSchool(String school) { StaticTest.school = school; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
3 A standard java bean class
public class animal{ } //***This constitutes a class, but generally it is not, because there is no practical point in changing it to an abstract class****** class Animal { private String name; public Animal() { //Parameterless constructor } public Animal(String name) { //Parametric constructor this.name = name; } public void show() { //Defined Method System.out.println(name + "Eureka"); } public String getName() { //The getter method provided to make it easy to call variables in this class elsewhere return name; } public void setName(String name) { //Provided setter method to facilitate setting variables in other classes this.name = name; } }