Java Functional Methods

0

I am learning to use the functional programming of java 8 and I do not know why but I can not make the code work for me. The problem I have is that everything is fine until I want to assign 2 variables of the class, I can one, but when trying to do the second peta the program. Does anyone know how I can do it?

class Camello{
    private String nombre;
    private Boolean soborno;
    private Boolean libertad;
    private int h;
    public Camello(String nombre) {
        this.nombre=nombre;
    }

    public static Camello parir(String nombre) {
        return new Camello(nombre);
    }

    public void venderHierva(int kilos) {
        System.out.println("Su camello le ha vendido "+kilos+" de maria");
        this.h=kilos;
    }
    public void sobornar(boolean soborno) {
        this.soborno=soborno;
    }
}

public class Pruebas {
    public static void main(String[] args) {
        Camello.parir("Gustabo")
               .venderHierva(100)
               .sobornar(true);   //Aqui da error, me dice que no puedo asignar
                                  //Datos con el tipo primitivo booleano

    }
}
    
asked by Antonio Jordan 28.11.2018 в 23:46
source

1 answer

1

The first invocation works because the method returns an instance of the class, the next invocation fails because the method returns nothing ( void ), meaning you are trying to call a method outside of a class and it does not make sense. If you want to chain the calls of the methods you would have to modify your methods like this:

public Camello metodo(/* parámetros */) {
    // ...
    return this;
}

This returns the same class you are working on each invocation and you can continue adding calls.

    
answered by 28.11.2018 / 23:54
source