java - How to use a lambda expression to pass a method as a parameter when calling another method? -


Say I have two ways 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)}   

and inside processSale () how will i actually call chargout person or chargeToCustomer () ?

You have two functions:

  zero charge (Int amount , Int accounts); Zero Incharge Companie (integer amount, int account);   

What is their likeness that they take both the two 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); }   

You can re-type the calling code as follows:

  if (that is, some person (some customers)) {processSale (anItem, SomeCustomer, MyClass :: chargeToIndividual); } Else if (isCompany (some customers)) {processSale (anItem, SomeCustomer, MyClass :: chargToCompany); } Else {...}   

Or if 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);   

The call to the charging function will now look like this:

  zero process all (item items, customer customer, IntIntConsumer func ) {... func.accept (item.getAmount (), customer.getAccount ()); ...}   

Of course I have made some assumptions, from where the amount and account are found, but I think you Can get ideas.

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -