Java basic review record

Java basic review records (arrays, objects, exceptions)

Array

Array definition

An array is an ordered collection of data of the same type. For example, a basketball team is an array. Players have numbers on their uniforms, which is equivalent to an index. Confirm a certain player by a certain number. The index in the array starts from 0.

Array creation statement

1. First, you must declare array variables. The following is the syntax for declaring array variables:

dataType[] arrayRefVar; // 使用首选
或
dateType arrayRefVar[]; // 一般不用
int[] a; //创建变量名为a,数组类型为int的数组

int a[];

2. The java language uses the new operator to create an array. The syntax is as follows:

dataType[] arrayRefVar = new dataTye[arraySize];
int[] a = new int[5]; // 创建长度为5的int类型数组

3. The array elements are accessed by index, and the array index starts at 0.

4. Get the length of the array.

arrays.length

Basic characteristics of arrays

1. The length is determined. Once created, the size and length cannot be changed.

2. The element types of the same array must be the same.

3. The type of the array can be any array type.

4. The object of the array itself is in the heap.

Memory analysis

1. The memory is divided into three parts: heap, stack, method area

2. Heap: store new objects and arrays

3. Store basic variable types and variables of application objects

4. Method area: can be shared by all threads, including all class and static variables

Three steps to generate an array

1. Declare the array

2. Create an array

3. Initialization

Sparse array

Object-oriented

Java core idea object-oriented (OOP), object-oriented: OO.

What is object-oriented

Object-oriented and process-oriented

1. Process-oriented thinking (micro operation):

Do one thing and planning arrangements of steps, such as: building the first step to digging machine to dig the foundation, the second step to cut brick division to cut bricks, the third step to cement the company to pull the cement cap. Each step must be planned in detail.

2. Object-oriented thinking (macro control):

There must be a classified mode of thinking and abstraction. Thinking must first solve those categories, then think about these categories separately, and finally process-oriented thinking about the details under a certain category. For example: the chief designer designs the building, how many square meters is this building, how deep is the foundation, how about the structure of each house, etc. Before starting construction, think about the construction method of the whole building and who will be assigned to work.

abstract

For example: students have and certainly have information such as student number and class, which can be abstracted out, and the class of students has student number and class.

For example, everyone must have information such as gender and name. Abstracted out, the attributes that a human class can have are gender and name.

Three characteristics

Encapsulation: For example, the water in the drinking fountain is encapsulated to prevent it from being contaminated by contact with dust in the air, and provides an interface for use.

Inheritance: For example, the son inherits all the things (property) of the father.

Polymorphism: For example, it is also learning, some people are learning bullies, some people are learning scum The same behaviors have different forms.

Classes and objects

A class is a description of a certain category, and objects are specific things.

Such as: the description of the person has gender, name, age. Through this information you can only know some attributes that everyone has in common.

Target: Gender male, name Zhou Yong, age 18. With this information, you can determine who it is.

Constructor

1. The constructor requires that the method name is the same as the class name, there is no return type, and void cannot be written with

2. Function: Assign the initial value, you can set the initial value in the constructor.

3. The general system generates a parameterless structure by default.

4. Parametric construction Generally, when new is an input parameter, parametric construction is used.

Memory analysis

Package

1. Packaging pursues high cohesion and low coupling.

2. Private property, get / set

Code

1. After using the private modification attribute in the encapsulation class, the data value cannot be obtained through the object point attribute externally.

2. Get and set methods are provided after encapsulation. The get method gets the value of the encapsulation class, and the set method sets the value of the encapsulation class.

3. Function: Set the range through the set method: For example, if the person's age is greater than 1 ~ 120 years old, it can be set in the set method. If it exceeds this range, it will be wrong.

public void setAge(int age) {
    if (120 >= age && age > 0) {
        this.age = age;
    } else {
        System.out.println("输入错误:");
    }
}

The meaning of encapsulation

1. Improve program security and protect data

2. Implementation details of hidden code

3. Agree with the interface

4. System maintainability increased

inherit

1. The essence of inheritance is the abstraction of a certain batch of classes, so as to achieve a better modeling of the real world

2. extends means "extension". The subclass is an extension of the parent class.

3. Classes in java only have single inheritance, not multiple inheritance.

4. Private cannot be inherited

super and this

1, super points to the inherited parent class

2. This points to the class itself

important point:

Note for super:

1. Super calls the constructor of the parent class, which must be the first in the constructor

2. Super must only appear in subclass methods or constructors.

3. Super and this cannot call the constructor at the same time.

Defining a parameterized structure must also define a parameterless structure, otherwise an error will be reported.

Method rewriting

Rewriting method: need to have inheritance relationship, subclass rewrite the function of parent class.

1. The method name must be the same

2. The parameter list must be the same

3. Modifier: The scope can be expanded, but cannot be reduced.

4. Exceptions thrown: the scope can be reduced, but not expanded; classNotFoundException-> Exception (large)

Reasons for rewriting

1. The function of the parent class, the child class does not necessarily need, or may not be satisfied.

Methods that cannot be overridden

1. The static method belongs to the class, it does not belong to the instance.

2. final constants;

3. Private method.

Polymorphism

Condition of existence

1. There is an inheritance relationship

2. Subclasses override parent class methods

3. The parent class reference points to the child class object

coding:

1. The parent class reference points to the child class

2. The methods rewritten by subclasses can be called, the methods of subclasses are called

3. Cannot call specific methods of subclasses

4. You can call the methods defined by the parent class

instanceof and type conversion

instanceof (type conversion) Reference type to determine what type an object is

instanceof is OK with a parent-child relationship, not without a parent-child relationship

Abstract class

1. The class modified with abstract is the abstract class.

public abstract class Action(){...}

2. I defined this method, but I don't want to implement it. I can use an abstract method. Abstract methods, only method names, no methods to achieve.

public abstract void doSomething();

3. All the methods of the abstract class inherit its subclasses, and the subclasses must implement the methods of the abstract class

4. Function: Improve development efficiency. For example, to create a game character, we can't create it from scratch, it should be assigned on the characteristics of all heroes in the game. Abstract the characteristics that all heroes have.

static

1. Static variables can be shared and can be called directly, and can only be used after new.

Execution order:

1. First execute the static code block

2. Secondly execute the anonymous code block

3. Then execute the constructor

Interface and implementation definition

Inner class

Knowledge expansion

abnormal

1. Abnormal architecture, the highest layer is Throwable,

Error: The operation error of the virtual machine is generally irrelevant to the executor.

Exception: Generally caused by personal coding problems.

Common mistakes are:

Five keywords for exception handling

Code

Define exception

This study record is mainly used to strengthen and review the foundation of the students with foundation.

Learning to watch the video is: Crazy God said java zero-based learning

Guess you like

Origin www.cnblogs.com/zhouyongyin/p/12683597.html