0

Hi I have this code where I'm trying to implement a C-Sharp like(not the best way to put it I know) getter setter functionality in java There was no problem when I defined getters and setters separately however when I started to make an accessor(something that has both a getter and a setter) I encountered this error

error: constructor Access in class Access cannot be applied to given types; public accessor Type = new Access ( () ->type, (Types v) -> { type = v; } ); ^ required: getter,setter found: ()->type,(Types v)-[...] v; } reason: argument mismatch; incompatible parameter types in lambda expression where T is a type-variable: T extends Object declared in class Access 1 error

Why is this happening even tho when used separately the abstract interfaces were perfectly overridden by the lambda expressions and what am I doing wrong?

Here's the code:

import java.util.Scanner;

interface getter<T>{
    T get();
}
interface setter<T>{
    void set(T value);
}
interface accessor<T>{
    T get();
    void set(T value);
}
class Access<T> implements accessor<T>{
    private getter<T> G;
    private setter<T> S;
    public Access(getter<T> Getter,setter<T> Setter){
        G = Getter;
        S = Setter;
    }
    public T get(){
        return G.get();
    }
    public void set(T value){
        S.set(value);
    }
}
class Bomb{
    public Bomb(){

    }
    public static enum Types{
        COMMON,
        RARE,
        SUPER
    }
    private Types type;
    public accessor<Types> Type = new Access ( () ->type, (Types v) -> { type = v; } );
}
public class Main{
    public static void main(String args[]){
        Bomb b = new Bomb();
        b.Type.set( Bomb.Types.COMMON );
        System.out.println(b.Type.get());
    }
}

hirad davari
  • 103
  • 6
  • 3
    You're using `Access` as a raw type. Try `new Access<>(...)` or `new Access(...)`. – khelwood Apr 15 '21 at 21:08
  • 1
    Also, in Java land, the style is types are leading caps, so `interface Accessor`, `interface Getter` etc, and fields are camel, so `private Getter g;` but typically the camel version of the type, so `private Getter getter;` – Bohemian Apr 15 '21 at 21:15

0 Answers0