Java BASICS (7: interface, variable scope, and parameter transfer)
I,Interface
Java does not allow a subclass to have multiple direct parent classes, that is, any subclass can have only one direct parent class. However, a class can implement multiple interfaces, that is, the interface Name List of the defined class can contain one or more interface names, so as to implement multiple inheritance features. The Interface Definition Format is as follows:
[Interface modifier list] interface name [extends interface Name List]
{
Interface body
}
The interface modifier list can contain 0, 1, or more interface modifiers. If multiple interface modifiers exist, separate them with spaces. Interface modifiers include public, abstract, and strictfp. A Java source file can contain multiple classes or interfaces, but cannot contain two or more classes or interfaces with public modifiers. If the public, protected, and private keywords are not included in the interface modifier list, the interface defined above has the default encapsulated Access Control attribute and can only be used in the current software package.
The interface name can be any legal identifier.
The list of Extends interface names is optional. if you include the list of extends j interface names, you can include one or more interface names in the list of interface names, the names of two adjacent interfaces are separated by commas.
The interface body can define two types of member elements: Member domains and member methods. The constructor is not included in the interface body. Therefore, you cannot directly generate an interface instance object through the interface. The member domain of the interface, abbreviated as the domain, is defined in the following format:
[Domain modifier list] list of variable names with initialization type;
All the member domains of the interface have the public, static, and final attributes.
The member method of the API. An interface member method can only be an abstract member method. Its Definition Format is as follows:
[Method modifier list] Return the type method name (method parameter list );
All member methods of the interface have the public and abstract attributes.
II,
Scope of Variables
In Java, variables mainly include member fields, parameter variables of member methods or constructor methods, and local variables in the method body. The scope of a variable refers to the valid scope of a variable in a Java program. The scope of a variable can be divided into three types: global scope, Class scope, and block scope. The details are as follows:
(1)GLOBAL SCOPE
Static member domains have a global scope.
(2)Scope
A member domain that does not have static attributes has a class scope. You can directly access a member domain that does not have static attributes in the method body of the member domain type or its Subtypes that does not have static attributes, even if the member method is defined before the member domain definition.
(3)Block Scope
Parameter variables of member methods or constructor methods and local variables defined in the method body have a block scope. Here, the parameter variables of member methods or constructor methods and the local variables defined in the method body are collectively referred to as local variables. The scope of a local variable ranges from the declaration of the variable to the end of the block where the variable is located.
III,
Method call value transfer method
The parameter transfer method of a method call refers to the method from the call parameters of the method to the parameters defined by the method during method call. In Java, the parameter passing method of a method call basically adopts the value passing method. The Declaration format of the member method is as follows:
[Method modifier list] return type method name (method parameter list)
The method call format is as follows:
(1) member method name (member method call parameter list)
(2) expression. Member method name (member method call parameter list)
(3) class name. static member method name (member method call parameter list)
When calling a method, the number of call parameters must be the same as the number of defined parameters of the member method, and the type must match.
When calling a method, first copy the content of the storage unit that calls the parameter expression to the storage unit that defines the parameter variable. That is, assign the value of the called parameter expression to the defined parameter variable, so that the value of the defined parameter variable is the same as the value of the called parameter expression. This process is called parameter transfer.
In addition, you must note that the value transfer of a parameter cannot change the value of the called parameter variable. If you need to change the value of the called parameter variable, you can use the reference data type value. (This is similar to a pointer in C. You can use a pointer to pass a value to change the value of the called parameter variable)