“Banner ad events (java)” 可以翻译为 “横幅广告事件(Java)”。

huangapple go评论60阅读模式
英文:

Banner ad events (java)

问题

我在Activity中有一个横幅广告。
mainActivity.java中有4个*@Overridable方法*的横幅广告。

我想要什么?

  1. 我想要声明一个方法。
  2. 我想把这4个事件放到我的方法中。

用一句话来说:

我可以把@Overrides放到方法里吗?

private void allTogether(){

@Override
public void onAdFailedToLoad(int errorCode) {
// 当广告请求失败时执行的代码。
}

@Override
public void onAdOpened() {
// 当广告打开覆盖屏幕的叠加层时执行的代码。
}

@Override
public void onAdClicked() {
// 当用户点击广告时执行的代码。
}

@Override
public void onAdLeftApplication() {
// 当用户离开应用程序时执行的代码。
}

@Override
public void onAdClosed() {
// 当用户在点击广告后即将返回应用程序时执行的代码。
}
}

我该如何做到这一点?

英文:

I have a banner ad in my Activity.
I have 4 @Overridable methods of banner ad in mainActivity.java.

What I want?

  1. I want to declare a method
  2. I want to put those 4 events to the my method

in one sentence:

Can I put @Overrides into the method?

private void allTogether(){

  @Override
public void onAdFailedToLoad(int errorCode) {
    // Code to be executed when an ad request fails.
}

@Override
public void onAdOpened() {
    // Code to be executed when an ad opens an overlay that
    // covers the screen.
}

@Override
public void onAdClicked() {
    // Code to be executed when the user clicks on an ad.
}

@Override
public void onAdLeftApplication() {
    // Code to be executed when the user has left the app.
}

@Override
public void onAdClosed() {
    // Code to be executed when the user is about to return
    // to the app after tapping on an ad.
}

How can I do that?

答案1

得分: 1

不能仅仅在任何方法上添加 @Override。该方法必须是可重写的,才能这样做。我认为你想要做的是让所有的 onAd* 方法调用相同的方法,类似于这样:

@Override
public void onAdClicked() {
    allTogether();
}
英文:

You can't just put @Override on any method. The method has to be overridable for you to do that. I think what you want to do is to have all the onAd* methods to call the same method, something like this:

@Override
public void onAdClicked() {
    allTogether();
}

答案2

得分: 1

子类覆盖方法的能力允许一个类继承自一个“足够接近”的超类,然后根据需要修改行为。
覆盖方法与被覆盖的方法具有相同的名称、参数数量和类型,以及返回类型。覆盖方法还可以返回被覆盖方法返回类型的子类型。这个子类型被称为协变返回类型。
如果您尝试从一个子类方法内部调用所有的@Overridable方法,

那你能做什么?
可重写的方法是用来分开使用的。你不能用一个方法调用它们全部。

英文:

The ability of a subclass to override a method allows a class to inherit from a superclass whose behavior is "close enough" and then to modify behavior as needed.
The overriding method has the same name, number and type of parameters, and return type as the method that it overrides. An overriding method can also return a subtype of the type returned by the overridden method. This subtype is called a covariant return type.
If you attempt to call all the @Overridable methods from inside of one subclass method,

What can you do with that?
Overridable methods are for using them seperately. you can't call all of them with one method.

huangapple
  • 本文由 发表于 2020年7月31日 07:59:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63183054.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定