问题
What is the purpose of NullKeys.NULL_PARTY
?
For example, when should I use
party: AbstractParty = NullKeys.NULL_PATRY
Rather than
party: AbstractParty? = null
回答1:
party: AbstractParty? = null
The above defines a nullable variable i.e. party will either be an AbstractParty
or it will be null
party: AbstractParty = NullKeys.NULL_PARTY
The above on the other hand, will never result in party being null
, but rather you'll end up with an AnonymousParty
with a null
public key.
NULL_PARTY
could come in useful during unit testing, particularly when testing for equality, but it's not advised to use it for production code.
回答2:
!= NULL_PARTY
works just as well as != null
and removes the nuisance of not-null assertion/safety operators when you can be sure the value will not be used, e.g. an owner
for a state that has not been purchased by one.
See also sample uses in Corda's repo.
来源:https://stackoverflow.com/questions/54450888/corda-purpose-of-null-party