First, Java It's a completely object-oriented language.
1) Object-Oriented overview
class : A template that constructs objects, such as students can be defined as student classes, all teachers can be defined as teacher classes, all objects have a parent class, that is, object
Object: An object is an instance of a class that has properties and actions.
Constructors: (functions that are called when objects are created when the object is constructed, used to initialize the object)
1) The name is the same as the class name
2) Do not define the return value, there is no specific return value (can be someone trturn, representing the End function)
3) can have 0, 1 or more parameters
4) is called along with the new operator
For example, Zhang San is a student object whose attributes are: Name, age, Action: learning, eating
The following defines a student class and instantiates a student class, as follows:
About the "encapsulation" concept
Refers to the hidden object's properties and implementation details, only provide public access, the object's users hide the way the data is implemented, the key to implement encapsulation is that the methods in the class can not directly access other classes of the instance domain ( data in the object )
Benefits of Encapsulation:
1) Hide implementation Details and provide public access
2) improved reusability of the code
3) Improve safety.
Encapsulation principle : Hide content that does not need to be provided externally, hide attributes, and provide public methods to access them
Some methods of using in object-oriented
1) How do I use objects? Create the object and use the format: Class name Object name = new class name ();
2) How do I use member variables? Format: Object name. Variable Name
3) How to use the Member method? Format: Object name. Method Name (...)
The difference between a member variable and a local variable
1) different positions in the class
- Member variables: Outside the method in the class
- Local variables: In a method definition or on a method declaration
2) different locations in memory
- Member variables: In heap memory (member variables belong to object, object into heap memory)
- Local variables: In-stack memory (local variables belong to method, method into stack memory)
3) Different life cycle
- Member variables: As objects are created, they disappear as the object disappears
- Local variables: As the method is called, it disappears as the method's call is complete
4) different initialization values
- Member variable: has default initialization value
- Local variables: There is no default initialization value, which must be defined and assigned before it can be used.
Precautions:
1) The local variable name can be the same as the member variable name, when used in the method, using the nearest principle.
2) Basic data type variables include which: Byte,short,int,long,float,double,boolean,char
3) Reference data type variables include: arrays, classes, interfaces, enumerations
Java first Knowledge object-oriented 1