标签: super

如何调用grand-parent的方法,并在ruby中跳过父级

如何在继承链中选择特定的方法调用?

class A
  def boo; puts "A:Boo"; end
end

class B < A
  def boo; super; puts "B:Boo"; end
end

class C < B
  def boo; self.A.boo(???); puts "C:Boo"; end
end
Run Code Online (Sandbox Code Playgroud)

因此输出将是A:Boo,C:Boo

TIA,

-daniel

ruby inheritance super

2
推荐指数
1
解决办法
716
查看次数

heroku&devise用户注册注册控制器错误

我刚刚切换到Devise/Omniauth组合,一切都在我的localhost服务器上正常工作.但是,当我上传到heroku时,当用户点击传统注册表单上的注册(而不是omniauth登录)时,应用程序崩溃.我正在使用rails 3.我的日志说

LoadError(没有这样的文件加载--bcrypt):app/controllers/registrations_controller.rb:11在'build_resource'app/controllers/registrations_controller.rb:4 in create'

引用的控制器:

class RegistrationsController < Devise::RegistrationsController

  def create
    super
    session[:omniauth] = nil unless @user.new_record?
  end

  private

  def build_resource(*args)
    super
    if session[:omniauth]
      @user.apply_omniauth(session[:omniauth])
      @user.valid?
    end
  end 
end
Run Code Online (Sandbox Code Playgroud)

由于注册控制器超越了Devise,因此第4行和第11行是超级的.出了什么问题?谢谢.

ruby-on-rails heroku super railscasts devise

2
推荐指数
1
解决办法
1611
查看次数

Super()首先要做的事情

所以我有这个代码.

public class HourlyWorker extends Worker {

private int hours;
public HourlyWorker(String name, int salRate,int hours) {
    super(name, salRate);
    this.hours=hours;
}

@Override
void computePay() {
    int pay;
    if(hours<60)
    {
        System.out.println("haha");
        pay=super.getSalRate()*hours;   //CALLING SUPER HERE.
        System.out.println("pay of"+super.getName()+"="+pay);
    }

}

}
Run Code Online (Sandbox Code Playgroud)

现在在Worker课堂上,我有这个方法getSalRate.有人告诉我,这super将是方法中的第一行代码.但是,如果我在这里(在给出的表达式)中调用它; 它工作得很好.

当有人这样说时,有人可以告诉我,你的意思是什么? "Make Sure That Super Is The First Line Of Code

它只适用于构造函数吗?

java super

2
推荐指数
1
解决办法
224
查看次数

在调用超类型构造函数之前,不能引用roomNumber

我已经创建了两个类Standard,Family并扩展了抽象类Room,在编译Standard类时我遇到了错误"在调用超类型构造函数之前无法引用roomNumber",但我无法理解为什么,任何帮助都将不胜感激.

Room

public abstract class Room
{
    public int roomNumber;
    public String roomType;
    public boolean ensuite;
    public boolean available;

public Room(int roomNumber, boolean enSuite)
   {
       this.roomNumber = roomNumber;
       ensuite = enSuite;
       available = false;
   }
}
Run Code Online (Sandbox Code Playgroud)

Standard

public class Standard extends Room
{
    private int roomNumber;
    private int childCap;
    private int adultCap;



public Standard(int theNumber, int kidsCap, int adultsCap)
   {
       super(theNumber, roomNumber);
       childCap = childsCap;
       adultCap = AdultsCap;
   }
}
Run Code Online (Sandbox Code Playgroud)

java abstract-class super

2
推荐指数
1
解决办法
5140
查看次数

调用超级方法存在意义吗?

例如:

public class ClassA extends ClassB { 


public void run() {
  super.execute();
 ...................
Run Code Online (Sandbox Code Playgroud)

方法执行仅存在于ClassB中.使用是否有意义:

super.execute();
Run Code Online (Sandbox Code Playgroud)

可能就够了:

execute();
Run Code Online (Sandbox Code Playgroud)

?谢谢.

java methods inheritance super

2
推荐指数
1
解决办法
281
查看次数

使用super.toString将String和int添加到父类中

好的,所以我的程序有4个不同的类,我正在尝试在我的一个子类中添加一个新变量.我将展示所涉及的所有类的代码,然后解释究竟是什么错误.

第一个代码是我的人员类:

class Person {
private String myName; // name of the person
private int myAge; // person's age
private String myGender; // 'M' for male, 'F' for female

// constructor
public Person(String name, int age, String gender) {
    myName = name;
    myAge = age;
    myGender = gender;
}

public String getName() {
    return myName;
}

public int getAge() {
    return myAge;
}

public String getGender() {
    return myGender;
}

public void setName(String name) {
    myName = name;
}

public …
Run Code Online (Sandbox Code Playgroud)

java string class super

2
推荐指数
1
解决办法
1万
查看次数

在方法的开头或结尾调用super()有什么区别?

我试图了解它是如何super()工作的.我理解它的作用,但我不了解幕后发生的事情的机制.我不太清楚的一点是:

class b(a):
    def __init__(self, name, age):
        self.name=name
        self.age=age
        super(b, self).__init__(name, age)
Run Code Online (Sandbox Code Playgroud)

和:

class b(a):
    def __init__(self, name, age):
        super(b, self).__init__(name, age)
        self.name=name
        self.age=age
Run Code Online (Sandbox Code Playgroud)

也许这两个例子没有区别,但我知道还有其他情况需要super()处理问题.例如,这个Django方法我前几天需要帮助,我被指示移动super()到if语句之上,而不是在底部.我想知道为什么这很重要.

class Mymodel(models.Model):
    photo = models.ImageField(upload_to="...", blank=True)

def save(self, *args, **kwargs):
    image_resized = kwargs.pop('image_resized',False)
    super(Mymodel, self).save(*args, **kwargs)
    if self.photo and image_resized:
        basewidth = 300
        filename = self.get_source_filename()
                image = Image.open(filename)
        wpercent = (basewidth/float(image.size[0]))
        hsize = int((float(image.size[1])*float(wpercent)))
        img = image.resize((basewidth,hsize), PIL.Image.ANTIALIAS)
        self.photo = img
        self.save(image_resized = True)
Run Code Online (Sandbox Code Playgroud)

python super

2
推荐指数
1
解决办法
947
查看次数

如何在Angular 2中调用方法从一个组件到另一个组件?

我有两个不同的组件 - 一个组件具有列表用户的click事件方法,我想继承(调用)详细页面上的click事件方法(另一个组件))

怎么做,请帮帮我...

答案更新

谢谢你的答案.

我通过在构造函数中使用扩展类和使用super()来获得解决方案.

头等舱

export class TeamsView {
   constructor(private test:TestService){
   }
   ...
}
Run Code Online (Sandbox Code Playgroud)

二等

export class TeamsForm extends TeamsView {
   constructor(private test:TestService, private router: Router){
     super(test)
   }
   ...
}
Run Code Online (Sandbox Code Playgroud)

extends super angular2-components angular

2
推荐指数
1
解决办法
2万
查看次数

理解Java super()构造函数

我正在尝试理解Java super()构造函数.我们来看看下面的课程:

class Point {
    private int x, y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public Point() {
        this(0, 0);
    }
}
Run Code Online (Sandbox Code Playgroud)

这个类将编译.如果我们创建一个新Point对象Point a = new Point();,那么将调用没有参数的构造函数:Point().

如果我错了,请纠正我,在做之前this(0,0),Class将调用构造函数,然后Point(0,0)才会调用它.如果这是真的,那么说super()默认情况下是否正确?

现在让我们看一下相同的代码并进行一些小改动:

class Point {

        private int x, y;

        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }

        public Point() {
            super();   // this is the change. …
Run Code Online (Sandbox Code Playgroud)

java constructor super

2
推荐指数
1
解决办法
181
查看次数

java:super必须首先在构造函数中

我知道这已经被问过了,但他们的情况与我的情况大不相同。

class Derived extends Base {
    public Derived()
    {
        try {
           super();
        } catch (Exception e) {
            ....
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是如何解决必须首先处理super的问题-我需要将其包装在try / except块中,并且无法编译。

较早的答案都没有涉及try / except的问题,因此请不要告诉我这个问题已经得到解答。

java try-catch super

2
推荐指数
1
解决办法
61
查看次数