Object-oriented concept-class
VA class is the "Basic prototype" that describes an object. It defines the data and operations that an object can perform. In object-oriented programming, a class is the basic unit of a program.
VSimilar objects can be merged into the same class, just like variables and types in traditional languages.
VAn object in a program is an instance of a class and a software unit. It consists of a group of structured data and a group of operations on it.
Object-oriented concept
Objects are described by an abstract data type in a program. This abstract data type is called a class ).
Relationship between classes and objects
VClass is the core and essence of Java. It is the basis of the Java language, because Classes define the essence of objects.
VClass defines a new data type. You can use a new type to create an object of this type.
VA class is a template of an object, and an object is an instance of the class ).
Class composition
VVariable: the State of the object.
VMethod: refers to the functional unit of an object.
VMessage:
Software Objects interact and communicate with each other by passing messages. A message consists of three parts:
- Message recipient
- Method to be taken for the recipient
- Parameters required by the Method
Class Definition syntax Specification
Strict Class Definition and Modifier
[Class modifier]Class Name[Extends parent class name] [implements Interface Name List]
{
Variable definition and initialization;
Method definition and method body;
}
Class modifier:[Public] [Abstract | Final]
The default mode is friendly.
Class Definition
VThe class consists of two parts: the class declaration part "Class class name" and the class body "{… }";
§ Example: class person {
// Class body content
}
VNote:
§ Lower case for the keyword class
§ Class names must comply with Java identifiers; good programming specifications generally use upper-case letters of class names; Class names must be meaningful (see the name recognition)
VDefinition of attributes (variables) and methods (functions) in the class body
Syntax specification for variable definition
VDefinition and modifier of Variables
[Variable modifier] variable data type variable name 1, variable name 2 [= initial variable value]…;
[Public | protected | private] [static] [Final] [transient] [volatile]
VThe type of member variables can be any data type in Java, including simple types, classes, interfaces, and arrays.
Variable Classification Definition
VThere are three types of variables in the class body: instance member variables, class (static) member variables, and local variables;
The first letter of the variable is generally lowercase;
VClass (static) member variables (class variables or static variables)
V class person {
StaticString classer;
}
VLocal variable
V class person {
Public void speak (){
String name;
}
}
Variable Scope
VScope determines the range of variables that can be used.
§ Global variables: variables can be accessed throughout the class;
§ Local variables: variables can only be accessed in the code segment that defines them.
VScope rules: variables defined in a code segment can only be visible in the code segment or the child code segment of the code segment.
VUsing local variables is safer than using global variables.
Variable Initialization
If no initial value is assigned to a member variable, it is automatically initialized.
Data Type |
Initial Value |
Boolean |
False |
Byte/short/INT/long |
0 |
Float |
0.0f |
Double |
0.0d |
Char |
'\ U000000' |
Object Type |
Null |
The initial value must be assigned to a local variable. Otherwise, an error will be reported at the referenced location.
Syntax specification of method definition
Method definition and Modifier
[Method modifier] return type method name (parameter 1, parameter 2 ,...) [Throws exceptionlist]
{
... (Statements;) // method body: content of the Method
}
[Public | protected | private] [static] [final | Abstract] [Native] [synchronized]
The return type can be any Java data type. If a method does not require a return value, the return type is void. The parameter type can be a simple data type, or a reference data type (array, class, or interface). The parameter transmission mode: simple data type is a value transfer, and the object is a reference transfer.
Method Classification
VScore by return value:
§ Return Value
§ No Return Value
VBy parameter:
§ No Parameter
§ Parameters (single parameter and multiple parameters)
VBy range or function:
§ Instance method;
§ Class methods;
§ Constructor
Method call
VWhen defining a method, the variables defined in the brackets in the method declaration section are form parameters (I .e. form parameters ); when calling a method, you need to input the corresponding actual parameters (real parameters) based on the method parameters ).
VMethod call
Instance method
VMethods In the class body are divided into: instance Member method; Class (static) member method; constructor method; the first letter of the method is generally lowercase.
VInstance Member method (instance method)
Class Method
Class (static) member method referred to as class method or static method
VConstructor. The constructor name is the same as the class name, And the constructor does not need the void keyword if there is no return value.
Constructor
VConstructor is a special method. Every class in Java has a constructor to initialize a new object of the class. The constructor has the same name as the class name and does not return any data type. The system will automatically execute when an object is generated.
VConstructor content:
Define some initial values or memory configuration work;
§ A class can have multiple constructor methods (overload) and decide which one to execute based on different parameters;
§ If no constructor is defined in the program, the default constructor is used during instance creation. It is a blank method without content.
This keyword
This indicates your reference, that is, the object of the current method. One of its main functions is to send its own object as a parameter to other methods in the object.
Method overload)
Method overloading means that multiple methods in the same class can have the same name. However, the parameters of these methods must be different (the number of parameters is different or the parameter type is different ).
Notes
VClass definition process notes:
§ Only the definition of variables and methods are included in the class.
§ The instance method can access any member variables and any member methods in this class. The class method can only access the class member variables and class member methods in this class;
§ In the class, the variable definition is placed before the class, and the method definition is placed behind the class; however, the variable definition method definition order is random;
Key points:
1. The standardization of mandatory editing every day;
(Class Name, method name, and variable name) Case sensitivity requirements, narrowing down;
In particular, use meaningful names;
(Chinese pinyin abbreviations are not supported)
2. Class Definition
Variable score: instance, class, local variable;
Method score: (difficult)
Score by return value:
There are return values, no return values;
By parameter: (simple description of the concept of participation in real parameters)
Parameter (single, multiple );
No Parameter
By range or function:
Instance method; Class method; constructor Method
3. Description value transfer; (Reference transfer next lesson)
========================================================== ======================================
The main task of developers is to first build an object model for simulating the problem domain, and then implement the object model through program code.
The problem field refers to the real-world system simulated by software systems.
The main task of object-oriented programming is to define various classes in the object model.
The object is a class instance.
Class is the object template.
The complex functions of the software system are completed by the collaboration of various objects.
1: What is a class?
Objects in real life can be abstracted. This abstract data type is called a class.
Is the abstraction of a group of objects with the same attributes and behaviors.
For example, animal (animal) fruit (fruit)
2: What is an object?
In Java, all people, things, or modules are an object.
1. Everything is an object 2. attributes and actions 3. Status 4. uniqueness 5. belong to a class
In an instant, the value of an object's property changes the object's state.
The same object has some same features.
Dog, cat, and Snake (animal object)
Apple, pear, and orange (fruit object)
3: Relationship between classes and objects
A class is an object template (abstract representation), and an object is an instantiation of A Class (concrete presentation)
4: Composition Structure of the class
Java is purely object-oriented (except for eight basic data types)
Objects are generated from classes. Therefore, classes are the most basic and core elements of Java programs.
Variable (attribute, member variable)
Method (function, member method, responsibility)
Message (communication between objects and transmission between object data)
5. Class Definition and Specification
6. Attribute definition and Specification
Attribute Definition
Attribute Initial Value (default)
= Variable Classification =
Class variable: The Class Object belongs to the object. Static modified variables are called class variables. Static zone. It is instantiated only once, so it occupies only one memory.
Object member variable: The member variable belongs to the object. Located in the heap area.
Local variable. The variables defined in the local method and the formal parameters are all local variables. Located in the stack area.
= Variable scope (lifecycle) =
The time when different types of variables survive in the memory.
7. Classification of methods:
= Divided by return values: =
Return Value
No return value
= By parameter: =
Parameter Method
No Parameter Method
Voidsum (int x, int y): between parameter definitions, separated by commas.
Sum (x, y); // calls are also separated by commas.
Real parameters (actual parameters)
Parameters ).
When passing parameters, there are two ways to pass the real parameters to the form parameters:
A: if the transfer is a basic data type, it is just a copy of the value.
B: If the object type is passed, the object address is passed.
= By function method: =
1. Constructor
2. instance method (Object method and member method)
3 methods
Constructor:
Is the operation that must be performed on the instantiated object.
=== Used to construct the initialization operation for a new object.
If no constructor is created, the system automatically creates an empty constructor.
If there is a constructor, the system will not build any more.
1. The method name must be the same as the class name
2. Do not declare the return type
3. Static, final, and abstract cannot be modified.
Overload construction method:
Objects may have different initialization behaviors under different conditions. Employee
Default constructor:
1. Implicit default constructor 2. The default constructor explicitly defines by the program. There must be at least one constructor with no parameter, public, and null.
2. If one or more constructor methods are defined when the class is displayed with parameters, the default constructor is lost.
========================================================== ========================================
This:
Indicates the current object.
This indicates your reference.
You can use this to call the attributes and methods of the current object.
Method overload)
Method overloading means that multiple methods in the same class can have the same name.
However, the parameters of these methods must be different (the number of parameters is different or the parameter type is different ).
During the call, the compiler will use the number or type of parameters to find which one to call.
Method.
Class methods, object methods, and constructor methods can be overloaded.
If the overloaded parameters have an inheritance relationship. Unless it is precisely described in the program
Transfer Type. Otherwise, all methods that comply with the subclass parameters are called.
Object OBJ = NULL;
String STR = NULL;
Test. Print (OBJ); // exact type
Test. Print (STR); // exact type
Test. Print (null); // all matching types. Call the subclass method.
Public void print (Object some ){
System. Out. println ("objectversion print ");
}
Public void print (string some ){
System. Out. println ("stringversion print ");
}