Object-oriented, class

1. Relationship between class and object

Class is an abstraction of objects, does not occupy memory, and represents the concept of a group. It is a group with the same attributes and methods A collection of objects;

An object is a specific instance of a class, used to describe an entity of objective things, composed of a set of attributes and methods, representing individual concepts, unique and independent, and occupying storage space .

Second, Object-Oriented and Process-Oriented

Both are software development ideas, first process-oriented, then object-oriented. In large-scale projects, object-oriented development ideas were introduced to address the shortcomings of process-oriented.

①Different programming ideas: Process-oriented focuses on the development of functions that realize functions, while object-oriented first abstracts classes, attributes and methods, and then completes functions by instantiating classes and executing methods.

②Encapsulation: Both have encapsulation, but process-oriented encapsulation is function, while object-oriented encapsulation is data and function.

③Object-oriented has inheritance and polymorphism, while process-oriented has no inheritance and polymorphism, so the advantage of object-oriented is obvious.

3. Relationship between method override and overload

Method overload: In the same class, the method name is the same, the parameter list is different, and the modification is Characters, return values, and throwing exceptions are irrelevant.

Method rewriting: In the two classes with inheritance relationship, the method name is the same, the parameter list is the same, and the modifier cannot be stricter than The parent class,return value, exception thrown or Same as the parent class, or is it(Parent class) Subclass.

Four, class construction method

ConstructionMethod:Specially used to construct objects, usually andnewUse with. Construct the method has no return value in the form (essentially return this), the method The name is the same as the class name. Construction methoddivided into parametric construction method And no-argument construction method .

1,If in the classnot shown defined no parametersConstruction method,jvmBy default, this class is assigned a no-parameter structure. The parameterless construction method is used to assign the default value to the object when constructing the object initial value.

2. If the class provides a parameterized structure,jvmno parameter structure is no longer allocated to this class. So, we need to provide no Parameter structure.

Five, this and super

1, this is a reference inside the object that refers to itself, and itself is an address

  ①this can call member variables, usually used to resolve the conflict between member variables and local variables with the same name

  ② this can call member methods

  ③ this can call overloads in the constructor The construction method, and must be the first statement of the construction method.

2, super represents a reference to the direct parent object of the current object, which has no meaning in itself

  ①super can call the member variables of the direct parent class (note the permission modifier, such as no access private member)

  ②super can call the member methods of the direct parent class (pay attention to the permission modifiers, such as not being able to access private members)

  ③super can call the construction method of the direct parent class, only Used in the construction method, and must be the first statement

VI. static keyword (modify variables, methods, code blocks and internal classes)

1. The static attribute belongs to this class, that is, all objects created by this class share the same static attribute. Object name, attribute name and class name. attribute name (recommended) to access.

  The difference between static variables and non-static variables (both are member variables, not local variables)

  ①No matter how many objects there are, there is only one static variable. For each object, non-static variables will have a separate copy

  ②Static variables are stored in the method area, and instance variables are stored in the heap memory

  ③Static variables: object name. Attributes Name and class name. Property name (recommended) to access, instance variable: Object name. Variable name access

  ④Static variable: When jvmWhen loading bytecode, allocate space in the method area, and instance variables: allocate space in the heap area when creating an object.

2. The static method can also be accessed in two ways: object name, method name and class name. method name (recommended).

  ①Static Methods cannot access instance variables, this, instanceMethod (static members allocate space before instance members)

  ② Example method can access static variables or static methods

3, static code block. When jvmload bytecode, the static code block is executed, and it is executed only once. The main function is to realize the initialization of static attributes.

4, static inner class: belongs to the entire outer class, not every object belonging to the outer class. Can not access the non-static members (variables or methods) of the external class, but can access the static members of the external class

VII. Code block

Code block{} injava can be divided into ordinary code block, structural code block, static code block, synchronization< /span>Code block.

1. The common code block is the scope block. ScopeNesting and scope chaining.

2, Construct the code block in The inside of the class, the outside definition of the construction method. Defined by { }. The construction code block is executed before the construction method;ConstructionThe code block constructs an object and executes it once.

3, The static code block is in The inside of the class, the outside definition of the method. Defined by static{ }. Static The code block is only executed when the class is loadedOnce; Static Code block is executed before construction code block and construction method;Static code blockcannot access instance members.

Leave a Comment

Your email address will not be published.