Explanation of nouns in Java
In the last article, I talked about the four features of Java. There are many rankings, including many commonly used rankings in learning Java in the future. For beginners, they may not know what they mean, or their harsh understanding is not particularly thorough. Here I will explain the meaning of these words based on my own understanding.
package
In Java, it is often said that a class under a package. So what is a bag? When operating a computer at ordinary times, we put the price of a document, video, audio, etc. in Changjiang under a folder, which is called a package in Java. In fact, saturated folders are equivalent in a sense, but they are called packages rather than folders in Java.
class
One by one under the bag and in order to The file at the end of Java is the class. As mentioned earlier, matching is the core of Java programs, and classes describe the properties and behavior of objects. Classes are object templates [classes are abstractions of objects, and objects are instantiations of classes]
Classes are divided into ordinary classes, abstract classes, interfaces, nested classes, etc. By level, it is divided into parent and child classes
General class
[access modifier] class name [implements / extensions class name] {}, in which there is a special class object class. Object class is the parent class of all classes. If a class does not explicitly indicate that it inherits from a class with extensions, it inherits object class by default.
abstract class
When there is not enough information in a class to describe a specific bookstore, this class is an abstract class.
Syntax: [access modifier] abstract class name {}
characteristic:
1. Abstract classes cannot be instantiated
2. Abstract classes can have constructor methods
3. Abstract classes can declare instance variables, static variables, ordinary methods and static methods. Variables can be initialized in abstract classes
4. Abstract class can also have abstract method syntax: [access modifier] Abstract return value type method name ([parameter]);
5. Abstract classes and abstract methods are implemented by subclasses of abstract classes
6. If a class is an abstract class, it does not necessarily have abstract methods, but if a class has abstract methods, it must be an abstract class
7. If the subclass of an abstract class does not implement all the abstract methods in the abstract class, the subclass also needs to be defined as an abstract class, and the subclass with subclasses is responsible for implementing the remaining abstract methods.
8. Ordinary methods in subclasses can be implemented or not, just like ordinary classes
Interface
It is the life that provides a series of functions externally (in other words, the methods defined in the interface only have the life of the method, and there is no method body)
Syntax: [access modifier] interface name {
/ / declaration of constants and methods
}
characteristic:
1. The method declared in the interface has no method body, only the method declaration
2. The member attributes declared in the interface are static and immutable, because the member attributes in the interface are modified by public static final by default
3. An interface has no constructor and cannot be instantiated
4. Interfaces can implement multiple inheritance, separated by commas
5. Methods in the interface can only be modified by public. Even if there is no explicit God access modifier, it is public by default
Implement the interface through implements. Syntax:
[access modifier] class name implements interface 1, interface 2 {}
Nested class
A class declared inside a class is called a nested class
Syntax: [access modifier] class name{
[access modifier] [static] class name{
}
}
Classification: Inner Class: non static nested class
Static nested class: a nested class decorated with static
Internal class: the internal class exists as a member of the external class, and is juxtaposed with the member variables and member methods of the external class
Local internal class: the class declared in the method body can access the final parameters and final local variables in its method
Static members or non static member variables can be declared in static nested classes, but only static members in external classes can be accessed
A method is a block of code that implements a function
Method definition syntax:
1. Method without return value and parameter [access modifier] void method name () {/ / function code of the method}
Note: void means that the method has no return value (that is, no result will be returned after calling the method)
2. Method with no return value and parameters [access modifier] void method name (data type parameter name,...) {/ / function code of the method}
Note: the parameters in the method are also called formal parameters (formal parameters). The data passed when calling the method is called actual parameters (actual data). The passed parameters should match the parameter types in the method
3. Method with return value and no parameters [access modifier] return value type method name () {return return value;}
Note: 1. The return value type is determined by the return value
2. In the last sentence of the return value method, it must be the return value of return;.
4. Method with return value and parameters [access modifier] return value type method name (parameter list) {return return value}
Definition: an element used to store data in Java, and the data stored by the element can change, so it is called a variable
Note: variables are also part of identifiers
Syntax: 1. Variable name variable assignment data type variable name = variable value
2. First declare and then assign the name of data type variable; Variable name = variable value;
Four steps to define variables:
1. Declaring a data type is actually to open up the corresponding memory space according to the data type
2. Naming a variable actually means that in order to associate the variable name with the memory space corresponding to the data type
3. Assigning a value to a variable is actually storing the data in the memory space corresponding to the variable name
4. Using variables is actually reading out the data in the memory space corresponding to the variables
Definition: in the program, its value is immutable, so it is a constant in time
Syntax: final data type constant name = constant value;
Suggestion: constant names are generally capitalized
Identifier is the name of class name, method name, parameter name, package name and other elements, and the written character sequence is collectively referred to as identifier
Naming rules for identifiers:
1. There are subtitles, numbers, underscores, and $
2. Cannot start with a number
3. Strictly case sensitive
4. Unlimited length
5. Cannot be a Java keyword or reserved keyword
Suggestion: see the name and meaning
When defining classes, properties and methods, you need to specify access modifiers to limit the accessible scope of classes, properties and methods. Access modifiers in java have the following keywords:
Public: public. This class or not can be accessed
Private: private. Can only be accessed in the body of the class. Only properties and methods can be modified, not classes
Protected: protected. Members of this class and its subclasses can be accessed, and classes in the same package can also be accessed. Only properties and methods can be modified, not classes
Default: no modifiers are used. Only classes in the same package can be accessed
keyword
A word that has been given a certain special meaning and purpose in Java
Reserved keywords are also keywords, but they do not have any special meaning and purpose at present.
This keyword
Scope of use: this keyword can only be used in methods not modified by static keyword
Function: this keyword refers to a reference to an object of the current class (in short, this can be regarded as an object of the current class, and the object that calls the current method will be pointed to by this keyword)
[when local variables and member variables are completely consistent, local variables shall prevail, i.e. the principle of proximity]
Usage:
1. When the parameter name or variable name in the method is exactly the same as the member variable, the scope can be distinguished by the this keyword
2. When this keyword appears in the first sentence of the constructor in the form of this (parameter list), it means that the current constructor has called other constructors in this class.
Final keyword
1. The variable modified by final is equal to a constant and cannot be changed once assigned (it means that the reference variable cannot be changed, and the content of the object pointed to by the reference variable can still be changed)
[final, whether modifying global variables or local variables, can be used only after initialization during definition]
2. The parameters in the final modification method are called final parameters, and the final parameters cannot be re assigned in the method body
3. Final modifies a class that cannot be inherited
4. The method of final modification cannot be overridden
Staitic keyword
Static can modify variables, methods, and code blocks
Static modifies variables, also known as class variables, because variables, methods and code blocks modified by static keyword belong to classes, not to an object
[instance variable: all member properties or methods not modified by static belong to an object, which is also called instance property or instance method. Only when the object exists can there be space for instance variables]
The genus and method of static modification can be through: class name Property name or l class name Method (parameter)
characteristic:
1. Only static methods and static member variables can be accessed in static methods
2. In non static methods, you can access not only non static methods and properties, but also static properties and static methods
3. This and super keywords cannot be used in static methods, because this and super represent references to objects, while static modified methods belong to classes, so there is a certain syntax conflict
Static code block: a static code block can only appear inside a class and outside any method
Static code block syntax: static {}
Static code block is used to initialize static member attributes
The priority of static code blocks is higher than that of construction methods
In Java, no matter the basic data type or the reference data type, the parameter is passed by value (that is, the passed data is concrete data)
During parameter transfer of basic data type, the value transferred is a copy, that is, when the current data is copied and then threaded, the value itself will not change when the parameter is modified in the target method
When referring to the data type word for parameter transfer, the memory address where the data is located is passed. Therefore, in the target method, modify the parameter, and the value itself will change accordingly
Upward and downward transformation
Upward Transformation: assign a subclass object to the parent class, and the object will be used as the parent class object
Dynamic (delayed) binding mechanism: during the running of the program, the method will be called according to the real creator of the object
Conditions for dynamic binding mechanism: 1. Inheritance 2. Rewriting 3. Upward transformation 4. Call the method rewritten by the subclass with the upward transformation object
Downward Transformation: to copy a parent object to a subclass, you need to add a castor
Downward transformation must be the restoration of types on the basis of upward transformation
The downward transformation calls methods in subclasses that do not have overrides
You can use the instanceof keyword to determine whether an exclusive belongs to a category
Finally, to illustrate, in the above article, the part enclosed with [] in all grammar can be omitted. Because it is also written while sorting, it may be messy, and some contents will not be talked about. These contents will be explained in detail in the process encountered in the future.