是否有可能只接受类的泛型类型的超类型?
我正在寻找的是:
class <T extends Object> MyClass {
public <TS super T> void myMethod(TS someObjectSuperToTheClass) {
//do stuff
}
}
Run Code Online (Sandbox Code Playgroud)
我不再需要它了(并且它可能不是那么有用)但我很好奇这是否可能,如果没有,为什么.
我是Objective CI的新手,正在尝试一些示例程序.我无法理解自我和超级方法如何在目标C中工作.在下面的pgm中,CashTransaction.m [super trackSpending:amount]被调用并且在CreditCardTransaction.m中[self] trackspending:amount]被调用.我找不到self和super.super之间的区别用于调用基类重写方法.self用于调用子类重写方法.这就是我的理解.请纠正我,如果我错了.谢谢你.
#import <Foundation/Foundation.h>
#import "BudgetObject.h"
#import "Transaction.h"
#import "CashTransaction.h"
#import "CreditCardTransaction.h"
int main (int argc, const char * argv[]) {
//!---Creating An Object And Allocating It With Values---
Budget* budget = [Budget new];
[budget createBudget:1000.00 withExchangeRate:1.2500];
//!---Declaring And Adding Elements To An Array---
NSMutableArray* transactions = [[NSMutableArray alloc] initWithCapacity:10];
Transaction* aTransaction;
aTransaction = [Transaction new];
[transactions addObject:aTransaction];
//!---Calculating The No Of Elements In An Array---
int k;
k=[transactions count];
NSLog(@"The count value is:%d",k);
//!---Selecting According …Run Code Online (Sandbox Code Playgroud)
我想知道super.onPreExecute()应该放在哪里?或换句话说,这是正确的代码:
@Override
protected void onPreExecute() {
super.onPreExecute();
RelativeLayout parent = (RelativeLayout) findViewById(R.id.layoutHomeInfo);
RelativeLayout.LayoutParams params = (LayoutParams) parent.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
progress = new ProgressBar(mContext);
parent.addView(progress, params);
}
Run Code Online (Sandbox Code Playgroud)
要么
@Override
protected void onPreExecute() {
RelativeLayout parent = (RelativeLayout) findViewById(R.id.layoutHomeInfo);
RelativeLayout.LayoutParams params = (LayoutParams) parent.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
progress = new ProgressBar(mContext);
parent.addView(progress, params);
super.onPreExecute();
}
Run Code Online (Sandbox Code Playgroud) 请考虑以下代码:
class A(object):
def __init__(self):
print("A.__init__")
super(A, self).__init__() # 1
print("A.__init__ finished")
class B(A):
def __init__(self):
print("B.__init__")
super(B, self).__init__() # 2
print("B.__init__ finished")
class C(A):
def __init__(self):
print("C.__init__")
super(C, self).__init__()
print("C.__init__ finished")
class D(B, C):
def __init__(self):
print("D.__init__")
print("Initializing B")
B.__init__(self) # 3
print("B initialized")
print("Initializing C")
C.__init__(self) # 4
print("C initialized")
print("D.__init__ finished")
D()
# D.__init__
# Initializing B
# B.__init__
# C.__init__
# A.__init__
# A.__init__ finished
# C.__init__ finished
# B.__init__ finished
# B initialized
# Initializing …Run Code Online (Sandbox Code Playgroud) python initialization multiple-inheritance super diamond-problem
我有一个继承自ActiveRecord :: Base的Commentable类和一个继承自Commentables的Event类.
我已经覆盖了这两个类中的destroy方法,并且Event.distroy调用了super.然而,一些意外的事情发生了.具体而言,将删除事件的has_and_belongs_to_many关联.我认为这种情况正在发生,因为一些模块被包含在Commentables和Event类之间,但不确定是否有办法阻止它.
这是简化的代码:
class Commentable < ActiveRecord::Base
has_many :comments
def destroy
comments.destroy_all
self.deleted = true
self.save!
end
end
class Event < Commentable
has_and_belongs_to_many :practitioners, :foreign_key => "commentable_id"
def destroy
#some Event specific code
super
end
end
Run Code Online (Sandbox Code Playgroud)
我不想从数据库中删除行,只需设置一个"已删除"标志.我也不想删除任何关联.但是,在Event.destroy和Commentable.destroy之间的某处,其他一些rails代码会破坏has_and_belongs_to_many表中的记录.
知道为什么会这样,以及如何阻止它?
我遇到了一个困扰我的问题,它是关键字'super',我的测试代码是这样的:
package test;
public class Parent {
private String name;
public Parent(){
this.name = "parent";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void showName(){
System.out.println(this.name);
}
}
public class Child extends Parent{
public Child(){
this.setName("Child");
}
public void showName(){
System.out.println(super.getClass().toString());
System.out.println(super.toString());
super.showName();
System.out.println(super.getName());
}
}
public class Test {
public static void main(String[] args) {
Child d = new Child();
d.showName();
}
}
Run Code Online (Sandbox Code Playgroud)
所以结果是这样的:
class test.Child
test.Child@2207d8bb
Child
Child …Run Code Online (Sandbox Code Playgroud) 我有一个继承的类并覆盖了一个也从基类继承的方法.但问题是中间方法创建了一个异常,我想通过调用第一个声明的方法来绕过它.有没有办法指定忽略第二次调用的mro?
一个例子可能是:
class Base(object):
def __init__(self):
res = "Want this"
print res
class BaseA(Base):
def __init__(self):
res = super(BaseA, self).__init__()
res = "Not this"
print res
class BaseB(BaseA):
def __init__(self):
res = super(BaseB, self).__init()
#At this poing res is "Not this"
#The desire is that it would be "Want this"
print res
Run Code Online (Sandbox Code Playgroud)
非常感谢
PD:像BaseB(Base,BaseA)这样的东西可以工作吗?
我想我想要的是根本不可能,但我想确定.说我有
public class bar {
public bar() {
}
public bar(Object stuff) {
// something done with stuff.
}
}
Run Code Online (Sandbox Code Playgroud)
和一个扩展
public class foobar extends bar {
public foobar() {
super();
// some additional foobar logic here.
}
public foobar(Object stuff) {
// Do both the
// "some additional foobar logic" and
// "something done with stuff" here.
}
}
Run Code Online (Sandbox Code Playgroud)
如何使foobar(对象的东西)尽可能简单,同时避免重复代码?我不能简单地调用超级(东西),因为"一些额外的foobar逻辑"没有完成,我不能只调用这个()因为我不做我想做的事情"东西".
注意: 我意识到在这种情况下我实际上并不需要这样做,因此现在这只是出于理论目的.
我是猴子修补__eq__类的方法。我发现以下作品:
def eq(obj, other):
if isinstance(other, str):
return obj.name.upper() == other.upper()
else:
return object.__eq__(obj, other)
Run Code Online (Sandbox Code Playgroud)
这不起作用:
def eq(obj, other):
if isinstance(other, str):
return obj.name.upper() == other.upper()
else:
return super().__eq__(other)
Run Code Online (Sandbox Code Playgroud)
这有时可行,但有时会引发错误:
def eq(obj, other):
if isinstance(other, str):
return obj.name.upper() == other.upper()
else:
return super().__eq__(self, other)
Run Code Online (Sandbox Code Playgroud)
错误:
<ipython-input-128-91287536205d> in eq(obj, other)
3 return obj.name.upper() == other.upper()
4 else:
----> 5 return super().__eq__(self, other)
6
7
RuntimeError: super(): __class__ cell not found
Run Code Online (Sandbox Code Playgroud)
您能解释一下这里发生了什么吗?如何正确替换object为super()?
我为2d游戏创建了一个抽象的形状类,但我在两个形状类中都出错.该错误与super()有关.可能还有其他错误.我还显示了我在代码中得到错误的位置.IS super()适合使用.
形状类
public abstract class Shape {
int Y;
int WIDTH;
int HEIGHT;
int DIAMETER;
public Shape(int Y, int WIDTH, int HEIGHT, int DIAMETER) {
this.Y = Y;
this.WIDTH = WIDTH;
this.HEIGHT = HEIGHT;
this.DIAMETER = DIAMETER;
}
public abstract void paint(Graphics g);
}
Run Code Online (Sandbox Code Playgroud)
球拍类
public class Racquet extends Shape {
int x = 0;
int xa = 0;
private Game game;
public Racquet(int Y, int WIDTH, int HEIGHT) {
super(Y, WIDTH, HEIGHT); // <- **Error Here**
}
public …Run Code Online (Sandbox Code Playgroud) super ×10
java ×5
inheritance ×3
python ×3
activerecord ×1
android ×1
constructor ×1
destroy ×1
generics ×1
objective-c ×1
overloading ×1
properties ×1
python-3.x ×1
self ×1
shapes ×1