super() 方法有什么作用?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
它通过调用其默认构造函数来表示“在初始化我之前初始化我的父类”。
It says "initialize my parent class before you initialize me" by calling its default constructor.
super() 调用父类的构造函数(一直返回到 Object),并且它在当前类的构造函数之前运行。
super()
calls the parent's class constructor (all the way back to Object) and it runs before the current class's constructor.不存在“超类上下文”这样的东西,上下文是从应用程序环境访问信息(如图像资源、系统服务等)的接口。上下文将来自您传入的任何内容,并且与类无关。例如,
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.在构造函数中使用时,
super()
关键字单独出现,并且必须在使用this
关键字之前使用。然后,this
关键字可用于调用父对象上的函数。When used in a constructor, the
super()
keyword appears alone and must be used before thethis
keyword can be used. Thethis
keyword may then be used to call functions on a parent object.当父类构造函数采用任何参数时,super() 是必需
当父类构造函数采用参数时,super() 是非必要 >没有参数因为 super() 已经被隐式指定。
看代码:
换句话说,每当你为子类创建一个对象时。首先,将调用父类的构造函数,然后调用子类的构造函数。所以需要使用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:
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