當(dāng)類實(shí)現(xiàn)接口時(shí)笑跛,接口就充當(dāng)可以引用這個(gè)類的實(shí)例的類型(type)亿卤。因此類實(shí)現(xiàn)了接口驱敲,就表明客戶端可以對(duì)這個(gè)類的實(shí)例實(shí)施某些動(dòng)作。為了其他目的而定義接口是不恰當(dāng)?shù)摹?/p>
常量接口
有一種接口被稱為常量接口(constant interface)赐俗,他不滿足上面的條件拉队。這種接口沒有包含任何方法,他只包含靜態(tài)的final域阻逮,每個(gè)域都到處一個(gè)常量粱快。使用這些常量的類實(shí)現(xiàn)這個(gè)接口,以避免用類名來修飾常量名叔扼。
public interface PhysicalConstants {
//阿伏伽德羅數(shù)
static final double AVOGADROS_NUMBER = 6.02214199e23;
//玻爾茲曼常數(shù)
static final double BOLRZMANN_CONSTANT = 1.3806503E-23;
//電子質(zhì)量
static final double ELECTRON_MASS = 9.10938188E-31;
}
public class test implements PhysicalConstants {
public static void main(String[] args) {
Double d = 11.11;
if(d.equals(AVOGADROS_NUMBER)){
System.out.println(d);
}
}
}
阿伏伽德羅數(shù)事哭,其具體的數(shù)值是6.0221367×1023,這個(gè)常數(shù)可用很多種不同的實(shí)驗(yàn)方法進(jìn)行測(cè)定瓜富。
12.000克C中所含碳原子的數(shù)目,因意大利化學(xué)家A.阿伏伽德羅而得名鳍咱,具體的數(shù)值是6.0221367×10。包含阿伏伽德羅數(shù)個(gè)微粒的物質(zhì)的量是 1摩爾与柑。例如1摩爾鐵原子谤辜,質(zhì)量為 55.847 克,其中含 6.0221367×10個(gè)鐵原子仅胞;1摩爾水分子的質(zhì)量為18.010克,其中含6.0221367×10個(gè)水分子;1摩爾鈉離子含6.0221367×10個(gè)鈉離子;1摩爾電子含6.0221367×10個(gè)電子剑辫。
** 常量接口模式是對(duì)接口的不良使用干旧。** 類在內(nèi)部使用某些常量,這純粹是實(shí)現(xiàn)細(xì)節(jié)妹蔽。實(shí)現(xiàn)常量接口椎眯,會(huì)導(dǎo)致把這樣的實(shí)現(xiàn)細(xì)節(jié)泄露到該類的導(dǎo)出API中。類實(shí)現(xiàn)常量接口胳岂,這對(duì)于用戶來講并沒有什么價(jià)值编整。實(shí)際上,這樣做反而會(huì)使他們更加糊涂乳丰。更糟糕的是掌测,他代表了一種承諾:如果將來的發(fā)行版本中,這個(gè)類被修改了产园,他不再需要使用這些常量了汞斧,他依然必須實(shí)現(xiàn)這個(gè)接口,以確保二進(jìn)制兼容性什燕。如果非final類實(shí)現(xiàn)了常亮接口粘勒,他的所有子類的命名空間也會(huì)被接口中的常量所“污染”。
在java平臺(tái)類庫中有幾個(gè)常量接口屎即,例如java.io.ObjectStreamConstants庙睡。這些接口應(yīng)該被認(rèn)為是反面的典型事富,不值得被效仿。
package java.io;
/**
* Constants written into the Object Serialization Stream.
*
* @author unascribed
* @since JDK 1.1
*/
public interface ObjectStreamConstants {
/**
* Magic number that is written to the stream header.
*/
final static short STREAM_MAGIC = (short)0xaced;
/**
* Version number that is written to the stream header.
*/
final static short STREAM_VERSION = 5;
/* Each item in the stream is preceded by a tag
*/
/**
* First tag value.
*/
final static byte TC_BASE = 0x70;
public class ObjectInputStream
extends InputStream implements ObjectInput, ObjectStreamConstants
{
/** handle value representing null */
private static final int NULL_HANDLE = -1;
/** marker for unshared objects in internal handle table */
private static final Object unsharedMarker = new Object();
/** table mapping primitive type names to corresponding class objects */
private static final HashMap<String, Class<?>> primClasses
= new HashMap<>(8, 1.0F);
(省略若干行)
/**
* The readStreamHeader method is provided to allow subclasses to read and
* verify their own stream headers. It reads and verifies the magic number
* and version number.
*
* @throws IOException if there are I/O errors while reading from the
* underlying <code>InputStream</code>
* @throws StreamCorruptedException if control information in the stream
* is inconsistent
*/
protected void readStreamHeader()
throws IOException, StreamCorruptedException
{
short s0 = bin.readShort();
short s1 = bin.readShort();
if (s0 != STREAM_MAGIC || s1 != STREAM_VERSION) {
throw new StreamCorruptedException(
String.format("invalid stream header: %04X%04X", s0, s1));
}
}
如果要導(dǎo)出常量乘陪,可以有幾種合理的選擇方案统台。
- 如果這些常量與某個(gè)現(xiàn)有的類或者接口緊密相關(guān),就應(yīng)該把這些常量添加到這個(gè)類或者接口中暂刘。例如饺谬,在java平臺(tái)類庫中所有的數(shù)值包裝類,比如Integer和Double谣拣,都導(dǎo)出了MIN_VALUE和MAX_VALUE常量募寨。
public final class Integer extends Number implements Comparable<Integer> {
/**
* A constant holding the minimum value an <code>int</code> can
* have, -2<sup>31</sup>.
*/
public static final int MIN_VALUE = 0x80000000;
/**
* A constant holding the maximum value an <code>int</code> can
* have, 2<sup>31</sup>-1.
*/
public static final int MAX_VALUE = 0x7fffffff;
- 如果這些常量最好被看做枚舉類型的成員,就應(yīng)該用枚舉類型(enum type)(見30條)來導(dǎo)出這些常量森缠。
- 使用不可實(shí)例化的工具類(utility class)(見4條)來導(dǎo)出這些常量
public class PhysicalConstants {
private PhysicalConstants(){}
//阿伏伽德羅數(shù)
public static final double AVOGADROS_NUMBER = 6.02214199e23;
//玻爾茲曼常數(shù)
public static final double BOLRZMANN_CONSTANT = 1.3806503E-23;
//電子質(zhì)量
public static final double ELECTRON_MASS = 9.10938188E-31;
}
工具類通常要求客戶端要用類名來修飾這些常量名拔鹰,例如PhysicalConstants.AVOGADROS_NUMBER。如果大量利用工具類導(dǎo)出的常量贵涵,可以通過利用靜態(tài)導(dǎo)入(static import)機(jī)制列肢。避免用類名來修飾常量名,不過靜態(tài)導(dǎo)入機(jī)制是在java發(fā)行版本1.5中才引入的:
package example;
import static example.PhysicalConstants.*;
/**
* Created by Jiang Meiwei on 2017/5/7.
*/
public class test {
double atoms(double mols){
return AVOGADROS_NUMBER * mols;
}
}
總結(jié):
簡(jiǎn)而言之宾茂,接口應(yīng)該只被用來定義類型瓷马,他不應(yīng)該被用來導(dǎo)出常量。