Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在类中实现接口时,有没有办法在接口中生成代码_Java_Eclipse_Formatter_Xcode Template - Fatal编程技术网

Java 在类中实现接口时,有没有办法在接口中生成代码

Java 在类中实现接口时,有没有办法在接口中生成代码,java,eclipse,formatter,xcode-template,Java,Eclipse,Formatter,Xcode Template,我想将类的getter和setter的所有抽象添加到我在该特定接口中实现的接口中。我还想生成一个类似于类变量的最终变量。反序列化后,此变量可用作访问类变量的字符串 例如: public class Abc implements IAbc{ private String oneVariable; public String getOneVariable(){ return oneVariable; } } 用接口IAbc实现上述类。IAbc应包含以下代码: public inte

我想将类的getter和setter的所有抽象添加到我在该特定接口中实现的接口中。我还想生成一个类似于类变量的最终变量。反序列化后,此变量可用作访问类变量的字符串

例如:

public class Abc implements IAbc{

private String oneVariable;

 public String getOneVariable(){
    return oneVariable;
 }
}
用接口IAbc实现上述类。IAbc应包含以下代码:

public interface IAbc{
  public static final String ONE_VARIABLE = "oneVariable";

  public getOneVariable();

}

我曾尝试在谷歌上搜索解决方案,但没有找到。此外,在生成此代码后,类中的方法应具有注释@Override。

TL;DR这是一个有趣的编程挑战,但我发现它在现实生活中几乎没有用处。
在这里,第一个字符串变量的名称是已知的,您可以直接在这里存储最终值,而不是以循环方式存储第二个变量的名称


如果理解正确,则尝试访问一个类的(字符串)变量,该类的名称将位于另一个字符串变量中。这可以通过java反射实现

此外,您希望将此代码放置在接口中,以便在实现它的所有类中(重新)使用它

import java.lang.reflect.Field;

interface Foo {

    public default String getStringVar() 
            throws NoSuchFieldException, IllegalAccessException {
        // get varName
        Class thisCls = this.getClass();
        Field varNameField = thisCls.getField("varName");
        String varName = (String) varNameField.get(this);

        // get String variable of name `varName`
        Field strField = thisCls.getField(varName);
        String value = (String) strField.get(this);

        return value;
    }
}


class FooImpl1 implements Foo {
    public final String varName = "str1";
    public String str1 = "some value";
}

class FooImpl2 implements Foo {
    public final String varName = "str2";
    public String str2 = "some other value";
}

class Main {
    public static void main(String[] args) 
            throws NoSuchFieldException, IllegalAccessException {
        System.out.println(new FooImpl1().getStringVar());
        System.out.println(new FooImpl2().getStringVar());
    }
}
这里,我在实现interface
Foo
的类中有两个字符串成员。第一个
varName
包含第二个字符串的变量名,第二个字符串变量包含数据。

在使用反射的界面中,我首先提取存储在
varName
中的变量名,然后使用此方法提取第二个字符串的值。

我不想为此编写代码,我想要一个模板,在创建类变量并保存时可以在eclipse中使用。相应的接口将按所述进行更新。现实世界中的应用程序是,这将仅用于每个接口只有一个类的孤立enity。此接口用于访问类(getter和setter)的方法。此外,接口中的最后一个变量用于在对象转换为map时访问该对象,并且您希望从map.Nope获取该特定变量。接口影响类,而不是相反。这将是反模式的。这就是为什么你找不到适合它的东西。这样看,接口定义了类应该具有的特定结构。编译器检查类是否遵循此结构。若你们允许类影响接口,那个么它会形成循环依赖关系。实际上不会,因为我需要的类的模板每个接口只有一个类。