rust的枚举多态
父类的定义
pub enum XMSSAddress{
HashTreeAddress(HashTreeAddress),
LTreeAddress(LTreeAddress),
OTSHashAddress(OTSHashAddress),
}
用匹配表达式判断父类对象是那个子类:
let resultAddress=match address {
XMSSAddress::LTreeAddress(tempAddress)=>{
address=XMSSAddress::LTreeAddress(crate::XMSS::LTreeAddress::LTreeBuilder::new1().withLayerAddress(tempAddress.getLayerAddress())
.withTreeAddress(tempAddress.getTreeAddress()).withLTreeAddress(tempAddress.getLTreeAddress())
.withTreeHeight(tempAddress.getTreeHeight()).withTreeIndex(tempAddress.getTreeIndex())
.withKeyAndMask(0).build());
address
},
XMSSAddress::HashTreeAddress(tempAddress)=>{
address = XMSSAddress::HashTreeAddress(crate::XMSS::HashTreeAddress::HashTreeBuilder::new1().withLayerAddress(tempAddress.getLayerAddress())
.withTreeAddress(tempAddress.getTreeAddress()).withTreeHeight(tempAddress.getTreeHeight())
.withTreeIndex(tempAddress.getTreeIndex()).withKeyAndMask(0).build());
address
}
};
把子类对象转换成父类对象:
let tempAddress: XMSSAddress = XMSSAddress::HashTreeAddress(hashTreeAddress);