我已经search了很多链接,无法理解经典inheritance和原型inheritance之间的区别。 我从中学到了一些东西,但是我仍然对这些概念感到困惑。 原型inheritance优于古典? http://aaditmshah.github.io/why-prototypal-inheritance-matters/ 古典inheritance // Shape – superclass function Shape() { this.x = 0; this.y = 0; } //superclass method Shape.prototype.move = function(x, y) { this.x += x; this.y += y; console.info("Shape moved."); }; // Rectangle – subclass function Rectangle() { Shape.call(this); //call super constructor. } //subclass extends superclass Rectangle.prototype = Object.create(Shape.prototype); 古典inheritance里面使用原型inheritance吗? http://aaditmshah.github.io/why-prototypal-inheritance-matters/ […]
假设我有一个实现两个或多个COM接口的类: class CMyClass : public IInterface1, public IInterface2 { }; 几乎我看到的每个文档都build议,当我为IUnknown实现QueryInterface()时,我显式地将此指针上传到其中一个接口: if( iid == __uuidof( IUnknown ) ) { *ppv = static_cast<IInterface1>( this ); //call Addref(), return S_OK } 问题是为什么我不能只复制这个 ? if( iid == __uuidof( IUnknown ) ) { *ppv = this; //call Addref(), return S_OK } 这些文档通常说,如果我做了后者,我将违反在同一对象上对QueryInterface()的任何调用必须返回完全相同的值的要求。 我不太明白。 他们的意思是,如果我QI()为IInterface2并调用QueryInterface()通过该指针C ++将通过这个略有不同,如果我QI()IInterface2因为C ++将每次使这一点的子对象?
如果我们有一个从多个接口inheritance的类,并且接口具有同名的方法,那么我们如何在我的类中实现这些方法呢? 我们如何指定实现哪个接口的方法?
ES6允许扩展特殊对象。 所以可以从函数inheritance。 这样的对象可以被称为一个函数,但是我怎样才能实现这种调用的逻辑呢? class Smth extends Function { constructor (x) { // What should be done here super(); } } (new Smth(256))() // to get 256 at this call? 任何类的方法通过this获得对类实例的引用。 但是,当它被称为一个函数, this是指window 。 当它被作为一个函数调用时,如何获得对类实例的引用? PS: 同样的问题在俄罗斯。
我有一组构造函数和一个赋值运算符。 class B { public: B(); B(const string & s); B(const B & b){(*this) = b;}; B & operator= (const B & b); private: virtual void foo(); // and other private member variables and functions } 我想创build一个只会覆盖函数foo()的inheritance类D,不需要其他更改。 但我想要D具有相同的一组构造函数,包括复制构造函数和赋值运算符为B: D(const D & d){(*this) = d;}; D & operator= (const D & d); 我是否必须在D中重写它们,还是有办法使用B的构造函数和操作符? 我特别想避免重写赋值运算符,因为它必须访问所有B的私有成员variables。
我有一个包含以下事件的基类: public event EventHandler Loading; public event EventHandler Finished; 在从这个基类inheritance的类中,我试图提出这个事件: this.Loading(this, new EventHandler()); // All we care about is which object is loading. 我收到以下错误: 事件“BaseClass.Loading”只能出现在+ =或 – =(BaseClass)的左侧 我假设我不能像其他遗传成员一样访问这些事件?
有人可以向我解释目标C中类别和inheritance之间的区别吗? 我已阅读维基百科的条目,关于类别的讨论与inheritance没有任何区别。 我也在“开放iPhone开发”一书中讨论了这个话题的讨论,但我仍然不明白。
我有一个通用的接口 public interface Consumer<E> { public void consume(E e); } 我有一个类消耗两种types的对象,所以我想要做一些事情: public class TwoTypesConsumer implements Consumer<Tomato>, Consumer<Apple> { public void consume(Tomato t) { ….. } public void consume(Apple a) { …… } } 显然我不能那样做。 我当然可以自己实施调度,例如 public class TwoTypesConsumer implements Consumer<Object> { public void consume(Object o) { if (o instanceof Tomato) { ….. } else if […]
有没有办法在C#中覆盖返回types? 如果是的话,如果不是为什么,什么是推荐的做法呢? 我的情况是,我有一个抽象的基类和后裔的接口。 我想这样做(好吧不是真的,但作为一个例子!): public interface Animal { Poo Excrement { get; } } public class AnimalBase { public virtual Poo Excrement { get { return new Poo(); } } } public class Dog { // No override, just return normal poo like normal animal } public class Cat { public override RadioactivePoo Excrement { […]
在Ruby中,由于可以包含多个mixin,但只能扩展一个类,所以mixin似乎比inheritance更受欢迎。 我的问题:如果你正在编写必须扩展/包含的代码,那么为什么你要把它变成一个类? 换句话说,你为什么不把它做成一个模块呢? 我只能想到你想要一个类的一个原因,那就是如果你需要实例化类。 但是,在ActiveRecord :: Base的情况下,你不能直接实例化它。 所以不应该是一个模块呢?