Declaration rules for Java source files <br />When defining multiple classes in a source file and there are import statements and package statements, pay special attention to these rules:
There can only be one public class in a source file.
A source file can have multiple non-public classes.
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, so the source file should be named Employee.java.
If a class is defined in a package, the package statement should be on 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 package statement are valid for all classes defined in the source file. In the same source file, different package declarations cannot be given to different classes.
Classes have several access levels, and classes are also divided into different types: abstract classes and final classes, etc. These will be introduced in subsequent chapters.
In addition to the types mentioned above, Java also has some special classes, such as internal classes and anonymous classes.
A simple example
In this example, we create two classes Employee and EmployeeTest, which are placed in packages p1 and p2, respectively.
The Employee class has four member variables, name, age, designation and salary. This class explicitly declares a constructor that has only one parameter.
In Eclipse, create a package named p1, create a class in the package named Employee, and copy the following code into the source file:
package p1;public class Employee{ String name; int age; String designation; double salary; // Constructor of Employee class public Employee(String name){ th is.name = name; } // Set the value of age public void empAge( int empAge){ age = empAge; } // Set the value of designation public void empDesignation(String empDesig){ designation = empDesig; } // Set the value of salary public v oid empSalary(double empSalary){ salary = empSalary; } // Output Information public void printEmployee(){ System.out.println("Name:"+ name); System.out.println("Age:" + age); System.out.println("Designation:" + de signation ); System .out.println("Salary:" + salary); }}The program starts with the main method. In order to run this program, you must include the main method and create an object.
The following is the EmployeeTest class, which creates two Employee objects and calls the method to set the value of the variable.
Create another package in Eclipse, name it p2, create a class in the package, name it 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 Smi th"); Employee empTwo = new Employee("Mary Anne "); // Call the member method of these two objects empOne.empAge(26); empOne.empDesignation("Senior Software Engineer"); empOne.empSalary(1000); empOne.printEmployee(); empTw o.empAge(21) ; empTwo.empDesignation("Software Engineer"); empTwo.empSalary(500); empTwo.printEmployee(); }} Compile and run the EmployeeTest class and you can see the following output results:
Name:James SmithAge:26Designation:Senior Software EngineerSalary:1000.0Name:Mary AnneAge:21Designation:Software EngineerSalary:500.0
Emphasize the programming style <br />Although the code style does not affect the operation of the program, it is very important to the readability of the program. The programs you write yourself should be understood by others, and you should pay great attention to the typesetting first.
In fact, everyone's programming style and every software development company's programming style are different. The program code written by a person should be able to be understood by others, and even after a long time, you must understand it yourself, otherwise the program will become a dead program.
Programming style refers to the format during programming, which makes the program look very layered. Here are some examples to illustrate the importance of 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); } Sy stem. out.println(x+y); }}Does the entire layout of the above program block look very comfortable and has a strong sense of layering? Do you know the entire program architecture at a glance? The key here is indentation, which can also be called jump grid.
The indentation used by the above code: "public class math" is the top grid, then the mian() method is indented into 4 spaces, and the code in the mian() method is indented into 8 spaces, and the body of the print() method The code indents 4 more spaces. In this way, the relationship between the entire program will be very obvious. The mian() method belongs to the math class, and the rest belongs to the main() method, and the code segments inside the print() method belong to this method. The rule is that code with more spaces belongs to code with less spaces.
I recommend that you use the tab key to indent. Most editors (such as Eclipse) support customizing the number of spaces of the tab key, which is generally 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(){ // .............. } }}The above program block has blank lines between the print() method and the view() method, which is used to distinguish different modules. The print() method does not perform the same functions as the view() method, so use blank lines to separate them, which increases the readability of the program.
In addition, it is important to note that the naming of methods or attributes. These names should have meanings, and it is best to be regular. Do not just use general variables such as "a" and "b". They can be named according to the functions of the variable or function. The above "print" is known at a glance by other programmers, which is a function related to printing or output. For example: the variable name "name", you can tell at a glance that it is a variable related to the name. Therefore, the naming must be meaningful, otherwise the program will not be readable.
Another point is explained by attention. Next to the method name of each method, some comments should be added. At the same time, after a program is completed, a simple description of the function and how to operate the program is.
As long as you do the above points, it will be easy for others to read this program. Even if you read the program for a long time, you will be clear at a glance.