Write a Java class

1 to write a Java class

Java classes in the keyword class declaration. Java source code files can contain only a public (public) class, the file name must be the same as the class name public class, the file extension ".java".

 

 

 

Member variables and methods of a class appear in the class declaration in braces. The following code shows a simple class that has not yet declare member variables and methods, the file name Employee.java:

 

public class Employee {

 

}

2 added to the class member variables

Become the property of the object member variable class. A class member variable by the following components:

  • Access modifier. It may be public, private, or protected; if access modifier is omitted, the default access modifiers.
  • type of data.
  • Member variable name. Member variable name must be a valid identifier, back end with a semicolon.

About access modifier, we will discuss in detail in a later section. Here we are with the public access modifier. It must be remembered that, if we specify the public modifier to a member variable or method in the class, then the member variable or method can be accessed by any other object.

The following five members of the class Employee variables: name, address, number, SSN and salary. When an Employee object initialized in memory, the system will allocate memory for the five member variables:

 

public class Employee {

public String name; // Name

public String address; // mailing address

public int number; // number of employees

public int SSN; // Social Security number

public double salary; // wages

}

 

 

It must be remembered: What class describes the object looks Yes. Employee class is used to describe the company's employees. On behalf of the Employee class member variables that appear in the information required to calculate employee remuneration.

For example, employees have a name and address, so there is an Employee class member variable name and address a member variable.

Each employee has other attributes such as height, weight and so on. However, these procedures independent of the sample program to calculate wages, so we will not include these attributes come in. If we want employees like for other purposes, then it probably is very different with the current class.

3 the method of adding the class

A behavior of an object to be method-related class. A class method typically consists of the following components:

  • Access modifier
  • return value
  • The method name must be a valid identifier
  • Parameter list, appear in parentheses
  • Defined methods

In the definition of Java, the method (commonly referred to as the method body) must appear in brackets after the method declaration. We will call the class how to write and methods discussed are discussed in Chapter 5, "Methods".

The following two methods by adding to the class Employee class, method declarations presentation method:

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  

/ * Listing 4.1 Employee.java

  Employee class code that represents employees

*/

 

public class Employee {

  public String name;            

  public String address;     

  public int number;

  public int SSN;

  public double salary;

 

  public void mailCheck() {

    System.out.println ( "sending a check to the" + name + ", the address is: \ n" + address);

  }

 

  public double computePay() {

    return salary/52;

  }

}

The Employee class is only used to demonstrate how to add a method to the class, so the implementation of the method is relatively simple. For example, the method mailCheck () just prints out the name and address of the employee to send a check, the method computePay () simply divided by the employees' wages 52 (assuming that salary is the annual salary).

在类的方法中可以访问该类的成员变量。我们注意到在Employee类中,mailCheck()方法打印员工的姓名和邮寄地址要使用该类的name和address成员变量。同样,computePay()方法要访问salary成员变量。

最后,我们可以看到Employee类中包含如下内容:

  • 类的名称是Employee。
  • 类有五个public成员变量。
  • 类有两个public方法。

Employee类以文件名Employee.java形式出现,编译的字节码以文件名Employee.class形式出现。

Guess you like

Origin blog.csdn.net/qq_45097560/article/details/90757069