Defines the method general format for a class.
Grammar:
< access modifiers > return value type < method name > (< parameter list >) {
The body of the method
}
which
1. < access modifier > refers to the permission range that the method allows access to, only public, protected, or private. Where the public access modifier indicates that the method can be called by any other code.
2. The return value type refers to the type of the method return value. If the method does not return any values, he should declare the type void. Java has strict requirements for the return value, and the method return value must match the type described. Return a value using the return statement.
3, < method name > is the name of the method defined, he must use a valid identifier.
4. < parameter list > is the parameter list that is passed to the method. Each parameter in the list is separated by commas.
A property in a class can be a single variable or an array. You can pass the operator "." An element that accesses an array member or array member of a class. For example:
Studentsbiz stubiz=new studentsbiz ();
Stubiz.names, or stubiz.names[1];
Calling the Take parameter method
Grammar:
The name of the object. Method name (parameter 1, Parameter 2, ..., parameter n)
When defining methods and calling methods, the parameters are referred to as formal parameters and actual parameters, for short, formal and practical arguments. Formal parameters are called when defining a method, and the purpose is to define the number and type of arguments that the method needs to pass in. Arguments are the actual values that are passed to the method when the method is called.
The following two points need to be noted when calling a method.
1. Instantiate the object first, then call the method.
2. The type, quantity and order of the arguments should correspond to the parameter one by one.
The package mainly has the following 3 aspects function:
1. The package allows classes to be grouped into smaller units (like folders), which are easy to find and use for the appropriate class files.
2. Prevent naming conflicts: only classes in a different package in Java can have duplicate names.
3. The package allows classes, data, and methods to be protected in a broader scope, and classes can be defined within the package. According to the rules, code outside the package may not be able to access the class.
How to declare a package
To declare a package, you can do so as long as the first statement of a Java source file is included as a bundle command.
The following is a common form of declaring a package:
Grammar:
Package name;
The package is a keyword. The declaration must be the first non-annotative statement in the Java source file, and a source file can have only one package declaration statement.
In Java, packages are designed to correspond to file system structures. Therefore, when naming packages, the following coding specifications are strictly adhered to.
1. The name of the Java package is usually made up of lowercase letters and cannot begin or end with a dot. For example, ". MyPackage" is the wrong package name.
2, a unique package name is usually prefixed with all lowercase ascii letters, and is a top-level domain, COM, edu, gov, net, or org, usually using the reverse order of the organization's network domain name. For example, if the domain name is javagroup.net, we can declare that the package is--package net.javagroup.mypackage;
3. The following parts of the package name vary according to the respective internal specifications of the different institutions. Such naming conventions may differentiate a department, project, machine, or registered name by the composition of a particular directory name. For example--package Net.javagroup.research.powerproject; (which is the department name, Powerproject is the project name)
To use a class that is not in the same package, you need to include the package display in a Java program. In Java, using the keyword import tells the compiler which package to use for the class in which it is called an import package. The following code is to import the package java.util provided to us by Java itself
Import java.util.*; Import Java.util Package
When using import, you can specify a complete description of the class, "package name. Class name", to import a particular class in the package.
Grammar:
Import package name. class name;
The package name here can be a system-provided package, such as java.util, or it can be a package of its own definition, such as cn.jbit.classandobject.
If you want to use some of the classes under the package (more than one), you can use the "package name. *" When importing with import. The syntax is as follows.
Grammar:
Import package name. *;
Declare the meaning of the package: declares where the current class is located.
Meaning of the import package: declares the location of other classes to be used in the current class.
S1/using Java to understand program logic/14-a method with parameters