java - How to use a lambda expression to pass a method as a parameter when calling another method? -
Say I have two ways and inside You have two functions: What is their likeness that they take both the two You can re-type the calling code as follows: Or if The call to the charging function will now look like this: Of course I have made some assumptions, from where the different from zero fee (integer zodiac, difference account) and
Zero ChargeCompany (int sum, int account) . Say I have another method which is called
zero processale (string item, customer C) . What can I do, I can either pass
in-charge person or
chargeToCompany as the argument for the
processSale and I can How do I call?
if (IG (some customers)) {PROCESSIAL (one item, some customer, charge to individual)} and If (iCompany (some customers)) {processSale (anItem, some customer, charge, customer)}
processSale () how will i actually call
chargout person or
chargeToCustomer () ?
zero charge (Int amount , Int accounts); Zero Incharge Companie (integer amount, int account);
int parameters and do back
zero . There is no functional interface in
java.util.function that matches this size but it is easy to define ourselves:
interface IntIntConsumer {zero acceptance (Int sum, int accounts); }
if (that is, some person (some customers)) {processSale (anItem, SomeCustomer, MyClass :: chargeToIndividual); } Else if (isCompany (some customers)) {processSale (anItem, SomeCustomer, MyClass :: chargToCompany); } Else {...}
fee-to-Indies and
in-charge customers are examples of ways, probably < Code> This :: Chargedondivative and
This :: Charged customer . Alternatively, the charging function must be stored in a local variable. After this you will have to make a call at
processSale :
IntIntConsumer chargeFunc; If (IIG (some customers)) {chargeFunc = it :: Chargedindiv; } Else if (isCompany (some customers)) {chargeFunc = this :: chargeToCompany; } And {...} with the process (one item, some customers, duty functions);
zero process all (item items, customer customer, IntIntConsumer func ) {... func.accept (item.getAmount (), customer.getAccount ()); ...}
amount and
account are found, but I think you Can get ideas.
Comments
Post a Comment