Declaration rules for Java source files
When you define multiple classes in a source file, and there are import and package statements, pay special attention to these rules:
There can be only one public class in a source file.
A source file can have more than one Non-public class.
The name of the source file should be consistent with the class name of the public class. For example, the class name of the public class in the source file is employee, and the source file should be named Employee.java.
If a class is defined in a package, then the package statement should be in the first line of the source file.
If the source file contains an import statement, it should be placed between the package statement and the class definition. If there is no package statement, the import statement should be at the front of the source file.
The import statement and the package statement are valid for all classes defined in the source file. In the same source file, a different package declaration cannot be given to different classes.
A class has several access levels, and classes are divided into different types: abstract classes and final classes. These will be covered in a later section.
In addition to the several types mentioned above, Java also has some special classes, such as internal classes, anonymous classes.
A simple example
In this example, we created two categories of Employee and employeetest, placed in the package P1 and P2 respectively.
The employee class has four member variables, namely, name, age, designation, and salary. This class explicitly declares a construction method with only one argument.
In Eclipse, create a package named P1, create a class in the package, name Employee, and copy the following code to the source file:
Package P1;
public class employee{
String name;
int age;
String designation;
Double salary;
Constructor for employee class public
employee (String name) {
this.name = name;
}
Sets the value of age of public
void empage (int empage) {Age
= empage;
}
Set designation value public
void Empdesignation (String empdesig) {
designation = Empdesig;
}
Set salary value public
void Empsalary (double empsalary) {
salary = empsalary;
}
The output information is public
void Printemployee () {
System.out.println ("Name:" + name);
System.out.println ("Age:" + age);
SYSTEM.OUT.PRINTLN ("designation:" + designation);
System.out.println ("Salary:" + Salary);
}
Programs are executed from the main method. In order to run this program, you must include the main method and create an object.
The Employeetest class is given below, which creates two employee objects and invokes the value of the method settings variable.
Create a second package in eclipse named P2, create a class in the package, name Employeetest, and copy the following code to the source file:
Package P2;
Import p1.*;
public class employeetest{public
static void Main (String args[]) {
//Create two objects
employee Empone = new Employee (" James Smith ");
Employee Emptwo = new Employee ("Mary Anne");
Call the Member Method
Empone.empage (n) of the two objects;
Empone.empdesignation ("Senior Software Engineer");
Empone.empsalary (1000);
Empone.printemployee ();
Emptwo.empage ();
Emptwo.empdesignation ("Software Engineer");
Emptwo.empsalary ();
Emptwo.printemployee ();
}
To compile and run the Employeetest class, you can see the following output:
Name:james Smith
age:26
designation:senior Software Engineer
salary:1000.0
name:mary Anne
age:21
Designation:software Engineer
salary:500.0
Emphasize the programming style
Although the code style does not affect the operation of the program, but the readability of the program is very important. Write your own program to make others understand, first of all in typesetting to be very careful.
In fact, everyone's programming style, each software development company's programming style is different. A person writes the program code, should be able to let others understand, even after a long time, oneself also want to see understand, otherwise this program became a dead program.
Programming style refers to the format of the programming, so that the program looks very layered sense. Here are some examples to illustrate the importance of the programming style:
public class math{public
static void Main (string[] args) {
int x=12;
Double Y=12.3d;
void print () {
char a= ' a ';
System.out.println (a);
}
System.out.println (x+y);
}
Does the entire layout of the above program section look comfortable and has a strong sense of hierarchy? Does it look like you know the entire program architecture at a glance? The key here is indentation, and indentation can also be called a jump lattice.
The preceding code uses indentation: "public class math" is below, then the Mian () method indents 4 spaces, the code in the Mian () method indents all 8 spaces, and the print () method's main code indents more than 4 spaces. So the entire program's affiliation is very obvious. The Mian () method belongs to the math class, and the rest belongs to the main () method, and the code snippet within the print () method belongs to this method. The rule is that the code that has more space is less than the space code.
I recommend that you indent using the TAB key, and most editors, such as Eclipse, support the number of spaces for customizing the TAB key, typically 4 spaces.
In addition to indentation, blank lines are also necessary, first look at the following program code:
public class math{public
static void Main (string[] args) {
int x=12;
int y=23;
void print () {
//...}.... ...
}
void view () {
//...........}}}
In the above program section, there is a blank line between the print () method and the view () method to differentiate between different modules. The print () method is different from the view () method, so separating them with empty rows increases the readability of the program.
In addition, you need to be aware of the name of the method or property. These names should have meaning, preferably regular, do not use only "a", "B" such a common variable, appropriate can be based on the function of the variable or function to name it. "Print" above, other programmers know this method at a glance, is about printing or output functions. Another example: variable name "Name", a look at the name of the variable is known. Therefore, must be named meaningful, otherwise the program's readability is not strong.
There is also a point about the annotation. Next to the method name of each method, you should add some comments and a simple description of how the program functions and how it operates after a program completes.
As long as you do the above, this program is easy for others to read. Even after a long time, read the program will be at a glance.