Dark Horse Programmer-java Basics-Object-oriented-classes and objects, encapsulation, constructors, this, static, a hungry man & lazy-type

Source: Internet
Author: User

First Lecture Object-oriented concepts

1. Definition

As opposed to process-oriented, encapsulating functionality into objects, we only care about the objects that have the functionality, not the specifics of the object.

Object-oriented features: make complex problems simple. We only care about what the object can handle, not the specifics of the implementation.

2. Object-oriented features

Encapsulation, inheritance, polymorphism.

Second lecture relationship of classes and objects

1. Overview

The class is: the description of things in real life, can be physical things can also be a thing, etc.

Object is: an instance of a certain kind of thing, a real existence of the individual;

Mapped to Java, the description is class defined by class.

The specific object is the entity that Java creates in heap memory with new.

2. Describe class and object relationships for example

The drawing is used to describe the car, and the actual car is a car entity.

The drawings describe the properties of the car (color, length, width, etc.) and describe the behavior of the car (turn on the light, start, etc.).

Third Speaking Package

1. Definition

Encapsulation refers to the properties and implementation details of hidden objects, which provide public access only externally, and can be encapsulated by the use of private.

Benefits: Isolation of changes, ease of use, improved reusability, increased security

Encapsulation principle: Hide content that does not need to be provided externally, hide properties, and provide public methods to access them.

2. Encapsulation Example

The age and name in the code are already encapsulated in the person class.

The reason for providing access to private members is that they can add logical judgments and other statements to access the data, and improve the robustness of the code.

Fourth Lecture Constructors & Building Code Blocks

1. Features

1) The function name is fixed, the same as the type;

2) Do not define return value type, different from void;

3) can not write return statement;

2. Application of constructor function

1) When an object is established, the corresponding constructor is called and can be used to initialize the object.

2) When a constructor is not defined in a class, the system defaults to a constructor that adds an empty argument to the class, which makes it easier to initialize the class.

3) When a constructor is customized in a class, the default constructor is gone.

Application Example: You can define multiple constructors, because different instance object initialization methods may be different.

3. The difference between a constructor and a general function

1) The constructor is executed when the object is established and initialized to the object;

2) The general function is executed only when the object is called, and the function of object is added to the object.

3) An object is established, the constructor runs only once, and the general method can be called multiple times on the object;

4. Determine when to define a constructor

When a thing is analyzed, it has some characteristics or behavior, then the content is defined in the constructor

5. Building Code Blocks

function: initializes the object. The object is already set up to run, and takes precedence over the constructor function execution.

and constructors: building blocks of code is a unified initialization of all objects, and constructors are initialized to the corresponding objects.

Fifth Lecture Key Words

1. This keyword

This represents the object of this class, which represents the reference to the object to which the function belongs, that is, which object is represented by this object, which is the function in which this is called.

Application:

1) used to distinguish between local variables and member variables with the same name as follows:

2) when defining a function in a class, this is used to represent the object when it is used inside the function to invoke the object that called the function.

2. The application of this in the mutual tuning of the structure function

Used to call each other between constructors.

Note: The statement can only be defined in the first row of the constructor, because initialization is performed first (when a constructor is called, this () in the function body is the equivalent of initialization)

Sixth talk static Key Words

1. Static effect

Used to decorate members (member variables, member functions).

Content that is modified by the static is shared by the object, and the content and objects are stored separately in memory.

2. Features

1) load with the load of the class and disappear with the disappearance of the class, the longest life cycle;

2) prior to the existence of the object, static is the first existence, the object is after the existence;

3) shared by all objects;

4) can be called directly by the class type

3. The difference between an instance variable and a class variable (a static modified member variable)

1) in-memory location

The L class variables exist in the method area (shared area/data area) as the class loads.

The instance variable exists in heap memory as the object is established;

2) Life cycle

The life cycle of the L-class variables is the longest, loaded with the loading of classes, disappearing with the disappearance of classes;

The life cycle of an instance variable is loaded as the object loads and disappears as the object disappears;

4, Static use precautions

1) Static methods can only access static members

Because static methods are loaded with the load of the class, but non-static members are dependent on the creation of the instance object, the static methods and non-static members have different lifecycles, and errors occur on invocation, so static methods cannot invoke non-static members. But non-static methods can access both static and non-static

2) cannot define This,super keyword in static method

Because static overrides the existence of an object, this, super represents an instance object.

3) The main function is static

L Main function: A special function, as the entry of the program, can be called by the JVM.

L Definition of main function:

L Public: Represents the maximum access rights of the function;

L Static: Represents the main function with the loading of the class already exists.

L Void: The main function does not have a specific return value.

L Main: Not a keyword, but a special word that can be identified by the JVM.

L (string[] args): The parameter of the function, the parameter type is an array, the element in the array is a string, an array of type string.

L Features: The main function is fixed format, only in this way can be recognized by the JVM, while the main function also has the characteristics of general functions, can be overloaded

5, the advantages and disadvantages of static

1) Advantage: The shared data of the object can be stored in a separate space, saving space, and it is not necessary to store one copy of each object. A static member can be called directly by a class name, rather than a static member that must be called through an object.

2) disadvantage: The life cycle is too long. Access is limited, static methods can only access static members

6. When to use static

1) When to define a static variable

When shared data is present in an object, the data can be modified statically, and the unique data of the object must be defined as non-static and stored in the object space corresponding to the heap memory.

2) When to define a static function

This function can be defined as static when there is no access to non-static data (unique data for the object) within the feature.

7. Static code block and non-static code block

1) Static code block: executes as the class is loaded, executes only once, and takes precedence over the main function execution to initialize the class.

2) constructs a code block: As the instance object is created, each object created is executed once and belongs to the instance object.

execution Order: static code block--Main function--building blocks of code

Writing format:

Static

{

The execution statement in a static code block.

}

Seventh Lecture single case design mode

1. Concept

Design Patterns: The most effective way to solve a class of problems, there are 23 design patterns in Java.

Singleton design pattern: Solve an issue where a class has only one object in memory

2. Methods to solve the uniqueness of objects

1) Prohibit other programs from establishing such objects: privatization of constructors;

2) in order for other programs to have access to the class object, you need to customize an object in this class;

3) in order to allow other programs to access objects in this class, you need to provide some way to access: provide a public way to get the class object

3. Single-Case design pattern representation

1) A hungry man: Once the class is loaded, the class object is created in heap memory.

2) Lazy: An object is created only when the method is called.

lazy drawback: lazy-type errors can occur when the function that creates the object is called by multiple programs at the same time.

Workaround: The problem can be solved by locking (synchronized), and the operation is slightly more efficient by double-judging whether the object is empty.

Dark Horse Programmer-java Basics-Object-oriented-classes and objects, encapsulation, constructors, this, static, a hungry man & lazy-type

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.