【问题标题】:reuse of fields or methods for their names and for javadoc description重用字段或方法的名称和 javadoc 描述
【发布时间】:2013-11-30 05:46:38
【问题描述】:

我正在使用struts 2,并从它的机制中受益,通过它我可以获取和使用request params,同时我声明我的班级的exact names as field variables

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 描述 */ 写在一个地方。**

现在我不能使用继承,因为我已经从其他类扩展,我尝试使用接口。

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)的更好策略的任何好主意

【问题讨论】:

    标签: java inheritance struts javadoc


    【解决方案1】:

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

    你试图做的不会奏效。

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

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

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

    【讨论】:

    • class3 有 int i;作为一个领域。 javadoc中没有显示。你是什​​么意思,它隐藏在接口中定义的常量中。为什么要隐藏它?它显示出来。其次,良好的构图策略。会做的。顺便说一句,界面使事情变得困难。我可以像组合一样使用继承吗? (正如我提到的,继承不是一个选项,因为我已经从其他类扩展。如果我没有。那会是更好的选择吗?)
    • 不,构图还是会更好。抱歉,我的 Class1 和 Class3 不匹配。 i 字段具有包级别的可见性,因此除同一包中的类之外的任何类都不能使用它,这就是 javadoc 不显示它的原因(默认情况下)。如果需要,您可以使用选项来显示包级别,甚至是私有字段。
    • 类和接口都在同一个包中。我认为它们应该是可见的?虽然我的问题得到了很好的回答。谢谢!
    • 我希望使 Address 类可以在 struts 2 中工作,就像 Class 1 将有 private Address addr; 的 getter 和 setter 一样,并且 Address 类将具有参数。最后 Class 1 将调用 addr.getUserName(); e.g
    猜你喜欢
    • 1970-01-01
    • 2020-08-28
    • 2018-12-19
    • 2013-09-18
    • 1970-01-01
    • 2011-08-05
    • 2017-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多