简体   繁体   English

这些类之间的关系应该是什么?

[英]what should be the relationship between those classes?

I wish just call Type.getNo() method to get some calculation result. 我希望只调用Type.getNo()方法来获取一些计算结果。 If if if then else block in the Type.getNo() says "take data from TypeX.getNo() ", then TypeX.getNo() is called internally; 如果如果if then else Type.getNo()中的if then else块显示“从TypeX.getNo()获取数据”,则在内部调用TypeX.getNo() afterwards, result of it will be sent to Type.getNo() , and from then, Type.getNo() will return the value to the user. 然后,将其结果发送到Type.getNo() ,然后Type.getNo()会将值返回给用户。 In the second case, result of TypeY.getNo() will be used. 在第二种情况下,将使用TypeY.getNo()结果。

For class relationship, what should be the relationship between those classes? 对于类关系,这些类之间的关系应该是什么? I thought it seems to extends relation but, I am a bit confused about how to implement the scenario. 我以为这似乎extends关系,但是我对如何实现该方案感到困惑。 To advance my project, should I use Design Pattern to implement this? 为了推进我的项目,我应该使用设计模式来实现吗? If yes, which design pattern is appropriate for this case? 如果是,哪种设计模式适合这种情况? I feel chain of responsibility looking like to fix, but in chain of responsibility , decision is done on the third class which is, in my case, it should be in the other package. 我觉得chain of responsibility看起来很固定,但是在chain of responsibility ,决定权是在第三类上做出的,就我而言,这应该在另一包中。 On the other hand, all decision should be done in the same class. 另一方面,所有决定都应在同一班上进行。 Just return value is going to be available in other packages. 只是返回值将在其他包中可用。

                        |--------------------|
                        |       Type         |
                        |--------------------|
                        |    +getNo():int    |
                        |                    |
                        |--------------------|



  |------------------|                    |----------------------|
  |      TypeX       |                    |        TypeY         |
  |------------------|                    |----------------------|
  |                  |                    |                      |
  |  +getNo():int    |                    |      +getNo():int    |
  |                  |                    |                      |
  |------------------|                    |----------------------|

     TypeX and TypeY are visible to only to other classes in the same package

Use an abstract class Type and two classes TypeX , TypeY which extend Type . 使用抽象类Type和两个扩展TypeTypeXTypeY

Type

abstract class Type {

    int no;

    int getNo() {
        return no;
    }
}

TypeX

public class TypeX extends Type {

    public TypeX(int no) {
        this.no = no;
    }

    @Override
    int getNo() {
        System.out.println("TypeX.getNo()");
        return no;
    }
}

TypeY

public class TypeY extends Type {

    public TypeY(int no) {
        this.no = no;
    }

    @Override
    int getNo() {
        System.out.println("TypeY.getNo()");
        return no;
    }
}

App

public class App {

    public static void main(String[] args) {
        Type x = new TypeX(1);
        Type y = new TypeY(2);

        System.out.println(x.getNo());
        System.out.println(y.getNo());
    }
}

Output: 输出:

TypeX.getNo()
1
TypeY.getNo()
2

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

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