Study notes: Java object foundation

Object-oriented and process-oriented

Object-oriented (OOP) and process-oriented (POP)
are both a kind of thought, and object-oriented is relative to process-oriented. Process-oriented, emphasizing functional behavior. Object-oriented, encapsulating functions into objects, emphasizing objects with functions.

Object-oriented emphasizes the use of human thinking methods and principles in daily thinking logic, such as abstraction, classification, inheritance, aggregation, polymorphism, etc.

Three characteristics of object-oriented

Encapsulation

Inheritance

Polymorphism
Insert picture description hereInsert picture description here

One of the members of the class: attributes

Syntax format:

Modifier type attribute name = initial value ;

Description modifier private: This attribute can only be accessed by methods of this class.

​ Modifier public: This attribute can be accessed by methods other than this class.

​ Type: any basic type, such as int, boolean or any class.

For example:

public class Person{

​ private int age; //Declare private variable age

​ public String name = "Lila"; //Declare public variable name

}
Insert picture description here
Member variables:

Member variables are defined in the class and can be accessed throughout the class.

Member variables are divided into class member variables and instance member variables, and instance variables exist in the heap memory where the object is located.

Member variables have default initialization values.

The permission modifier of member variables can be selected according to needs

Local variables:

Local variables are only defined in the local scope, such as: methods, code blocks, etc.

Local variables exist in the stack memory.

When the scope of action ends, the variable space will be automatically released.

Local variables have no default initialization value and must be explicitly initialized each time.

Do not specify permission modifier in local variable declaration

Class member two: method

Modifier return value type method name ( parameter list ) {

Method body statement;

}
What is a method (function)?

A method is an abstraction of the behavior characteristics of a class or object, also called a function.

Methods in Java cannot exist independently, all methods must be defined in the class.

Modifier return value type method name (parameter type parameter 1 , parameter type parameter 2 , …. ) {

code

return return value ;

among them:

Formal parameters: Variables used to receive external data when the method is called.

Parameter type: it is the data type of the formal parameter.

Return value: The data that the method returns to the program that called it after execution.

Return value type: The data type of the result to be returned by the method.

Actual parameters: The data actually passed to the function formal parameters when the method is called.

How to understand the situation that the return value type of the method is void ?
Void: empty means, it cannot be used to define variables, it can only be used as the return value type

important point:

  1. Cannot define variables to receive
  2. Can not be placed directly in the output statement

Object

When an object is created, various types of member variables are automatically initialized and assigned. In addition to the basic data types, variable types are reference types, such as the Person above and the array mentioned above.

Member variable type Initial value
byte 0
short 0
int 0
long 0L
float 0.0F
double 0.0D
char '\u0000' (represents empty)
boolean false
Reference type null

The Java permission modifiers public, protected, and private are placed before the definition of the members of the class to limit the access rights of the object to the members of the class.

Modifier Inside the class Same package Subclass All regions
private Yes
(default) Yes Yes
protected Yes Yes Yes
public Yes Yes Yes Yes

Class member three: constructor ( construction method )

如:Order o = new Order(); Person p = new Person(Peter,15);

Just as we stipulate that every "person" must take a bath at birth, we can add the program code to complete the "bath" in the construction method of "person", so that every "person" will automatically complete the "bath" as soon as he is born. , The program no longer has to tell everyone to "bath" one by one when they are just born.

Guess you like

Origin blog.csdn.net/qq_44909275/article/details/104891771