JAVA class and object (2) ---- class definition basics, java ----
Classes are the basic elements of java programs and an important composite data type in java. It encapsulates the states and methods of a class of objects, and is the prototype of this class of objects. The implementation of a class includes two parts: class declaration and class body. The basic format is as follows:
Class <class name>
{
Attribute
Method
}
Class is a keyword used to define a class. "Class <class name>" is the declaration part of the class. The class name must be a valid java identifier and the content between braces is the class body.
Class declaration Syntax:
[Public] [abstract | final] class ClassName
[Extends superClassName] [implements interfaceNameList]
{
...
}
The modifiers public, abstract, and final indicate the class attributes. ClassName indicates the class name, superClassName indicates the name of the parent class, And interfaceNameList indicates the list of interfaces implemented by the class. Traditionally, the first letter of the class name is in uppercase, but this is not necessary. The naming rules of class names must comply with the naming rules of java variables. In addition, when a class name is composed of multiple words, the first letter of each word is capitalized for differentiation. For example, BeijingTime.
Class Definition Syntax:
The purpose of writing a class is to describe the attributes and functions of a class of things. The description process is implemented by the class body. The content of the class body consists of two parts: (1) definition of variables, used to characterize attributes (2) Definition of methods, used to portray functions.
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 javasttionlist] // syntax of the member Method
}
The following is an Employee class. Two variables are defined in the class body. One is the name variable of the String type and the other is the salary variable of the double type. 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;
}
}