New features of J2SE1.5 (2)

zhaozj2021-02-17  48

New features of J2SE1.5 (2)

What is the advantage of type safety enumerations than previous enumerations?

There is a feature:

Provide security checks for compile time INT enumerations, and no longer provide additional types of security checks.

Provide enumeration namespace

They can be placed directly in the collection.

Because they are essentially class, you can add properties and methods to the inside.

These features are really good. Can I talk about the characteristics of type security and the characteristics of type safety of type safety?

In general, the above features are simple from semantics. Look at the example below, and the statement of C / C enumeration is very similar:

ENUM season {winter, spring, summer, fall}

Although the declaration is similar, it makes the compiler realize many of the features mentioned above. You can also use Season to use the Switch's judgment statement.

Please exemplify the advantages of "enumeration of type security".

Here is an example of a type of enumerated per minute.

Public enum coin {

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

COIN (int value) {this.value = value;

PRIVATE FINAL INT VALUE

Public int value () {return value;}

}

This is a pioneering. We define Value as a common variable that reads Coin. In the enumerated constructor, you can initialize it when declaring the instance.

Let's take a look at the example of further using this enumeration.

I don't do it. The following program prints a table and the size and color of Coin.

Public class cointest {

Public static void main (String [] args) {

For (COIN C: coin.values)

System.out.println (C ": / T"

C.Value () "¢ / t" color (c));

}

Private Enum Coincolor {Copper, Nickel, Silver}

Private static coincolor color (coin c) {

Switch (c) {

Case coin.penny: return corncolor.copper;

Case coin.nickel: Return Coincolor.nickel;

Case coin.dime:

Case coin.quarter: returnoColor.silver;

DEFAULT: Throw new assertionerror ("Unknown Coin:" C);

}

}

}

Gang. So what is the help of static introduction Static Import?

First he avoids the static member of the programmer to use the prefix. An alternative approach to previously common is:

// "constant interface" Antipattern - Not recommended practices

Public interface physics {

PUBLIC STATIC FINAL DOUBLE AVOGADROS_NUMBER = 6.02214199E23;

Public Static Final Double Boltzmann_Constant = 1.3806503E-23;

Public Static Final Double Electron_Mass = 9.10938188E-31;

}

Public class guacamole imports physics {

Public static void main (String [] args) {

Double Moles = ...; Double Molecules = AvogAdros_Number * Moles;

...

}

}

The above practices have reached the effect, but it violates some design principles. The interface is used to define methods and types, not to provide constant declarations. Moreover, it is also exposed by the constant used by GuAcamole to the client.

Static introduction features provide a simple implementation. This feature is similar to the IMPORT function of the package.

Import static org.iso.physics. *;

Class Guacamole {

Public static void main (String [] args) {

Double Molecules = AvogAdros_Number * Moles;

...

}

}

Understand, what is the function of metadata type Metadata?

Using Metadata and third-party tool providers allow programmers to have their own days.

Previously many published APIs need a lot of description information. For example: Defining a JAX-RPC Network Service API you need to provide its interface and implementation classes. as follows:

Public interface coffeeorderif extends java.rmi.remote {

Public coffee [] getpricelist ()

Throws java.rmi.remoteexception;

Public String ORDERCOFEE (String Name, int Quantity)

Throws java.rmi.remoteexception;

}

Public Class CoffeeOrderImpl Implements CoffeeOrderif {

Public coffee [] getpricelist () {

...

}

Public String ORDERCOFEE (String Name, Int Quantity) {

...

}

}

Using metadata features, you can save a lot of work. What you need to do is to add a special annotation on your code. Have you used the development tool that use these comments to automatically generate related generations. As follows:

Import javax.xml.rpc. *;

Public class coffeeorder {

@Remote public coffee [] getpricelist () {

...

}

@Remote Public String ORDERCOFFEE (String Name, INT Quantity) {

...

}

}

It's really very good, but we can't define all properties and comments?

Yes, JSR-175 now provides a Framework to let third parties define attributes and related tools. After full text ============== Some local translations are not accurate, I hope everyone is forgiveness accesine@163.com ==============

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

New Post(0)