Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.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/9/loops/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
重复使用字段或方法的名称和javadoc描述_Java_Inheritance_Struts_Javadoc - Fatal编程技术网

重复使用字段或方法的名称和javadoc描述

重复使用字段或方法的名称和javadoc描述,java,inheritance,struts,javadoc,Java,Inheritance,Struts,Javadoc,我使用的是struts2,通过它的机制,我可以获取并使用request-params,同时将确切名称声明为类的字段变量 MyClass extends SomeOtherClass { /** the user id*/ private int userId; /** the user name*/ private String userName; // /** java doc description for getters and setters here*/ //getters and

我使用的是
struts2
,通过它的机制,我可以获取并使用
request-params
,同时将
确切名称声明为类的字段变量

MyClass extends SomeOtherClass {
/** the user id*/
private int userId;
/** the user name*/
private String userName;

// /** java doc description for getters and setters here*/
//getters and setters for userId and userName here
}

mysite.com?userId=3&userName=sarah
这里的宇宙运行良好


现在我有很多(many)这样的类,它们共享相同的命名字段变量(当然有不同的值,但名称和javadoc描述相同)

我想把名字和/javadoc description*/写在一个地方。**

现在,由于我已经从另一个类扩展,所以无法使用继承,所以我尝试使用接口

public interface MyInterface {
    /** description from interface for String s */
    String s = null;
    /** description from interface for int i */
    int i = -1;
    /** description from interface for boolean b */
    boolean b = false;

    /** description from interface for getB */
     boolean getB();

}

public class Class3 implements MyInterface {
    /** description from class 2 for int i */
    int i;
    /** description from class 2 for getB */
    private boolean getB(){
        return b;
    }
    /** description from class 2 for s() */
    private String s(){
        return null;
    }
}


public class Class1 implements MyInterface {
    String getS(){
        return s;
    }
}
问题:

为类1或3生成的java文档不显示它们的任何字段或描述。为什么?

考虑struts参数并在一个地方声明公共字段及其描述,但使用相同的字段(每个类中有不同的值和getter和setter),是否有更好的策略的好主意


接口中不能有实例字段,因为接口没有任何状态。添加到接口的字段实际上是
publicstaticfinal
常量

你想做的事行不通

Class1没有javadoc中描述的任何字段,因为它没有任何字段

Class3:有一个实例字段
i
,它与接口中定义的静态常量
i
完全不同(它将其隐藏)


如果您想重用字段、getter和setter,最好的办法是使用组合而不是继承。将所有这些公共字段放在一个类中(比如Address),并在所有需要处理地址的操作类中都有一个Address类型的字段。

class3有int i;作为一个领域。这在javadoc中没有显示。你说它对接口中定义的常量隐藏是什么意思。为什么要把它藏起来?它会出现的。第二,良好的写作策略。我会的。顺便说一下,接口让它变得很困难。我可以像构图一样使用继承吗?(正如我提到的,继承不是一个选项,因为我已经从其他类中扩展了。如果我没有继承,会怎么样?那会是一个更好的选项吗?)。对不起,我错配了1班和3班。i字段具有包级别的可见性,因此除了同一个包中的类之外,任何类都不能使用它,这就是为什么javadoc没有显示它(默认情况下)。您可以使用选项来显示包级别,如果需要,甚至可以显示私有字段。类和接口都在同一个包中。我想他们应该看得见?。虽然我的问题得到了很好的回答。谢谢我希望使Address类在struts2中工作,就像类1拥有私有addr一样它的getter和setter,Address类将具有参数。最后,类1将调用
addr.getUserName()例如