类定义以关键字class开头,后跟类名。类的身体被一对大括号包围。
以下是语法-
<access specifier> class class_name {
// member variables
<access specifier> <data type> variable1;
<access specifier> <data type> variable2;
...
<access specifier> <data type> variableN;
// member methods
<access specifier> <return type> method1(parameter_list) {
// method body
}
<access specifier> <return type> method2(parameter_list) {
// method body
}
...
<access specifier> <return type> methodN(parameter_list) {
// method body
}
}以下是类名称的约定-
类名的编码约定是类名的名称,例如,应为PascalCasing-
public class CalculateCost {
}上面,类名称CalculateCost在PascalCasing中。
优先添加类名称作为名词或名词短语-
public class Department {
}