草庐IT

inheritence

全部标签

inheritance - Go 中创建复杂结构层次结构的惯用方法是什么?

我正在用Go编写一个解释器,我正在寻找存储AST的惯用方式。我阅读了Go编译器源代码,似乎他们使用带有空方法的接口(interface)来表示AST。例如,我们有以下层次结构,Object--Immovable----Building----Mountain--Movable----Car----Bike上面的层次结构就是这样以“空方法”的方式实现的。typeObjectinterface{object()}typeImmovableinterface{Objectimmovable()}typeBuildingstruct{...}typeMountainstruct{...}typ

types - 如果类型 T2 基于类型 T1,那么从 T1 到 T2 是否有任何类型的 "inheritance"?

如果typeT2是基于typeT1,除了共享相同的数据字段外,T1和之间有什么关系吗>T2?packagemainimport"fmt"typeT1struct{sstring}func(v*T1)F1()string{returnv.s}typeT2T1func(v*T2)F2()string{returnv.s}funcmain(){vart1=T1{"xyz"}vart2=T2{"pdq"}s0:=t2.F1()//error-expectedoks1:=((*T1)(&t2)).F1()//ok-expecteds2:=((*T2)(&t1)).F2()//ok-notexpe

C++ 元编程 : A template parameter which *must* inherit an abstract class

我有一个用于可比较+哈希值的抽象类:classKey{public:virtualbooloperator==(constKey&)const=0;virtualbooloperator!=(constKey&)const=0;virtualu32hashcode()const=0;};还有一些继承这个的具体类C。classC:publicKey{private:u32a,b;public:staticconstC&null;//aprototypeforrepresentinga"novalue"C//Somereasonableimplementation;it'sjustapai

C++ : Multiple inheritance with polymorphism

(请原谅菜鸟问题)我有4节课:classPerson{};classStudent:publicPerson{};classEmployee:publicPerson{};classStudentEmployee:publicStudent,publicEmployee{};基本上Person是基类,它是Student和Employee的直接子类。StudentEmployee使用多重继承来继承Student和Employee。Personpat=Person("Pat");Studentsam=Student("Sam");Employeeem=Employee("Emily");S

C++ OO设计: Inheritance of template parameter

我有一个以Base为基类的继承链。我希望能够编写一个继承Base和可能的另一个Base派生类的类模板。我可以使用虚拟继承,但我找到了另一种解决方案。我想知道它是否是常见的/可观的/合法的类设计:编写一个类模板,其中模板参数是它派生的类,即它必须是Base或Base派生类。在构造函数中,我可以使用静态断言来真正确保用户没有使用任何非法类作为模板参数。如果它有效,我将永远不会有虚拟继承问题......问题是,这样做是可以的。我在其他项目中从未见过它,所以我想在使用它之前先确定一下。编辑:为了确保我不会混淆你,这里有一些代码:classBase{};classDerived:publicBa

c++ - 澄清 Sean Parent 的谈话 "Inheritance is the base class of evil"

SeanParent的演讲,Inheritanceisthebaseclassofevil,表示多态性不是类型的属性,而是如何使用它的属性。作为一个经验法则,不要使用继承来实现接口(interface)。这样做的许多好处之一是类的去虚拟化,这些类仅仅因为它们实现了一个接口(interface)而具有虚函数。这是一个例子:classDrawable{public:virtualvoiddraw()=0;};classDrawA:publicDrawable{public:voiddraw()override{//dosomething}};classUseDrawable{public:

c++ - 有效 C++ : discouraging protected inheritance?

我正在阅读ScottMeyers的EffectiveC++(第三版),并在第32项:确保公共(public)继承是页面上的“is-a”中的一段中151他发表评论(我用粗体表示):Thisistrueonlyforpublicinheritance.C++willbehaveasI'vedescribedonlyifStudentispubliclyderivedfromPerson.Privateinheritancemeanssomethingentirelydifferent(seeItem39),andprotectedinheritanceissomethingwhosemea

node.js - coffeescript 中的 'extends' 和 node.js 中的 'util.inherits' 之间的区别

我最近在学习Node.js。我对Node.js中的函数util.inherits有疑问。我可以在CoffeeScript中使用extends来替换它吗?如果不是,它们之间有什么区别? 最佳答案 是的,您可以使用extends代替它。至于区别?让我们先来看看CoffeeScript:classBextendsA我们来看看theJavaScripttheCoffeeScriptcompilerproduces对于这个JavaScript:varB,__hasProp={}.hasOwnProperty,__extends=functio

javascript - util.inherits - 替代或解决方法

我是node中的n00b,发现util.inherits()非常有用,除了它似乎替换了原始对象的整个原型(prototype)。例如:varmyClass=function(name){this._name=name;};myClass.prototype={(...)};util.inherits(myClass,require('events').EventEmitter);似乎抹去了我原来的原型(prototype)。这给我带来了两个不便:1-我必须在调用inherits,后向我的原型(prototype)声明添加属性varmyClass=function(name){this.

node.js - util.inherits - 如何在实例上调用 super 的方法?

我在玩util.inheritsmethodfromnode.js并且似乎无法获得所需的行为。varutil=require("util");functionA(){this.name='old';}A.prototype.log=function(){console.log('myoldnameis:'+this.name);};functionB(){A.call(this);this.name='new';}util.inherits(B,A);B.prototype.log=function(){B.super_.prototype.log();console.log('myn