NICE User Manual (4)

xiaoxiao2021-03-06  27

Parametric classes

There is a very powerful feature in Nice to define a parameter class. The parameter class is very similar to the template in C or models in other function types. The programming for parameter classes is sometimes referred to as generic programming.

The parameter class is a class with parameters, in which case the parameters are more like a type instead of a value. You can think of the parameter class as a family-related class, which have the same behavior and structure in addition to the parameterized part. Parameter class is often used in the data structure.

Example 3.4 Simple Java Collection

Class stack {

List contents = new linkedList ();

Void Push (Object O) {

Contents.Add (o);

}

// ... Omitted Methods

Public static void main (String [] args) {

Stack st = new stack ();

St.Push ("Test");

Integer Num = (Integer) st.pop (); // Runtime Error

}

}

Here is a very large security hazard. I will put a string, and try to pop into an Integer, which will cause a runtime error. The parameter class can solve this problem.

Example 3.5 Simple Nice Collection

Class Stack {

List Contents = new LinkedList ();

Void push (t t) {

Contents.add (t);

}

// ... Omitted Methods

}

Void main (string [] args) {

Stack St = New Stack ();

St.Push ("Test");

Integer num = st.pop (); // compile time error!

}

In the collection of Nice, we have a T type parameter class Stack. In fact, Stack is a signal to the compiler that tells the compiler how to create a Stack for a given type. When the compiler knows that we will only put the String in this stack, any code that takes out from Stack will cause the compiler to report error.

Interface declaration

Nice and Java or C # are only allowed to inherit, which means that any class can only inherit from one superclass. Sometimes, a class is "inherited" from two (or more) classes, from each of its parent classes, which may be better. In NICE, this situation can be implemented using an interface like Java.

The interface declaration is like a class, but the interface can only contain methods without including data members. Unlike Java, the interface in Nice can also contain the default implementation of the method, so that it is more convenient than the interfaces in Java.

Although it is said that it is still aware of Java's model. One interface in Nice does not really contain anything, it is just a tag. Just like the java.io.serializable interface, just a tag, used to inform Java to serialize a class's instance. All interfaces in Nice are tags because of the multi-way methods in Nice. Multi-way methods are defined by themselves without including in any class or interface. You can add a new method to the interface at any time, just like adding a new method to the class at any time. Another result of Nice is based on a multi-method is that the interface is not only the "method signature" method "," also "contains the specific implementation of the method.

NICE can define an interface like Java, just like the example below:

Example 3.6 Defining Interface

Interface complent {

String getId (); (int, int) getPosition ();

(int, int) getDimensions ();

Int getarea () {

Int width, int Height = this.getdimensions ();

Return Width * Height;

}

}

Of course, you can use the following method to define the interface:

Example 3.7 In the interface definition contains global methods

Interface component {}

String GetId (Component Comp);

(int, int) getPosition; Component CoMP;

(int, int) getDimensions; Component CoMP;

INT getarea (Component Comp) {

Int width, int Height = comp.getdimensions ();

Return Width * Height;

}

In fact, mixing these two forms are completely unpleasant, and the method can be some defined in the interface block (block), and others are defined outside the interface block. A better approach is to define the method without the default implementation inside the interface, and the default implementation is defined outside the interface. This will clearly know how the methods must be implemented when others read the code. Of course, the compiler guarantees that all necessary methods are implemented, so the above content is just a suggestion.

Enumeration

Enumeration class (or simply eNUM) is a set of associated constants. Many language supports a simple constant, of course, Nice is no exception. Some digital constants are used in many programs to indicate special meaning. For example, a method in a vending machine is like this:

Let Int nickel = 5;

Let Int Dime = 10;

Let int quart = 25;

Class VendingMachine {

INT Change = 0;

}

Void AddCoin (VendingMachine Machine, Int Cents) {

Machine.change = CENTS;

}

But this method is not safe! It can accept any number of change, including some ridiculous numbers like 3 or 234320. A method of processing this problem is to check at runtime to ensure that data can be accepted, and throw an exception when unacceptable data. However, there is a simpler solution in Nice: enumeration.

Enum class-name [(Parameter-type parameter-name, ...)] {

Option, ...

}

Enumeration can be like this simple symbol:

ENUM color {red,

Orange

, Yellow, Blue, Indigo, ViOLET}

Or they can include the value of integer (or other):

ENUM VENDINGCOIN (INT VALUE) {

Nickel (5), DIME (10), Quarter (25);

}

Class VendingMachine {

INT Change = 0;

}

Void Addcoin (VendingMachine Machine, VendingCoin CENTS) {

Machine.change = CENTS.VALUE;

}

Of course, a real automatic vending opportunity retains the actual quantity of each coin, not just their sum!

转载请注明原文地址:https://www.9cbs.com/read-40577.html

New Post(0)