gpt4 book ai didi

java - 如何在不初始化的情况下访问公共(public)方法

转载 作者:行者123 更新时间:2023-12-01 07:47:43 24 4
gpt4 key购买 nike

我正在尝试创建一个简单的库,我想在其中定义类似于 Glide 的类。这样我就可以用 MyAnimation.with(context) 来调用它,但无论我定义它 abstractfinal 还是 Singleton,我不可以这样做。我确实必须创建一个实例来访问其公共(public)方法with(Context context),其编写如下:

/**
* initializes context for the current instance
* @param context context to be used for {@link ConstraintSet}
* @return an instance of {@link MyAnimation}
*/
public MyAnimation with(Context context) {
mContext = context;
mConstraintSet = new ConstraintSet();
return this;
}

如您所见,with() 返回类的实例,然后我有另一个公共(public)方法 from(@LayouRes int res) 它采用布局并返回再次这个

/**
*
* @param secondKeyframe This is the layout from which constraints will be picked
* @return An instance of this class
* @throws IllegalStateException Throws exception if context has not been initialized
*/
public MyAnimation from(@LayoutRes int secondKeyframe, Transition customTransition) throws IllegalStateException {
if(mContext == null) {
throw new IllegalStateException("Context not initalized. Please call with(Context) first");
}
mTransition = customTransition;
mConstraintSet.clone(mContext, secondKeyframe);
return this;
}

最后,有一个方法 animate(ViewGroup view),可以在 ViewGroup 上执行简单的动画。我有什么想法可以实现这一目标吗?

   /**
* Applies animation after applying new {@link ConstraintSet}
* @param view The ViewGroup on which the animation will be performed
*/
public void animate(ViewGroup view) {
mConstraintSet.applyTo((ConstraintLayout) view);
mLayout = (ConstraintLayout) view;
if(mTransition != null) {
TransitionManager.beginDelayedTransition(mLayout, mTransition);
}
else {
TransitionManager.beginDelayedTransition(mLayout);
}

}

拜托,我真的很想像 Glide 这样调用我的库 (:D)

最佳答案

您需要将这些方法设为静态。静态方法不需要调用类的实例。他们只是类成员,而不是实例成员

关于java - 如何在不初始化的情况下访问公共(public)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48247673/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com