在JavaScript中这两个例子有什么区别: 先决条件: function SomeBaseClass(){ } SomeBaseClass.prototype = { doThis : function(){ }, doThat : function(){ } } inheritance示例A使用Object.create: function MyClass(){ } MyClass.prototype = Object.create(SomeBaseClass.prototype); inheritance示例B使用新的关键字 function MyClass(){ } MyClass.prototype = new SomeBaseClass(); 这两个例子似乎都是一样的。 你什么时候select一个呢? 还有一个问题:考虑下面的链接(第15行)中的代码,其中对函数自己的构造函数的引用存储在原型中。 为什么这是有用的? https://github.com/mrdoob/three.js/blob/master/src/loaders/ImageLoader.js 摘录(如果你不想打开链接): THREE.ImageLoader.prototype = { constructor: THREE.ImageLoader }
在Java中,具有“受保护”修饰符的成员不仅可以被同一个类和子类访问,而且也可以被同一个包中的每个人访问,这是什么原因呢? 我想知道语言devise的原因,而不是实际的应用(如testing)
在C ++ 11中,inheritance构造函数是什么意思? 如果这是我认为的(基类构造函数被引入派生类的范围),它对我的代码有什么影响? 这个function有什么用途?
我有一个抽象类: abstract class AbstractDataExport { public string name; public abstract bool ExportData(); } 我有派生自AbstractDataExport的类: class XmlExport : AbstractDataExport { new public string name = "XmlExporter"; public override bool ExportData() { … } } class CsvExport : AbstractDataExport { new public string name = "CsvExporter"; public override bool ExportData() { … } } 有没有可能做这样的事情? (伪代码:) foreach […]
我发现以下两项工作: class Foo(): def a(self): print "hello" class Foo(object): def a(self): print "hello" 所有的Python类都应该扩展对象吗? 没有扩展对象有什么潜在的问题吗?
我很困惑如何重写不同于隐藏在Java中。 任何人都可以提供有关这些不同的更多细节? 我读了Java教程,但示例代码仍然让我感到困惑。 更清楚的是,我明白Overriding。 我的问题是,我没有看到,除了一个在实例层面,另一个在课堂层面,隐藏是不同的。 看一下Java教程代码: public class Animal { public static void testClassMethod() { System.out.println("Class" + " method in Animal."); } public void testInstanceMethod() { System.out.println("Instance " + " method in Animal."); } } 那么我们有一个子类猫: public class Cat extends Animal { public static void testClassMethod() { System.out.println("The class method" + " in Cat."); } […]
在Java中禁止inheritance的好理由是什么,例如通过使用单个私有的无参数构造函数来使用最终的类或类? 做出最终方法的好理由是什么?
考虑这个 – 一个基类A,从Binheritance的类B,从Binheritance的类C.什么是在构造函数中调用父类构造函数的通用方法? 如果这仍然听起来太模糊,这是一些代码。 class A(object): def __init__(self): print "Constructor A was called" class B(A): def __init__(self): super(B,self).__init__() print "Constructor B was called" class C(B): def __init__(self): super(C,self).__init__() print "Constructor C was called" c = C() 这是我现在做的。 但是它看起来还是非常通用的 – 你仍然必须手工传递正确的types。 现在,我已经尝试使用self.__class__作为super()的第一个参数,但显然这不起作用 – 如果将它放在C的构造函数中,那么B的构造函数会被调用。 如果你在B中做同样的事情,“self”仍然指向C的一个实例,所以你最终再次调用B的构造函数(这以无限recursion结束)。 现在没有必要考虑钻石inheritance,我只是想解决这个具体问题。
以下代码工作: class Foo(tuple): def __init__(self, b): super(Foo, self).__init__(tuple(b)) if __name__ == '__main__': print Foo([3, 4]) $ python play.py play.py:4: DeprecationWarning: object.__init__() takes no parameters super(Foo, self).__init__(tuple(b)) (3, 4) 但不是以下内容: class Foo(tuple): def __init__(self, a, b): super(Foo, self).__init__(tuple(b)) if __name__ == '__main__': print Foo(None, [3, 4]) $ python play.py Traceback (most recent call last): File "play.py", […]
我怎样才能投射到派生类? 下面的方法都给出了以下错误: 无法从BaseType转换为DerivedType。 没有构造函数可以采取源types,或构造函数重载parsing是模糊的。 BaseType m_baseType; DerivedType m_derivedType = m_baseType; // gives same error DerivedType m_derivedType = (DerivedType)m_baseType; // gives same error DerivedType * m_derivedType = (DerivedType*) & m_baseType; // gives same error