Chapter 3 Class and Interface
Class declaration
Data member
A class of the most important components make it a list of data members. Data members are variables attached to each class instance, which have types, names, and initial values. The statement of the data member is as follows:
TYPE FIELD-NAME [= INITIAL-value];
If it is not given the default value, then each call constructor must give this member to a specified value. If it has the default value, the call to the constructor overwrites that value, in which case the default value will not be calculated (this will become important when the calculation will bring side effects).
Constructor
The class in NICE has a default (or "automatic") constructor generated by the compiler. This constructor allows all event members in the class to be explicitly initialized, but if a data member already has a default value when a data is declared, it will not appear in the constructor but use the default value. In most cases, this default constructor can already meet the needs, it will be liberalized from trouble, just the received value and deliver them into the code of the data member.
Example 3.1 Declaration and creation class with the default constructor
Class car {
String brand;
String model;
INT NUMBEROFWHEELS = 4;
INT NUMBEROFDRIVINGWHEELS = 2;
}
Void test () {
Car renault5 = new car (BRAND: "Renault", Model: "CINQ");
Car Jeep = New Car (Brand: "Jeep", Model: "Some Jeep", NumberofDrivingWheels: 4);
}
This is important to include the name of the data member when calling the constructor. First, this is easy to understand what the parameters represent don't have to view the class definition. Second, it is possible to never use the data member to disconnect 1. (1 in Java When the order of constructor parameters must be changed, there is a problem. Do you have to modify all call points, and this modification will lead to cumbersome work and errors in the best case, and in worst cases It is impossible to implement (when writing a library). When the order changes, only some of the call points are modified, if the parameter type of the exchange is inconsistent, the compilation cannot be passed; or the compilation can pass but run The result of errors will be generated. There is no simple method in Java to solve this problem. In NICE, the name is a solution when the constructor is called.) Because the name of the data is explicit, it can be Use any order.
When more control is required in the constructor, a new constructor can be written to replace the automatic constructor. The writing of new constructors is very similar to other methods, but there is a minor difference in the statement:
New class-name (param-typeparam-name [= initial-value], ...) {
Method-body;
this (argument, ...);
}
The last statement in any custom constructor is required in the syntax, and other constructors in the same type must be called, and in most cases, the automatic constructor is called.
Example 3.2 Using a custom constructor to define and create configuration classes
/ **
* Class Which Encapsulates A Strategy for
* Way of translating a character.
* /
Class translator {
// the function That actually Performs the translation
Char-> char transfunction;
// Convenience Method
CHAR Translate (Char INPUT) {Return (THIS.TRANSFUNCTION) (INPUT);
}
}
/ **
* Constructor Which Takes a Map of Characters, And
* Returns a Translator Which Looks Up The Input
* Character in the map, or just returns the input
* CHARACTER if it's not in the map.
* /
New Translator (Map
THIS (Transfunction: Char C => Characters.Get (C) || C);
}
// a Translator Which provides rot13 ciphering.
// Uses automatic constructor.
Var Translator Rot13 = New Translator (Transfunction: CHAR C => Char (int (c) 13));
// a Translator Which Just Changes 's' or 's' to '$'.
// Uses Custom Constructor.
Var Translator Stodollar = New Translator (Characters: ListtTomap ([('s', '$'), ('s', '$')]));