Home >Java >javaTutorial >How to write java class

How to write java class

(*-*)浩
(*-*)浩Original
2019-05-22 19:36:2011077browse

Steps to write a java class: 1. Use the class keyword to declare the class, the syntax is "public class class name {//member variables and methods of the class}"; 2. Add member variables, the syntax is "access modifier Data type member variable name;"; 3. Add a method. The method consists of access modifier, return value, method name, parameter list, and method definition.

How to write java class

#If you want to know how to write a Java class, you must first know the basic structure of the class.

Basic structure of the class

Attributes: Description of object data

Method: Behavior of object

Construction method: used Instantiated object

block: divided into static block and instance block

Class declaration: (Access permission modifier public.default (can be ignored and not written, it is the default)) (Modifier final. abstract.synchronized) class class name { class body }

The role of the class: a class is a template that defines common properties and methods for multiple objects. For example: student class (Zhang San, Li Si) mobile phone class (Huawei .oppo)

A class is a collection of entities with certain common characteristics. It is an abstract data type, which is an abstraction of entities with the same characteristics. In object-oriented programming languages, a class is an abstraction of the properties and behavior of a type of "thing". Give an example to illustrate the following class. For example, Person (person) is a class, then a specific person "Zhang San" is the object of the "human" class, and information such as "name, height, weight" are the attributes of the object. Person Actions such as "eating, dressing", etc. are methods of objects. In short, a class is a collection of things with the same characteristics, and an object is a specific instance of the class.

Steps to create a class in java:

1. Declare a Java class

A class in Java is declared with the keyword class. A Java source code file can only contain one public class. The file name must be the same as the class name of the public class, and the file extension is ".java".

The member variables and methods of a class appear within the braces of the class declaration. The following code shows a simple class that has not declared member variables and methods. Its file name is Employee.java:

public class Employee {
 

}

2. Add member variables to the class

The properties of the object become Member variables in related classes. A member variable in a class consists of the following parts:

  • Access modifier. Can be public, private, or protected; if the access modifier is omitted, the default access modifier will be used.

  • type of data.

  • Member variable name. The member variable name must be a valid identifier, followed by a semicolon.

About access modifiers, we will discuss them in detail in later chapters. Here, we all use the public access modifier. What must be remembered is that if we specify the public modifier on a member variable or method in a class, then the member variable or method will be accessible by any other object.

The following class Employee has five member variables: name, address, number, SSN and salary. When an Employee object is initialized in memory, the system allocates memory for these five member variables:

public class Employee {
public String name;     //姓名
public String address;     //邮寄地址
public int number;     //员工号
public int SSN;          //社保号
public double salary;     //员工的工资
}

Must remember: the class describes what the object looks like. The Employee class is used to describe employees in a company. The member variables appearing in the Employee class represent information about the employees whose compensation needs to be calculated.

For example, an employee has a name and address, so the Employee class has a name member variable and an address member variable.

Each employee also has other attributes, such as height, weight, etc. However, these programs are not related to the example program for calculating wages, so we will not include these properties. If we were to use the employee class for other purposes, it might be very different from the current class.

3. Add methods to the class

The behavior of an object becomes a method in the related class. A method in a class typically consists of the following parts:

  • Access modifier

  • Return value

  • Method name, must be a valid identifier

  • Parameter list, appears in brackets

  • Definition of method

In Java, the definition of a method (often called the method body) must appear within curly braces after the method declaration. We will discuss how to write and call class methods in Chapter 5, "Methods."

The following class demonstrates the method declaration method by adding two methods to the Employee class:

/* 代码清单4.1 Employee.java
  Employee类代码,代表公司员工
*/
public class Employee {
  public String name;            
  public String address;     
  public int number;
  public int SSN;
  public double salary;

  public void mailCheck() {
    System.out.println("邮寄支票到" + name  + ",地址为:\n" + address);
  }

  public double computePay() {
    return salary/52;
  }
}

This Employee class is only used to demonstrate how to add methods to the class, so the method Implementation is relatively simple. For example, the mailCheck() method just prints out the employee's name and address to send the check, and the computePay() method just divides the employee's salary by 52 (assuming the salary is the annual salary).

Member variables of the class can be accessed in the method of the class. We noticed that in the Employee class, the mailCheck() method prints the employee's name and mailing address using the name and address member variables of the class. Similarly, the computePay() method needs to access the salary member variable.

Finally, we can see that the Employee class contains the following content:

  • The name of the class is Employee.

  • The class has five public member variables.

  • The class has two public methods.

The Employee class appears in the form of the file name Employee.java, and the compiled bytecode appears in the form of the file name Employee.class.

The above is the detailed content of How to write java class. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn