Classes are the basic elements that make up Java programs and are an important composite data type in Java. It encapsulates the state and method of a class of objects, and is the prototype of this class of objects. The implementation of a class consists of two parts: class declaration and class body, basic format:
Class <class Name>
{
Property
Method
}
Where class is the keyword used to define classes. "Class <class name>" is the declaration part of a class, the class name must be a valid Java identifier, and the contents of the braces and between are the class body.
Syntax for class declarations:
[Public] [Abstract|final] class ClassName
[Extends Superclassname] [Implements Interfacenamelist]
{
...
}
Where the modifier public,abstract,final describes the properties of the class, classname the class name, Superclassname is the name of the parent class, and interfacenamelist the list of interfaces that the class implements. The first letter in the customary class name is uppercase, but this is not required. The naming convention for class names adheres to the naming conventions of Java variables. In addition, when a class name is compounded by multiple words, the first letter of each word is capitalized to differentiate. such as: Beijingtime.
Class Body definition syntax:
The purpose of writing classes is to describe the properties and functions of a class of things, and the process of describing them is implemented by the class body. The content of a class body consists of two parts: (1) The definition of a variable, which describes the definition of the property (2) method, which is used to characterize the function.
Class ClassName
{
[Public|protected|private|static|final] [Transient] [volatile] type variableName; Definition of member variables
[Public|protected|private] [Static] [Final | Abstract] [native] [synchronized]
ReturnType methodName ([paramlist]) [throws excepttionlist]//syntax for member methods
}
The following is a class of employee. Two variables are defined in the class body, one is a name variable of type string and one is a salary variable of type double. The method definition section describes the GetName () method of the name variable.
public class employee{
Private String name;//Variable definition
private double salary;
Public Employee (String n,double s) {//constructor
Name=n;
Salary=s;
}
Public String GetName () {//getname () method
return name;
}
}
Java classes and Objects (ii)