标签: super

java中的超级构造函数

请解释

public class Contact {
    private String contactId;
   private String firstName;
    private String lastName;
    private String email;
    private String phoneNumber;

public Contact(String contactId,String firstName, String lastName,   String email,        String phoneNumber) {
    super();  //what does standalone super() define? With no args here?
    this.firstName = firstName;  
    this.lastName = lastName;     //when is this used?, when more than one args to be entered?
    this.email = email;
    this.phoneNumber = phoneNumber;
}
Run Code Online (Sandbox Code Playgroud)

内部没有参数的Super()意味着要定义多个参数?这是在"this.xxx"的帮助下完成的吗?

为什么我们在"公共类联系"本身中定义.为什么我们再次定义并在此处调用其参数?

java constructor class super superclass

3
推荐指数
2
解决办法
3874
查看次数

访问构造函数中的实例成员

我在一本书中读到,实例成员只有在超级构造函数运行后才能访问.

我偶然发现了以下代码:

class Parent {

    Parent() {
        printIt();
    }

    void printIt() {
        System.out.println("I'm in a overridden method. Great.");
    }
}

class Child extends Parent {

    int i = 100;

    public static void main(String[] args) {
        Parent p = new Child();
        p.printIt();
    }

    void printIt() {
        System.out.print(i + " ");
    }
}
Run Code Online (Sandbox Code Playgroud)

它打印:

0 100

我的问题是:

如果只有在超级构造函数运行后才能访问实例成员,那么为什么在执行类Parent的printIt()方法时(由于多态性实际上是Child的printIt()),它能够访问未初始化的尽管Parent的构造函数尚未执行,但是Child的实例变量i?

我错过了什么?

java constructor instance-variables super

3
推荐指数
1
解决办法
724
查看次数

python,继承,super()方法

我是python的新手,我有下面的代码,我无法开始工作: - 这是继承,我有一个圆基类,我在一个circle类中继承它(这里只是单继承).

我理解这个问题是在类中的ToString()函数内circle,特别是行,text = super(Point, self).ToString() +.. 它至少需要一个参数,但我得到了这个:

AttributeError: 'super' object has no attribute 'ToString'

我知道super没有ToString属性,但Point课程确实 -

我的代码:

class Point(object):
    x = 0.0
    y = 0.0

    # point class constructor
    def __init__(self, x, y):
        self.x = x
        self.y = y
        print("point constructor")

    def ToString(self):
        text = "{x:" + str(self.x) + ", y:" + str(self.y) + "}\n"
        return text

class Circle(Point):
    radius = 0.0

    # circle class …
Run Code Online (Sandbox Code Playgroud)

python inheritance super

3
推荐指数
1
解决办法
4612
查看次数

使用super进行Python递归方法调用

我正在使用依赖于递归方法调用的库:

class A(object):
    def __init__(self):
        self.foo = None

    def f(self):
        if not self.foo:
            print("Hello")
            self.foo = 100
            self.f()
Run Code Online (Sandbox Code Playgroud)

我想在使用原始实现时覆盖方法f():

class B(A):
    def f(self):
        super(B, self).f()
        print("World")
Run Code Online (Sandbox Code Playgroud)

这样,我希望得到:

Hello
World
Run Code Online (Sandbox Code Playgroud)

相反,我看到:

Hello
World
World
Run Code Online (Sandbox Code Playgroud)

我理解这是因为A类中的原始代码调用self.f(),它找到了B.self.

问题:使用"超级(B,自我).f()"将自我视为A类,递归调用Af(),然后返回Bf()打印"World?"的最Pythonic方法是什么?

谢谢.

python super

3
推荐指数
1
解决办法
554
查看次数

什么时候超级(Baseclass,self).__ init__使用

何时应该在Python中使用以下代码(假设Baseclass继承自Parent类,而Parent类在__init __()方法中启动了一些变量)

class Baseclass(Parent):
    def __init__(self, some_arg):
        self.some_arg = some_arg
        super(Baseclass, self).__init__()
Run Code Online (Sandbox Code Playgroud)

此代码是否使在Parentclass的__init__方法中定义的所有局部变量都可以在Baseclass中访问?它有什么意义?

python class super

3
推荐指数
1
解决办法
279
查看次数

Javascript - 对父母调用 super 父母?

我在 Odoo 中定义了当前自定义 javascript 视图的扩展:

openerp.account_move_journal_test = function(instance){
    var _t = instance.web._t,
        _lt = instance.web._lt;
    var QWeb = instance.web.qweb;
    instance.web.account.QuickAddListView.include({
        init: function(){
            this._super.apply(this, arguments);
            console.log("QuickAddListView modified init")
        },
    });    
};
Run Code Online (Sandbox Code Playgroud)

现在,为了更好地表示,我添加了控制台登录QuickAddListView和 in ListView,它们是称为 using 的父级_super

因此,如果我像这样运行它,我会得到这些打印结果:

'ListView init'  // This is parent of QuickAddListView
'QuickAddListView Init'
'QuickAddListView modified init'
Run Code Online (Sandbox Code Playgroud)

构造函数的顺序是这样的View-> ListView->QuickAddListView

因此,所有这些都按应有的方式打印,但我想要的是以init直接调用ListView并跳过QuickAddListView原始init.

所以之后它应该只打印这个(意味着原始的 QuickAddListView init 没有被调用):

'ListView init'  // This is parent of …
Run Code Online (Sandbox Code Playgroud)

javascript super odoo odoo-8

3
推荐指数
1
解决办法
2211
查看次数

超级尝试MRO中的每个班级

class Base(object):
    def m(self):
        print 'base'


class MixinA(Base):
    def m(self):
        super(MixinA, self).m()
        print 'mixin a'


class MixinB(Base):
    def m(self):
        super(MixinB, self).m()
        print 'mixin b'


class Top(MixinB, MixinA, Base):
    def m(self):
        super(Top, self).m()
        print 'top'


t = Top()
t.m()
Run Code Online (Sandbox Code Playgroud)

这打印:

base
mixin a
mixin b
top
Run Code Online (Sandbox Code Playgroud)

我对多种事情感到惊讶.第一个MRO Top(<class 'Top'>, <class 'MixinB'>, <class 'MixinA'>, <class 'Base'>, <type 'object'>)

  1. 为什么mixin a来之前mixin b
  2. 是否super尝试在MRO每一个类(不同于搜索时,返回找到的第一个属性的属性)?

python oop multiple-inheritance super

3
推荐指数
1
解决办法
70
查看次数

如何在Java中使用super?

您能帮我如何super.paintComponent(g);在Java中使用super()关键字。

我们可以paintComponent在Java方法中使用super关键字吗?如果我们在main方法中使用super怎么办。可能吗

File Drawrainbow.java
package com.company;

import javax.swing.*;
import java.awt.*;

public class DrawRainbow extends JPanel {
    // define indigo and violet
    private final static Color VIOLET = new Color(128,0,128);
    private final static Color INDIGO = new Color(75,0,130);

    //colors to use in the rainbow, starting from innermost
    // the two white entries result in an empty arc in the center

    private Color[] colors ={Color.WHITE, Color.WHITE, VIOLET,INDIGO,Color.BLUE,Color.GREEN,Color.YELLOW,Color.ORANGE,Color.RED};

    //constructor
    public DrawRainbow(){
        setBackground(Color.WHITE); // set the background to while
    } …
Run Code Online (Sandbox Code Playgroud)

java super

3
推荐指数
1
解决办法
96
查看次数

带参数和不带参数的 super() 有什么区别?

我遇到了super()以两种不同方式使用该方法的代码,我无法理解逻辑上的区别。

我现在正在学习pygame模块,我有一个任务来创建一个类,Ball它继承自模块Sprite的类pygame(如果我没记错的话)。

我遇到了这个代码:

import pygame

class Ball(pygame.sprite.Sprite):

    def __init__(self, x, y):
        super(Ball, self).__init__()
Run Code Online (Sandbox Code Playgroud)

我无法理解与以下内容的区别:

import pygame

class Ball(pygame.sprite.Sprite):

    def __init__(self, x, y):
        super(Ball, self).__init__()
Run Code Online (Sandbox Code Playgroud)

(方法论super()

这些代码块在逻辑上有什么区别?为什么我需要传递给super()方法参数?这些论据必须是什么?

python pygame class super python-3.x

3
推荐指数
1
解决办法
988
查看次数

为什么 Comparable 和 Comparator 是 Java 中 PECS 通配符类型的消费者

在Effective Java中,在“使用有界通配符增加API灵活性”一文中,在谈到PECS(producer-extends,consumer-super)的使用时,作者提到:

Comparable 始终是消费者,因此您通常应该使用 Comparable<? super T> 优先于 Comparable。比较器也是如此;因此,您通常应该使用 Comparator<? super T> 优先于比较器。

我不清楚为什么 Comparables 和 Comparator 被认为是消费者。

在讨论 PECS 的主题之一中,什么是 PECS(生产者扩展消费者超级)?,消费者通常将 Collection 称为某个泛型方法的参数。

而这里 Comparable 只是一个接口。

任何人都可以分享一些见解吗?谢谢!

java generics super pecs

3
推荐指数
1
解决办法
185
查看次数