super() 方法有什么作用?

发布于 2024-11-09 16:31:02 字数 228 浏览 1 评论 0原文

super 方法有什么作用?

public DataFetch(Context context) {
    super();
    this.ctx = context;
}

这个构造函数是否使新创建的对象的上下文成为超类的上下文?不能 100% 确定这是如何工作的。那么,super() 方法本质上只是用外行人的话来说“让我进入超类模式”吗?

What does the super method do?

public DataFetch(Context context) {
    super();
    this.ctx = context;
}

Does this constructor make the context of the newly created object the context of the super class? Not 100% sure how this works. So would the super() method essentially just be saying "put me into super class mode" to say it in lay man's terms?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

天暗了我发光 2024-11-16 16:31:02

它通过调用其默认构造函数来表示“在初始化我之前初始化我的父类”。

It says "initialize my parent class before you initialize me" by calling its default constructor.

∞琼窗梦回ˉ 2024-11-16 16:31:02

super() 调用父类的构造函数(一直返回到 Object),并且它在当前类的构造函数之前运行。

super() calls the parent's class constructor (all the way back to Object) and it runs before the current class's constructor.

梦过后 2024-11-16 16:31:02

不存在“超类上下文”这样的东西,上下文是从应用程序环境访问信息(如图像资源、系统服务等)的接口。上下文将来自您传入的任何内容,并且与类无关。例如,Activity 是您可能使用的 Context 接口的实现,您在该活动中创建的任何视图都将具有与该活动实际提供的相同的 Context。

There is no such thing as "superclass context" the context is an interface to access information from the application environment like image resources, system services ect. The context will be from whatever you pass in and is class independent. For instance, an Activity is the implementation of the Context interface you are likely to be using and any view you make from within that activity will have the same Context which is actually that provided by the activity.

开始看清了 2024-11-16 16:31:02

在构造函数中使用时,super() 关键字单独出现,并且必须在使用 this 关键字之前使用。然后,this 关键字可用于调用父对象上的函数。

When used in a constructor, the super() keyword appears alone and must be used before the this keyword can be used. The this keyword may then be used to call functions on a parent object.

剑心龙吟 2024-11-16 16:31:02

当父类构造函数采用任何参数时,super() 是必需

当父类构造函数采用参数时,super() 是非必要 >没有参数因为 super() 已经被隐式指定。

看代码:

public class A{  
    int a;
    A(int a){
        this.a=a;
        //Constructor of parent class which requires a parameter
    }   
}  
public class B extends A{
    int b;
    B(int a, int b){
        super(a);
        this.b=b;
        //here super(a) is used to pass the parameter required by the 
        constructor of the parent class A.
    }
}

换句话说,每当你为子类创建一个对象时。首先,将调用父类的构造函数,然后调用子类的构造函数。所以需要使用super()方法从子类构造函数中传递父类构造函数所需的参数。

希望有帮助!

请不要忘记投票

The super() is essential when the parent class constructor takes any parameter

The super() is not necessary when the parent class constructor takes no parameter because super() will be already specified implicitly.

Look at the code:

public class A{  
    int a;
    A(int a){
        this.a=a;
        //Constructor of parent class which requires a parameter
    }   
}  
public class B extends A{
    int b;
    B(int a, int b){
        super(a);
        this.b=b;
        //here super(a) is used to pass the parameter required by the 
        constructor of the parent class A.
    }
}

In other words, whenever you create an object for the child class. First, the constructor of the parent class will be called followed by the constructor of the child class. So it is necessary to pass the parameters required by the parent class constructor from the child class constructor using super() method.

Hope it helps!

Please don't forget to upvote

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文