Java Basics Note (vii: interfaces, variable scopes, and parameter passing)

Source: Internet
Author: User

OneInterface

The Java language does not allow a subclass to have more than one direct parent class, that is, any subclass can have only one direct parent. However, a class is allowed to implement multiple interfaces, that is, a list of interface names that define a class can contain 1 or more interface names, thereby enabling multiple inheritance attributes. The interface is defined in the following format:

"interface modifier word list" interface Interface Name "extends interface Names list"

{

Interface Body

}

The interface modifier list can contain0A,1One or more interface modifiers. If more than one interface modifier is present, a space is separated between the adjacent two interface modifiers. Interface modifiers include: Public,Abstractand theSTRICTFPand so on. In the sameJavaA source file can contain multiple classes or interfaces, but it cannot contain two or more than two Publica class or interface that modifies a word. If the keyword is not included in the interface modifier list Public,protectedand thePrivate, the interface defined above has the default encapsulated access control property and can only be used in the current package.

The interface name can be any legal identifier.

Extends Interface Name list is optional, if you include the Extends J Interface Name list option, you can include 1 or more interface names in the interface name list. The adjacent two interface names are separated by commas.

The interface body section defines two types of member features of an interface: member fields and member methods. There is no construction method inside the interface body, so it is generally not possible to generate an interface's instance object directly from the interface. Where the member domain of the interface, referred to as the domain, is defined in the following format:

"domain modifier word list" type List of variable names with initialization;

all member fields of an interface have public,static , and final properties.

The member method of the interface, referred to as the method. The member method of an interface can only be an abstract member method, which is defined in the following format:

"method modifier word list" return type method Name (the parameter list of the method);

all member methods of an interface have public and abstract properties.

TwoVariable Scope range

in the in the Java language, variables mainly include member fields, parameter variables of member methods or constructor methods, local variables in the method body. Variable scope range refers to the valid range of variables in a Java program. Variable scope scopes can be divided into three types: global scope, class scope, and block scope. Specific as follows:

(1) global scope range

static member domains have global scope scopes.

(2) scope of class scope

member domains that do not have static properties have class scope scopes. Member fields that do not have static properties can be accessed directly in the method body of a member method of the type or its subtypes of that member domain, even if the definition of the member method precedes the definition of the member domain.

(3) block scope range

parameter variables for member methods or constructor methods and local variables defined within the method body have block scope scopes. Here the parameter variables of the member method or constructor method and the local variables defined in the method body are collectively referred to as local variables. The scope of a local variable is from the declaration of the variable to the end of the block where the variable resides.

Threehow the value of a method call is passed

Method invocation parameters are passed in the way that the method calls the parameters of the method when they are called from the method . In the Java language, the method invocation's parameters are passed in a way that is basically passed by value. The declaration format of the member method is as follows:

"method modifier word list" return type Method Name (parameter list of the method)

the format of the method call is in the following 3 ways :

(1) member Method name (member method invocation parameter list)

(2) an expression . member Method Name (member method invocation parameter list)

(3) class name . static Member Method name (member method invocation parameter list)

when a method is called, the calling parameter is required to have the same number of parameters as the member method's definition, and the type matches.

When making a method call, the contents of the storage unit that invokes the parameter expression are first copied to the storage unit that defines the parameter variable, and the value of the parameter expression is assigned to the definition parameter variable, so that the value of the definition parameter variable is the same as the value of the invocation parameter expression, which is called parameter passing.

In addition, it is important to note that the value passing of the parameter cannot change the value of the calling parameter variable, and the value of the calling parameter variable can be passed with the reference data type value if it needs to be changed. (This is similar to a pointer in C, passing a value using a pointer to change the value of the calling parameter variable)

Java Basics Note (vii: interfaces, variable scopes, and parameter passing)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.