我正在处理一个扩展的类JFrame.
它不是我的代码,它super在开始构建GUI之前调用它.我想知道为什么这样做,因为我总是只是访问超类的方法而不必调用super();
让我们假设我有以下两个类
public class alpha {
public alpha(){
//some logic
}
public void alphaMethod1(){
//some logic
}
}
public class beta extends alpha {
public beta(){
//some logic
}
public void alphaMethod1(){
//some logic
}
}
public class Test extends beta
{
public static void main(String[] args)
{
beta obj = new beta();
obj.alphaMethod1();// Here I want to call the method from class alpha.
}
}
Run Code Online (Sandbox Code Playgroud)
如果我发起一个beta类型的新对象,我怎样才能执行alphamethod1类alpha而不是beta中的逻辑?我可以使用super().alphaMethod1()< - 我想知道这是否可行.
Eclipse IDE中的Autotype允许我alphamethod1从类alpha或类中选择beta.
我正在用GDB调试一个C++程序.
我有一个指向某个类的对象的指针.指针被声明为某些超类,它由几个子类扩展.
对象中没有字段指定此对象的精确类类型,但是定义了一些虚函数(例如bool is_xxx())以在运行时告知类类型.
有没有办法在不调用这些虚函数的情况下告诉GDB中对象的精确类类型.当程序是多线程的时,在GDB中调用这些函数可能会产生令人困惑的结果.
我有两节课.
public class A {
public Object method() {...}
}
public class B extends A {
@Override
public Object method() {...}
}
Run Code Online (Sandbox Code Playgroud)
我有一个B的实例.如何从b调用A.method()?基本上,与从B调用super.method()的效果相同.
B b = new B();
Class<?> superclass = b.getClass().getSuperclass();
Method method = superclass.getMethod("method", ArrayUtils.EMPTY_CLASS_ARRAY);
Object value = method.invoke(obj, ArrayUtils.EMPTY_OBJECT_ARRAY);
Run Code Online (Sandbox Code Playgroud)
但是上面的代码仍然会调用B.method()
出于某种原因,该super()方法并不总是按预期运行,选择返回:
TypeError('super(type, obj): obj must be an instance or subtype of type)'
Run Code Online (Sandbox Code Playgroud)
我理解错误的含义.我不明白为什么它会出现错误.这是破解的代码片段.系统中的所有对象都是新样式对象.
真正有趣的是,这个错误并不总是出现.我不知道是什么导致了它.该super()在方法Retrieval中传递Retrieval类,然后本身作为一个对象,它是,据我所知,究竟如何super()是应该被调用.
有什么想法吗?
在文件DBConnection.py中:
class DBAdminConnection(object):
def __init__(self):
self.user = DBUserConnection().user
self.submissions = DBSubmissionConnection()
Run Code Online (Sandbox Code Playgroud)
在文件Retrieval.py中
class Retrieval(DBConnection.DBAdminConnection):
def __init__(self, username=None, password=None, unique_key=None):
super(Retrieval,self).__init__()
if username and password:
self.username = username
self.user.login(username,password, config.DATABASE)
if self.user.error:
raise UserLoginError(username)
self.unique_key = unique_key
Run Code Online (Sandbox Code Playgroud) 我有以下类,使用init方法:
class user {
var name:String
var address:String
init(nm: String, ad: String) {
name = nm
address = ad
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试子类化这个类,但我不断收到错误super.init():
class registeredUser : user {
var numberPriorVisits: Int
// This is where things start to go wrong - as soon as I type 'init' it
// wants to autocomplete it for me with all of the superclass' arguments,
// and I'm not sure if those should go in there or not:
init(nm: String, ad: String) { …Run Code Online (Sandbox Code Playgroud) 我想重构一些目前由超类和两个子类组成的代码.
这些是我的课程:
public class Animal {
int a;
int b;
int c;
}
public class Dog extends Animal {
int d;
int e;
}
public class Cat extends Animal {
int f;
int g;
}
Run Code Online (Sandbox Code Playgroud)
这是我目前的代码:
ArrayList<Animal> listAnimal = new ArrayList<>();
if (condition) {
Dog dog = new Dog();
dog.setA(..);
dog.setB(..);
dog.setC(..);
dog.setD(..);
dog.setE(..);
listAnimal.add(dog);
} else {
Cat cat = new Cat();
cat.setA(..);
cat.setB(..);
cat.setC(..);
cat.setF(..);
cat.setG(..);
listAnimal.add(cat);
}
Run Code Online (Sandbox Code Playgroud)
如何重构有关公共属性的代码?
我想要这样的东西:
Animal animal = new Animal();
animal.setA(..);
animal.setB(..);
animal.setC(..); …Run Code Online (Sandbox Code Playgroud) 我已经阅读了很多关于Java中的接口和类继承的内容,我知道如何做到这两点,我觉得我对两者都有很好的感觉.但似乎没有人真正将两者并排比较,并解释了何时以及为什么要使用其中一种.我没有发现很多次实现接口比扩展超类更好的系统.
那么什么时候实现一个接口,什么时候扩展超类呢?
在C#中,如何获取对给定类的基类的引用?
例如,假设您有某个类MyClass,并且您想获得对MyClass"超类" 的引用.
我记得这样的事情:
Type superClass = MyClass.GetBase() ;
// then, do something with superClass
Run Code Online (Sandbox Code Playgroud)
但是,似乎没有合适的GetBase方法.
superclass ×10
inheritance ×5
java ×5
subclass ×2
c# ×1
c++ ×1
constructor ×1
gdb ×1
instanceof ×1
interface ×1
methods ×1
oop ×1
overriding ×1
python ×1
reflection ×1
super ×1
swift ×1
types ×1