Object-oriented programming ideas

1. Characteristics of object-oriented programming language:

(1) When writing the code, I found that the code structure was all based on the class, and the interfaces and enumerations learned in the later period were also at the same level as the class.

Class class name {method {statement; }}Copy the code

(2) When writing code, discover that all functions are called through objects or classes

Such as:

System.out.println(xx);
Scanner input = new Scanner(System.in);
int num = input.nextInt();
String str = input.next();
double d = Math.random();
Copy the code

(3) Later, it will be found that there are relationships between classes

For example: inheritance and other relations, inheritance, composition, aggregation, dependency, etc.

2. From the way you think about it

Object-oriented programming: Abstracting real-world transactions into classes

For example, Math and Scanner

3. Compare with other programming ideas

Process-oriented: it is represented by C language, a function-centered way of thinking about problems: the steps to solve problems are given priority to, similar to the perspective of the performer to think about problems;

It is more suitable for the writing of the underlying system software, such as operating system.

Object-oriented: it represents the Java language, object-based, thinking way: to find the right class or object to complete the specified function, similar to the commander of the point of view of thinking.

It is more suitable for the development of upper application software, or the preparation of large websites.

Essence: Everything is an object.

4. Start to learn the grammar, slowly experience object-oriented programming ideas.

(1) Basic syntax: classes and objects

(2) The basic features of object-oriented: encapsulation, inheritance, polymorphism

(3) API application programming interface, commonly known as help document and core class library

Collections framework, IO, network programming, multi-threaded, reflection, JDBC, StreamAPI, date and time of the API, String, String, StringBuilder, StringBuffer)

class

What is a class?

Class: An abstract description of a class of things that have the same concrete properties.

Same features:

For example: students (tuition fees. Both have the property of learning odd numbers, encapsulation method study ()

Teacher (salary. Both have the characteristics of teaching technology, and the encapsulation method is Teach ())

People (have eating, sleeping, etc.)

Students belong to people, and students can inherit

Teachers belong to people, teachers can be successors

Things: noun

2. How to declare a class?

Data characteristics of things -> attributes, member variables

Behavioral/functional characteristics of transactions –> member methods

Class class name {member variable member method}Copy the code

A member variable of a class member

1. What are member variables?

It is used to describe the data characteristics of a transaction.

Data Features:

(1) Each object is different, for example: name and age

(2) Each object is the same, for example, the nationality is the same

2. How to declare member variables?

(1) Location: outside of a method in a class.

Variables previously declared in methods are called local variables.

(2) Grammatical structure

【 modifier 】 class class name {modifier 】 Data type variable name; }Copy the code

3. How to use member variables

Object name. Variable name

4. Member variables are divided into two categories:

(1) Static member variables

It is static and not specific to an object, so its use is not recommended by “object name”. Static variable name “to change, recommended” class name. Static variable name “initiate use

(2) Non-static member variables: each object is independent

5. How to use member variables?

Static member variables: static modifier

Static member variable names (recommended)

​ 或

Object. Static member variable name

Non-static member variables:

Object. Name of a non-static member variable

object

1. What are objects?

Class: An abstract description of a class of transactions that have the same specific properties.

Object: A concrete individual or entity of a class of things.

2. How to create an object? new

An object must be created through a class.

newThe name of the class ();// An anonymous object that cannot be called laterClass name Object name =newThe name of the class ();// The named object
Copy the code

3. The relationship between classes and objects

Classes: Abstractions of objects, blueprints, templates

Object: is a class entity, concrete individual

A. Java file can have only one class of type public, and it must have the same name

package

1. Why declare packages? What are the functions of a bag?

(1) Avoid class names

(2) The organization manages different classes, so that classes with the same function or classes of the same business module can be conveniently managed together.

For example, the Java.net package is all about networking, and the java.sql package is all about database use

(3) restrict the visibility range of certain classes or members, with modifiers

2. How do I declare packages?

In the first line of the.java file, there is a line of code:

Package the package name;Copy the code

Note: The hierarchy of package names must be the same as the directory structure in which the. Java file is actually located, otherwise an error will be reported during compilation.

3. Naming conventions for package names

(1) All words are in lower case

(2) Each episode is used between directory structures. segmentation

(3) Used to use company domain name inversion + classification module name

Level 1 domain name:

Com: commercial company

Org: organization, usually not for profit

Edu: Education

Gov: government

Do not include Java words in the package name

Error: java.test package name

prohibited package name : java.test

4. How do I use classes from other packages

Need to guide package

Guide package statements

importPackage. Class name;/ / or
importPackage. *;// But we can only omit the last class name
// Static import (static member variables)
import static java.lang.Math.*;
// Static math.pi can be omitted as PI, math.random () can be omitted as random(); // Static math.pi can be omitted as PI, math.random () can be omitted as random();
Copy the code

Note:

1. If you use classes with the same name in different packages:

You can use “import” packages for one and “import” packages for the other. The full name of the class name

2. The java.lang package does not need to be imported and can be used directly because it is used frequently.

For example, the System,String, and Math classes are all in the java.lang package

Memory map of member variables

Memory for the JVM:

(1) Method stack: store the local variables of the method

(2) heap: all new objects are in the heap

‘(3) Method area: used to store loading class information, static variables, constant values.

Conclusion:

(1) After the new object returns the first address of the object, you can put the first address in the object name, the object name is actually a reference to the data type variable

Data type variable name = value Ractangle R1 =new Ractangle();
/* Rectangle; The variable name is R1; The value is the first address of new Rectangle(); * /
Copy the code

Byte,short,int,long (0 by default); short,int,long (0 by default); Float,double defaults to 0.0; Char is a null character with an encoding value of 0 by default; Boolean defaults to false; Reference data type: NULL

(3) Non-static member variables, each object is stored separately

(4) Each object, in addition to storing the value of the non-static member variable of the current object, also needs to store the address of the class to which the current object belongs. In essence, the memory of an object has three parts: A: object header: contains the information about the class to which the object belongs B: values of non-static member variables of the object C: Align blank: If the memory occupied by all member variables of the object plus the header is not an integer multiple of 32 or 64, blank space will be added to align the memory. (5) Static variables are stored in the method area, and all objects of a class share the same static variable.

Java data types fall into two broad categories:

  • Basic data types: 8

  • Reference data types:

    One-dimensional array [], two-dimensional array [][]

    Classes: Any class declared as a class can be used as a type to declare variables

    Interfaces, enumerations, annotations, etc

A method of a member of a class

1. What is method

Method: also known as function, a piece of independently run, reusable code, this piece of code is to complete an independent function.

For example: math.random (), which is a method whose function is to return a random decimal of [0,1], or math.sqrt (x), which is a method whose function is to take the square root of x and return.

2. How to declare methods

(1) Position: outside the method in the class

(2) Format:

【 modifier 】classThe name of the classReturn value type method name (parameter list)throwsException list 】{method body statement block; }}Copy the code

(3) Return value type:

What is the return value? The result that needs to be returned after the method is run.

What is a return value type? The type of result returned by the method.

Note: Some methods do not return a value. If the method does not return a value, the return value type must be void

Method name: an identifier that represents a method and its function. The naming conventions for method names are as follows: capitalize the first letter from the second word.

(parameter list) : is a parameter required to complete the function of the method. If you don’t need parameters, you don’t need to write a parameter list.

For example: math.sqrt (x) The x is the argument.

SQRT is declared in math. SQRT to indicate that the method is used when the user needs to give it an argument

In math.random (). The random() method has no argument list, and the user does not need to pass it arguments.

Note:

  • Some methods have no return value. If there is no return value, you need to decorate the function with void

3. How to call a method?

(1) Call methods in other classes

  • Non-static method, called:
Object.methodCopy the code

Note: If a method is declared with a list of arguments, the argument list must be passed in when the method is called.

Number: The number of argument lists must be the same as that of parameter lists.

Type: The argument type of the argument list must be the same or compatible with the type of the parameter list.

Order: The order of arguments in the argument list must correspond to the order of arguments in the parameter list.

If the method declaration has a return value, the call needs to accept it; if not, the return value is lost.

(2) Use a member variable of this class in a method: you can use it directly

4. Classification of methods

1. The function realization of the method is related to the object. (Non-static method)

For example: the method of finding the area, different objects, the area is different.

2. Methods should be declared static if their implementation is independent of the object. (Static method)

How methods and variables are called

1. Method invocation

(1) In other categories:

Non-static methods:

Object name. method

Static method:

Class name. method

or

Object name. method

(2) in this class

Static methods cannot use non-static methods of this class.

Static can access static

Non-static access can be static or non-static

Static methods cannot use non-static methods of this class. This is because:

Static is loaded as the class is loaded, while non-static is loaded only when the class is created.

2. Call member variables

(1) in other classes

Static variables:

Static variables (recommended); Static variables;Copy the code

Non-static variables:

Object. Non-static variable;Copy the code

(2) In this class, you can directly use the variable name to access.

Static methods can access static variables

Non-static methods can access static and non-static variables