For the children who have not been involved in this field (for example, I), just start learning is very difficult, but once learned to feel "wow, originally I can be so 6", I do not ask myself to become more than 6, only hope it becomes my own another weapon, the protection of life is not compromised.
Above.
Structure of the Java class
1. Object-oriented
(1) Essence of "Object": Attribute + behavior
Eg: "Car" attribute: Form, brand, price, manual automatic file, etc. behavior: Start, stop, turn, reverse, etc.
(2) Class: A class is a collection of objects with common properties and behaviors;
You can instantiate multiple objects of this class, and each object has a different property value;
A class is the basic constituent unit of a Java program.
* The analysis process first has the object after the class, the development process has first class after the object.
2. Structure of the class:
Properties: Description of Object data;
Method: The behavior of the object (what can be done);
Construction method: Used to instantiate the object;
Inner class: (inner Class) is the class declared in the class body.
Blocks: Divided into static blocks, instance blocks
* Elements that are used frequently by properties, methods, and construction methods, while blocks, inner classes are used less.
3. Declaration form and function of class
(1) "Access modifier" "modifier" class Name {class Body}
There are two types of access modifiers: Public,default
Modifier: Final, Synchronized,abstract
Access rights modifiers and modifiers are optional.
(2) The role of a class: A class is a template that defines properties and methods common to multiple objects
4. Declaration form and function of attributes
(1) The definition of the attribute: the data of the object, also called the member variable;
(2) Declaration method: "Access modifier" "modifier" Data Type property name "= initial value";
private String name;
private double salary;
private static int count=0;
5. Declaration form and function of the method
(1) Method: That is, the behavior of the object, defined in the class with a specific function of a section of a separate applet, also known as a function.
(2) Access modifiers and modifiers: Access permission control method called range
Modifiers include static/final/abstract/synchronized
(3) Return value type: The data type of the returned result after the method is run, if no return value is used for void.
(4) Parameter type: The data type of the formal parameter.
Formal parameters: A variable that is used to store the actual arguments passed to the method when the method is called.
Return is used to end the method.
Return value: The result of the method's execution, which is returned to the caller.
(5) The characteristics of the method: the definition method can encapsulate the function code.
Facilitates the reuse of this feature.
method is executed only if it is called.
The appearance of the method increases the reusability of the code.
If the method does not have a return value, it is represented by the keyword void, then the return statement in the method can be omitted if the last line is not written.
Methods can be called in a method, and methods can not be defined inside a method.
When a method is defined, the result of the method should be returned to the caller and referred to the caller for processing
6. Method overloading:
(1) The composition of the method:
[Access modifier] [modifier] Returns a value type method name (formal parameter) {
Method body;
}
(2) Definition: In Java If there are multiple methods with the same name but different parameters become "method overloads"
Overloading of the/*add method */
int add (int x,int c) {...}
float Add (float x, int c) {...}
Double Add (double x,double x) {...}
Long Add (int x, int x,int x) {...}
(3) Features: Method name is the same
Different parameters (different numbers; different types; different order)
Same scope
* Method overloading does not have any relationship with the return value type of the method. That is, only methods that have different return values do not form overloads.
eg
public class Overloadtest {
Define a method to add a two integer
public void Add (int a,int b) {
int sum = a+b;
System.out.println ("The sum of two integers is:" + (a+b));
}
Define a method to add a three integer
public int Add (int a,int b,int c) {
return a+b+c;
}
Define a method to add two floating-point numbers
Protected double Add (double a,double b) {
return a+b;
}
Define a method to add an integer number and a decimal
void Add (int a,double b) {
Double sum = a+b;
System.out.println ("The sum of two integers is:" +sum);
}
7. Declaration form and function of construction method
(1) Function: Instantiate a class, that is, create an object
(2) Composition:
Access permission modifier class name (formal argument list) {method body}
public class Car {
private String color;
private double price;
Public Car () {
}
Public Car (String color) {
This.color=color;
}
Public Car (String color,double price) {
This.color=color;
This.price=price;
}
}
(3) Construction method
is called when the object is instantiated.
No return value, not even void
The method name must be the same as the class name
Modifiers cannot be used, including static, final, abstract
8. Construction method overloading: In real development, when initializing an object, different parameters are passed, so you need to define multiple construction methods in a class, that is, to construct method overloads.
Public Cars (String color) {
This.color = color;
}
Public Cars (String color,double price) {
This.color = color;
This.price = Price;
}
}
Java has abused me O (╥﹏╥) o