简体   繁体   English

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

[英]reuse of fields or methods for their names and for javadoc description

I am using struts 2 and getting benefit by its mechanism by which I can get and use request params while I declare the exact names as field variables of my class. 我正在使用struts 2并从它的机制中受益,在我将exact names as field variables声明exact names as field variables类的exact names as field variables ,可以通过该机制获取和使用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

The universe uptill here works fine and nice. 这里的宇宙正常运转很好。


Now I have many ( Many ) of such classes that share same named field variables (with of-course different values but same name and same javadoc description). 现在我有一个共享相同的命名的字段变量(与道不同的值,但相同的名称和javadoc的描述),这些类的许多( 很多 )。

I want to write the names and / javadoc description */ for them in one place.** 我想将它们的名称和/ javadoc描述* /放在一个地方。**

Now since I cant use inheritance as I am already extending from an other class, I tried to use Interfaces. 现在,由于无法使用继承,因为我已经从其他类扩展了,所以我尝试使用接口。

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;
    }
}

Questions: 问题:

The java docs generated for class 1 or 3 do NOT show any fields or description for them. 为类1或3生成的Java文档不会显示任何字段或描述。 Why? 为什么?

And any bright ideas for a better strategy considering struts params and declaring the common fields and its description at one place but using the same (with different values and getters and setters in each class) 以及考虑到struts参数并在一个地方声明公共字段及其描述的更好策略的任何明智的主意,但要使用相同的字段(每个类中使用不同的值,getter和setter)

在此处输入图片说明在此处输入图片说明

You can't have instance fields in interfaces, because interfaces don't have any state. 接口中不能包含实例字段,因为接口没有任何状态。 The fields you added to the interfaces are actually public static final constants. 您添加到接口的字段实际上是public static final常量。

What you're trying to do won't work. 您尝试执行的操作无效。

Class1 doesn't have any field described in the javadoc because it doesn't have any. Class1在Javadoc中没有描述任何字段,因为它没有任何字段。

Class3: has one instance field i which is completely different (it's hiding it) from the static constant i defined in the interface. Class3:具有一个实例字段i ,该实例字段与接口中定义的静态常量i完全不同(将其隐藏)。

If you want to reuse fields, getters and setters, your best bet is to use composition and not inheritance. 如果要重用字段,获取器和设置器,最好的选择是使用组合而不是继承。 Place all these common field in a class (let's say Address), and have a field of type Address in all the action classes that need to handle addresses.. 将所有这些公共字段放在一个类中(比方说地址),并在所有需要处理地址的操作类中将一个地址类型的字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM