Home  >  Article  >  Java  >  Java -- a few simple concepts of object-oriented programming language (oop)

Java -- a few simple concepts of object-oriented programming language (oop)

php是最好的语言
php是最好的语言Original
2018-08-06 17:35:131918browse

Java is an object-oriented programming language (oop), and everything in it is an object.

A few concepts:

1. Reference. Reference is used to operate objects. Manipulating objects requires references. For example, the remote control (reference) controls the TV (object) , can exist independently of each other.

String s = "asdf";Create a reference and initialize it.

String s = new String("asdf"); The reference is associated with the object.

2. Basic types, different from reference variable creation,

include:

boolean, char, byte, short, int, long, float, double, void.

3. Scope, the scope determines the life cycle and scope of the variables defined within it.

4. Class. A class determines the appearance and behavior of a certain type of object. It is a collection of similar objects. In layman's terms, a class specifies basic types for some objects.

class a{

/**asjgjhg/

} Create a class

a b = new a();//Create an object of this type

5. Methods and fields, methods and fields are inside the class a member of.

class a{

int i;

double d;

} //A class with certain fields

Although this The class has no methods and can't do anything, but it can still create objects

a data = new a();

Assign values ​​to fields

data.i = 1;

data.d = 1.1;

6. Method, parameters, return value, a method is a function, the basic components of a method include: name, parameters, return value, method body.

Basic form:

ReturnType (return type) methodName (/*Parameter list*/){

/*Method body*/

}

Assuming that the return type is int and the parameter list is empty, then an object a calls a method, int x = a.methodName();

The parameter list is the information passed to the method.

7.static, the description of global variables (external variables) is preceded by static to form a static global variable. Global variables themselves are static storage methods, and static global variables are of course also static storage methods. There is no difference between the two in the way they are stored. The difference between the two is that the scope of non-static global variables is the entire source program. When a source program consists of multiple source files, non-static global variables are valid in each source file. Static global variables limit their scope, that is, they are only valid within the source file in which the variable is defined, and cannot be used in other source files of the same source program. Since the scope of static global variables is limited to one source file and can only be shared by functions in that source file, errors can be avoided in other source files.

From the above analysis, we can see that changing a local variable to a static variable changes its storage method and changes its lifetime. Changing a global variable to a static variable changes its scope and limits its scope of use.

Static functions have different scopes from ordinary functions. only in this document. Functions used only in the current source file should be declared as internal functions (static), and internal functions should be described and defined in the current source file. For functions that can be used outside the current source file, they should be stated in a header file. The source files that use these functions must include this header file

What is the difference between static global variables and ordinary global variables: static Global variables are only initialized once to prevent them from being referenced in other file units;

What is the difference between static local variables and ordinary local variables: static local variables are only initialized once, and the next time will be based on the previous result value;

What is the difference between static functions and ordinary functions: static functions have only one copy in memory, while ordinary functions maintain a copy every time they are called.

Summary of this chapter: The form of a Java program

import java.util.*; //Reference other components and import packages

class ClassName{ //Create a class, The class contains member methods, fields

int method(){ //Create a method

/*The method contains various commands*/

}

}

Related recommendations:

Basic concepts of object-oriented object-oriented programming language Yu Guoli js object-oriented programming

An understanding Examples of PHP object-oriented programming (OOP), object-oriented programming oop

The above is the detailed content of Java -- a few simple concepts of object-oriented programming language (oop). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn