按钮类:
class SubmitButton extends JButton implements ActionListener {
public SubmitButton(String title){
super(title);
....
Run Code Online (Sandbox Code Playgroud)
我在哪里声明:
SubmitButton submit = new SubmitButton("Submit");
submit.setBounds(530+150, 200, 100, 25);
Run Code Online (Sandbox Code Playgroud)
super(title)如何将String标题设置为按钮的标题?它如何与常规JButtons的方法.setText()相同?
换句话说,如何调用super()与.setText()做同样的事情super()知道如何更改标题?是.setText()是JButton Class中唯一一个以字符串作为参数的方法吗?
这是一个关于编码风格和推荐做法的问题:
正如在问题的答案中解释的那样,在构造函数中放置super()是不必要的?,如果你为一个应该使用超类中的默认(no-arg)构造函数的类编写构造函数,你可以super()在构造函数的开头调用:
public MyClass(int parm){
super(); // leaving this out makes no difference
// do stuff...
}
Run Code Online (Sandbox Code Playgroud)
但你也可以省略通话; 在两种情况下,编译器都会像super()调用一样.
那么,你是否将呼叫置于你的构造函数中?
一方面,人们可能会争辩说,包括super()使事情更明确.OTOH,我总是不喜欢编写冗余代码,所以我个人倾向于将其删除; 然而,我经常在其他人的代码中看到它.
你有什么经历?你有一种或另一种方法有问题吗?您是否有规定一种方法的编码指南?
BTW:相关问题(仅供参考):
我有一个必须用参数构造的基类.在子类中,我需要在构造基类之前准备这个参数,但是在Java中必须先调用super.处理这种情况的最佳方法是什么(见下面的简单示例).
class BaseClass {
protected String preparedParam;
public BaseClass(String preparedParam) {
this.param = param;
}
}
class ChildClass {
public ChildClass (Map<String, Object> params) {
// need to work with params and prepare param for super constructor
super(param);
}
}
Run Code Online (Sandbox Code Playgroud) 这引发了我在1.9.2 Ruby中的SystemStackError(但在Rubinius中工作):
class Fixnum
def +(other)
self + other * 2
end
end
Run Code Online (Sandbox Code Playgroud)
但没有super了+(基于其他错误).
我如何访问原始+功能?
所以......我正在尝试使用PyQt4从基本的Python转向一些GUI编程.我正在看几本不同的书籍和教程,他们每个人似乎都有一种稍微不同的方式来推动课程定义.
一个教程从这样的类开始:
class Example(QtGui.QDialog):
def __init__(self):
super(Example, self).__init__()
Run Code Online (Sandbox Code Playgroud)
另一本书是这样的:
class Example(QtGui.QDialog):
def __init__(self, parent=None):
super(Example, self).__init__(parent)
Run Code Online (Sandbox Code Playgroud)
而另一个是这样做的:
class Example(QtGui.QDialog):
def__init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
Run Code Online (Sandbox Code Playgroud)
我仍然在试图总结我的脑海围绕类和OOP和super()所有......我在想,在第三个例子中的最后一行完成或多或少同样的事情,使用呼叫纠正super()在前面的,通过明确直接调用基类?对于这些相对简单的例子,即单一继承,是否有任何真正的好处或理由使用单向与另一种方式?最后......第二个例子parent作为一个参数传递,super()而第一个例子没有...任何关于为什么/何时/哪里合适的猜测/解释?
我试图在Java中获得继承权,并且已经了解到当在子类中重写方法(和隐藏字段)时,仍然可以使用"super"关键字从超类中访问它们.
我想知道的是,'super'关键字是否应该用于非重写方法?
有没有区别(对于非重写方法/非隐藏字段)?
我在下面举了一个例子.
public class Vehicle {
private int tyreCost;
public Vehicle(int tyreCost) {
this.tyreCost = tyreCost;
}
public int getTyreCost() {
return tyreCost;
}
}
Run Code Online (Sandbox Code Playgroud)
和
public class Car extends Vehicle {
private int wheelCount;
public Vehicle(int tyreCost, int wheelCount) {
super(tyreCost);
this.wheelCount = wheelCount;
}
public int getTotalTyreReplacementCost() {
return getTyreCost() * wheelCount;
}
}
Run Code Online (Sandbox Code Playgroud)
具体来说,既然getTyreCost()没有被覆盖,应该getTotalTyreReplacementCost()使用getTyreCost(),还是super.getTyreCost()?
我想知道是否应该在访问超类的字段或方法的所有实例中使用super(在代码中显示您正在访问超类),或者仅在重写/隐藏的那些实例中使用super(因此它们脱颖而出).
我正在编写一个LoginRequest类的构造函数,它扩展了一个名为JsobObjectRequest的类(来自Android中的Volley框架,但这与问题完全无关)
使用此代码:
public LoginRequest(String username, String password, Response.Listener<JSONObject> responseListener, Response.ErrorListener errorListener) {
Boolean hasCredentials=(username!=null && password!=null);
int method=hasCredentials? Method.POST:Request.Method.GET;
super(method, API_URL_LOGIN, null, responseListener, errorListener);
this.username=username;
this.password=password;
}
Run Code Online (Sandbox Code Playgroud)
我得到错误:调用super()必须是构造函数体中的第一个语句
相反,这段代码编译得很好:
public LoginRequest(String username, String password, Response.Listener<JSONObject> responseListener, Response.ErrorListener errorListener) {
super((username!=null && password!=null)? Method.POST:Request.Method.GET, API_URL_LOGIN, null, responseListener, errorListener);
this.username=username;
this.password=password;
}
Run Code Online (Sandbox Code Playgroud)
但这不是有效的完全相同的事情吗?在这两种情况下,在调用超级构造函数之前,根据传递给子类构造函数的参数值,进行一些简单的计算.为什么编译器不能编译第一个例子,因为它可以编译第二个例子?
call-super-constructor-must-first-statement语句规范是否比它需要的更简单,或者我错过了什么?
编辑:这被错误地标记为重复为什么this()和super()必须是构造函数中的第一个语句?.这个问题更通用,并询问为什么super()必须是第一个声明.这里的问题是为什么像我发布的那样的案例会破坏这些要求(并且在这个问题中得到了令人满意的回答)
我super().__init__()在代码中使用和显式调用超类构造函数之间的行为有一个无法解释的差异.
class IPElement(object):
def __init__(self, ip_type='IPv4'):
self.ip_type = ip_type
class IPAddressSimple(IPElement):
def __init__(self, ip_name, ip_type='IPv4'):
self.ip_name = ip_name
super().__init__(self, ip_type=ip_type)
Run Code Online (Sandbox Code Playgroud)
这里,该行super().__init__(self, ip_type=ip_type)导致类型错误:
TypeError: __init__() got multiple values for argument 'ip_type'
Run Code Online (Sandbox Code Playgroud)
当我将调用更改为ip_type按位置传递时(例如,super().__init__(self, ip_type)我得到一个不同的类型错误:
TypeError: __init__() takes from 1 to 2 positional arguments but 3 were given
Run Code Online (Sandbox Code Playgroud)
这些错误都没有对我有意义,当我用super()超类的显式名称替换时,一切都按预期工作.以下工作正常,就像按位置传递ip_type一样:
class IPAddressSimple(IPElement):
def __init__(self, ip_name, ip_type='IPv4'):
self.ip_name = ip_name
IPElement.__init__(self, ip_type=ip_type)
Run Code Online (Sandbox Code Playgroud)
__init__如果有必要,我当然可以使用显式调用超类方法,但我想理解为什么super()不在这里工作.
有两个类Super1和Sub1
Super1.class
public class Super1 {
Super1 (){
this.printThree();
}
public void printThree(){
System.out.println("Print Three");
}
}
Run Code Online (Sandbox Code Playgroud)
Sub1.class
public class Sub1 extends Super1 {
Sub1 (){
super.printThree();
}
int three=(int) Math.PI;
public void printThree(){
System.out.println(three);
}
public static void main(String ...a){
new Sub1().printThree();
}
}
Run Code Online (Sandbox Code Playgroud)
当我调用printThree类的方法时,Sub1我希望输出为:
打印三
3
因为Sub1构造函数调用了super.printThree();.
但我真的得到了
0
打印3
3
我知道0是默认值,int但它是如何发生的?
我有一个基类:
class ViewController: UIViewController
{
init(nibName nibNameOrNil: String?)
{
super.init(nibName: nibNameOrNil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) { }
}
Run Code Online (Sandbox Code Playgroud)
子类:
class OneViewController: ViewController
{
private var one: One
init(one: One)
{
self.one = one
super.init(nibName: "OneNib")
}
required init?(coder aDecoder: NSCoder) { }
}
Run Code Online (Sandbox Code Playgroud)
以及上述子类的子类:
class TwoViewController: OneViewController
{
private var two: Two
init(two: Two)
{
self.two = two
super.init(nibName: "TwoNib") <== ERROR
}
required init?(coder aDecoder: NSCoder) { }
}
Run Code Online (Sandbox Code Playgroud)
在指定的行我得到错误:
Ambiguous reference to member 'init(one:)'
我不明白为什么编译器无法弄清楚我指的是 …
super ×10
java ×6
constructor ×4
coding-style ×2
inheritance ×2
ios ×1
pyqt ×1
pyqt4 ×1
python ×1
python-3.x ×1
redundancy ×1
rubinius ×1
ruby ×1
swift ×1
swift2 ×1
yarv ×1