Method Definition: A method is a piece of code that can be called repeatedly
Format:
The access modifier returns a value type (which can be void, base data type, reference data type) method name () {
Method body
}
Note: The void return value type does not require a return value, and other types require a return value.
Overloaded method: The method name is the same, but the type and number of parameters are different, and the operation of different functions is accomplished by passing the number and type of parameters.
Class and object relationships: A class is a description of a thing, an abstraction, a conceptual meaning, an object that is actually present in every individual of that kind of thing, also known as an instance
Declaration format for the class:
Class name
{
Property
Method
}
Class Name Object name =null//declaration
Object name =new class name ()//instantiation operation
Class Name Object name =new class name//declaration and instantiation
Memory Partitioning:
Class Name Object name: It opens up a space in the stack memory
Object name points to the corresponding heap memory space
New class name (): Heap memory space opened with new
The properties in the open heap memory are the default values, for example:
The default value of String is null int is 0 and so on;
The value in the heap memory changes after the assignment of the property
In the absence of privatization properties and methods, you can use:
Object. Properties//Assignment
Object. Method//Call
Note: null pointer exceptions occur if the new class name () operation is not performed:
Exception in Thread "mans" Java.lang.NullPointerExcetion
The object-oriented basis is understood:
Program development: Process-oriented, object-oriented
What is process-oriented and object-oriented: for example, to make a box, the process is: do not want to do what kind of box, random tool to do. Object oriented is: First think about what kind of box to do, looking for the corresponding tool to do
Three main features of object-oriented:
1. Encapsulation
Not visible to external
For example, TV: television has a shell, and the role of the shell is to protect the internal parts of the TV, and television is usually left with a button to facilitate people to use, and the program will leave some interface for us to use.
2. Inheritance
Features of the extended class
For example: Your father has 1000W when your father is old, he has only you a son, his wealth accumulation process is over, and you inherit your father's 1000W, your wealth accumulation will start from 1000W accumulation and will not appear the phenomenon of loss
3. Polymorphism
Overloading of methods
The polymorphism of the object
Recursive invocation of the method:
1. Method recursive invocation is a special form of invocation, which means that the method calls itself
Recursive calls → methods (parameters)
N↓y (exit of recursive call)
Execute statement ← ——— meet end condition ——— → return to call
The code is as follows:
public class demo{
public static void Main (string[] args) {
System.out.println (Addnum (100));
}
public static int Addnum (int n) {
if (addnum==1) {//For this program Addnum==1 is the program exit
return 1;
}else{
Return N+addnum (n-1)
}
}
Encapsulation:
Protects certain properties and methods of the current class from being seen by external classes
The implementation of encapsulation:
Encapsulation of properties and methods is declared by the keyword private
Implements the set and get methods for this property, which are accessed externally
Anonymous objects:
An anonymous object is an object that does not have a name, and if the object is used only once in the program, the anonymous object can be used
Format: New class name. Method Name (note: An object that is only suitable for use once)
Construction Method:
Format:
Access modifier class name () {
Program statements
}
Note: 1. The constructor method name is the same as the class name
2. Construction method does not return a value type
The constructor method is primarily for property class
The constructor method is called after each class instantiation, and if there is no constructor, the program creates a non-parametric construction method that does nothing at compile time.
Overloading of constructor methods:
Method names are the same, but the type and number of parameters are different, and the operation of different functions is accomplished by passing the number and type of parameters.
Reference delivery:
This keyword:
1. Representing properties and calling methods in a class
this. Property name (the property in the current Class) this. Method name (method of the current class)
2. Call the constructor method in this class
This () (call the parameterless construction method, note that this () must be placed in the first row of the constructor method)
3. Represents the current object
Inherited:
Basic concepts of inheritance: extending the functionality of a parent class
For example: Your father has 1000W when your father is old, he has only you a son, his wealth accumulation process is over, and you inherit your father's 1000W, your wealth accumulation will start from 1000W accumulation and will not appear the phenomenon of loss
Using the extends keyword to complete inheritance in Java
Class Subclass extends parent class {}
Inheritance restrictions in Java:
1. Only single inheritance allowed
2. Subclasses cannot directly access private members of the parent class if they want to call only through get and set
Instantiation of a subclass object:
1. The subclass object must be instantiated before the constructor method in the parent class is called, and then the constructor of the child class is invoked
↑→ first call the parent class's construction method → Call the parent class construct
Instantiating a subclass object
↓→ in calling subclass construction method → Call subclass construct
Method overrides and Super keywords in Java:
1. In succession, there is also the notion of rewriting, in fact, that a method with the same name in the parent class is defined in the subclass
2. Definition:
Same method name, same return value type, same parameter
3. Override Restrictions:
A method overridden by a quilt class cannot have more restrictive access than the parent class method
4. Access rights:
Private<default<public where default is
Super Keyword:
Super Force calls methods in parent class
The difference between overrides in Java and overloading:
overloaded (overloading) Definition: The method name is the same, the type or number of arguments is different for the permission, the scope is in the same class
overriding (overriding) Definitions: method names are the same, return value types are the same, parameters are of the same type,
The overridden method cannot have more restrictive access than the parent class method, which is scoped to the inherited class
Abstract classes and Interfaces
Use of the final keyword
Final is called the end in Java, which means the final meaning
Final can declare classes, methods, properties
Classes that use Final declaration cannot be inherited
Methods that use Final declaration cannot be overridden
Variables declared with final are called constants and cannot be modified
Abstract class
1. Abstract Class Concepts:
Classes that package classes of abstract methods are abstract classes
2. Abstract Method:
Methods that are declared without being implemented, the abstract method must use the abstract keyword to declare
3. Abstract class Quilt inheritance, subclasses (if not abstract classes) must override all abstract methods in the abstract class
4. Define the Format abstract class ClassName () {
Property
Method
Abstract methods
}
5. Abstract classes cannot be instantiated directly, but are instantiated by their subclasses
Some basic understanding of JAVA object-oriented