我想了解一些构造函数链,我知道调用来自同一类的其他构造器的构造被称为构造函数链,混淆构造函数链
当我们使用这个建筑工中,我们实际上调用我们已经定义的另一个构造函数,但我想了解该计划是怪异,
public AESCipher(Key key) {
this(key.getEncoded());
}
/**
* Create AESCipher based on existing {@link Key} and Initial Vector (iv) in bytes
*
* @param key Key
*/
public AESCipher(Key key, byte[] iv) {
this(key.getEncoded(), iv);
}
/**
* <p>Create AESCipher using a byte[] array as a key</p>
* <p/>
* <p><strong>NOTE:</strong> Uses an Initial Vector of 16 0x0 bytes. This should not be used to create strong security.</p>
*
* @param key Key
*/
public AESCipher(byte[] key) {
this(key, INITIAL_IV);
}
private AESCipher(byte[] key, byte[] iv) {
try {
this.secretKeySpec = new SecretKeySpec(key, "AES");
this.iv = new IvParameterSpec(iv);
this.cipher = Cipher.getInstance(ALGORITHM_AES256);
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
throw Throwables.propagate(e);
}
}
在第一个构造函数,用这条线this(key.getEncoded())
,其构造是该行打来的?在它之前没有构造函数,实际上没有带一个参数的构造函数。
,并在第三构造,有byte[]
类型的只是一个说法,这就是发生的事情里面,
this(key, INITIAL_IV);
它链接有两个参数,key
类型之一的构造函数键入byte[]
,所以它链接这个构造函数AESCipher(Key key, byte[] iv)
,这很好,但仍然...在第一个构造函数中发生了什么,以及为什么最初需要4个构造函数。
P.S:我只是没有发布这个问题,我花了4小时试图了解发生了什么,但它太混乱了。
下面是完整的代码,
[Key#getEncoded()](https://docs.oracle.com/javase/7/docs/api/java/security/Key.html#getEncoded%28%29)。 '事实上没有一个参数的构造函数。“当然?我可以看到两个带有一个参数的构造函数。 – Tom 2014-11-20 23:24:25
我猜'Key#getencoded()'返回一个'byte []'。因此,'this(key.getencoded())'在你显示的代码中调用第三个构造函数。 – 2014-11-20 23:25:02