1. Understanding Object-oriented
1.1 Object-oriented programming (OOP)
Program = Object + Object + ...
Data first, algorithm second
Key: Get each object responsible for performing a set of related tasks
1.2. Object-oriented programming features
In the Java language, everything is object,
A program is a group of objects that send messages to each other
Each object has its own memory footprint and can be assembled into larger objects
Each object has a type, and all objects of a particular type can receive the same message
2. Concepts, distinctions and linkages of classes and objects
2.1. Concept
Class is the template and blueprint for creating objects
A class is a common abstract definition of a set of similar objects
2.2. Differences
Class is statically defined
object is a dynamic instance
Program code is manipulating objects, not classes.
Modeling gets tired rather than objects
2.3. Contact
Class is the definition of an object
The creation of objects is inseparable from the template of class
The purpose of a class exists is to instantiate the resulting object
2.4. Steps to define a class
Define class name
Writing a field for a class represents a property
Methods for writing classes represent behavior
2.5. The modeling of classes is an abstraction and encapsulation process
Abstraction: Removing unimportant, secondary information and preserving important information
Packaging: Information Packaging
3. Creating and Using objects
3.1.new--creating an object operator
When an object is created, allocates memory space, initializes a reference variable, calls a constructor method, returns a reference to an instance object
Example: Create a class first, create a student class, have 2 properties
public class student{
int age;
String name;
Public Student () {
I'm a non-parametric construction method,
No parameter construction method can be created, the system will be assigned by default
}
public static void Main (String [] args) {
Create a Student object
Student stu = new Student ();
Class name + variable name + Assignment (=) +new (open a memory space) + Call the parameterless constructor
}
}
3.2. Creating and Using objects
Use the member accessor "." Action Object
Assigning a value to an object
The object name. Property = value to be assigned
Calling methods
The name of the object. method ();
4. Method definition and invocation
4.1. The method is to complete a specific function and return the processing result
A method of a class represents a behavior (or function) of an instance
Method definition: Access modifier return value type method name (parameter list) {
Method body
Method Classification:
Method with no return value (void)
Methods with a specific return type
Methods with no parameters
A method with parameters
Method return value
If the method has a return value, the method must return the value with a key return, and the return value type is the return value type defined by the method
Note: Other methods can be called within a method.
4.2.java method invocation parameter passing using value passing mode
If the parameter is the base data type, the argument is passed with the value of the parameter to the method
If the argument is an object or an array, the argument is passed when the reference value of the object or array is passed to the method
In summary, the process of passing a parameter with a value copy
5. Scope of variables
The position of the variable declaration determines the scope of the variable
Variable scope determines the area of the variable that can be accessed by variable name in the program
Example: public class student{
Global variables, which can be used in this class
int age;
String name;
public void Eat () {
Local variables, which can only be used in this method
int num;
}
public void Show () {
Local variables, which can only be used in this method
String Address
}
}
Java Fundamentals (6) object-oriented programming