简体   繁体   English

如何创建类和链接方法

[英]How to create class and chaining methods

I built a class (such as a class to make simple animations): 我构建了一个 (例如一个类来制作简单的动画):

public class myAnimations {

    private Animation animation;
    private ImageView imageView;

    public myAnimations(ImageView img) {
        super();
        this.imageView = img;
    }

    public void rotate(int duration, int repeat) {
        animation = new RotateAnimation(0.0f, 360.0f,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setRepeatCount(repeat);
        animation.setDuration(duration);
    }

    public void play() {
        imageView.startAnimation(animation);
    }

    public void stop() {
        animation.setRepeatCount(0);
    }
}

and I just can use it in this way: 我只是可以这样使用它:

ImageView myImage = (ImageView) findViewById(R.id.my_image);
myAnimations animation = new myAnimations(myImage);
animation.rotate(1000, 10);
animation.play(); //from this way…

but if I wanted to be able to use it like this: 但如果我想能够像这样使用它:

ImageView myImage = (ImageView) findViewById(R.id.my_image);
myAnimations animation = new myAnimations(myImage);
animation.rotate(1000, 10).play(); //…to this way

so I can call this double method (I don't know the name), how should I build my class? 所以我可以调用这个双重方法 (我不知道名字), 我应该如何构建我的类?

PS please feel free to edit the title if you know the name of my what I need. 如果您知道我需要的名称,请随时编辑标题。

You're asking about allowing method chaining and to do this, some of your methods should not return void, but rather the they should return this . 你问的是允许方法链接并且这样做,你的一些方法不应该返回void,而是应该返回this For example: 例如:

// note that it is declared to return myAnimation type
public MyAnimations rotate(int duration, int repeat) {
    animation = new RotateAnimation(0.0f, 360.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    animation.setRepeatCount(repeat);
    animation.setDuration(duration);
    return this;
}

Thus when calling this method, you can chain another method call onto it since it returns the current object: 因此,当调用此方法时,您可以将另一个方法调用链接到它,因为它返回当前对象:

animation.rotate(1000, 10).play();

You will need to do this for every method that you want to allow chaining. 您需要为要允许链接的每个方法执行此操作。

Note that as per Marco13 , this is also referred to as a Fluent Interface . 请注意,根据Marco13 ,这也称为Fluent界面

As an aside, you will want to learn and use Java naming conventions . 另外,您将需要学习和使用Java命名约定 Variable names should all begin with a lower letter while class names with an upper case letter. 变量名都应以较低的字母开头,而类名以大写字母开头。 Learning this and following this will allow us to better understand your code, and would allow you to better understand the code of others. 学习这一点并遵循这一点将使我们能够更好地理解您的代码,并使您能够更好地理解其他代码。 So re-name your myAnimations class to MyAnimations. 因此,将myAnimations类重命名为MyAnimations。

It is called Builder Design Pattern , which is used to avoid handling too many constructors. 它被称为Builder Design Pattern ,用于避免处理太多构造函数。

To achieve it, first your method return type should be your class name and you have to return this for all the methods which you want in chain. 为了实现这一目标,首先你的方法的返回类型应该是你的类名,你必须回到this对所有你想要链的方法。

So, in your case, rotate method return type will be myAnimations . 因此,在您的情况下,rotate方法返回类型将是myAnimations

public myAnimations rotate(int duration, int repeat) {
    animation = new RotateAnimation(0.0f, 360.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    animation.setRepeatCount(repeat);
    animation.setDuration(duration);
    return this;
}

Now, you can call as per your expectation, 现在,您可以按照您的期望打电话,

animation.rotate(1000, 10).play();

Also, I strongly recommend to use proper naming convention for class name. 另外,我强烈建议对类名使用正确的命名约定。 It should be ideally MyAnimations . 它应该是理想的MyAnimations

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

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