Tag: inheritance

为什么Java在编译时绑定variables?

考虑下面的示例代码 class MyClass { public String var = "base"; public void printVar() { System.out.println(var); } } class MyDerivedClass extends MyClass { public String var = "derived"; public void printVar() { System.out.println(var); } } public class Binding { public static void main(String[] args) { MyClass base = new MyClass(); MyClass derived = new MyDerivedClass(); System.out.println(base.var); System.out.println(derived.var); […]

JavaScript中的组合,inheritance和聚合

网上有很多关于组合与inheritance的信息,但是我还没有find像JavaScript这样的好例子。 使用下面的代码来演示inheritance: function Stock( /* object with stock names and prices */ ) { for (var company_name in arguments[0]) { // copy the passed object into the new object created by the constructor this[company_name] = arguments[0][company_name]; } } // example methods in prototype, their implementation is probably redundant for // this question, but list() returns […]

我如何初始化派生类构造函数中的基类成员variables?

为什么我不能这样做? class A { public: int a, b; } class B : public A { B() : A(), a(0), b(0) { } }

inheritance一个通用的基类,应用一个约束,并在C#中实现一个接口

这是一个语法问题。 我有一个通用的类inheritance自一个通用的基类,并正在对其中一个types参数应用约束。 我也希望派生类实现一个接口。 对于我的生活,我似乎无法弄清楚正确的语法。 这是我的: DerivedFoo<T1,T2> : ParentFoo<T1, T2> where T2 : IBar { … } 首先想到的是这样的: DerivedFoo<T1,T2> : ParentFoo<T1, T2> where T2 : IBar, IFoo { … } 但这是不正确的,因为这导致T2需要实现IBar和IFoo,而不是DerivedFoo来实现IFoo。 我尝试了一些谷歌search,使用冒号,分号等,但我已经缩短了。 我相信答案是头脑简单。

在Rails中跳过before_filter

为了清楚起见,名称和对象已被简化。 基本概念保持不变。 我有三个控制器: dog , cat和horse 。 这些控制器都从控制器animalinheritance。 在控制器animal ,我有一个用于validation用户的filter: before_filter :authenticate def authenticate authenticate_or_request_with_http_basic do |name, password| name == "foo" && password == "bar" end end 在dog的show行动中,我需要对所有用户开放访问(跳过authentication)。 如果我要为dog单独编写validation,我可以这样做: before_filter :authenticate, :except => :show 但是,由于dog从animalinheritance的,我没有访问控制器的具体行为。 在animal控制器中join:except => :show不仅可以跳过对dog的show动作的authentication,还可以跳过cat和horse的show动作。 这种行为是不希望的。 在inheritanceanimal同时,我怎样才能跳过只用于dog的show动作的authentication?

如果在派生类中覆盖此属性,如何调用基类的属性?

我正在把我的一些阶级从吸收者和制定者的广泛使用改变为对属性更为pythonic的使用。 但是现在我被卡住了,因为我之前的一些getter或setter会调用基类的相应方法,然后执行其他的操作。 但是,怎样才能完成属性? 如何在父类中调用属性getter或setter? 当然,调用属性本身会给出无限recursion。 class Foo(object): @property def bar(self): return 5 @bar.setter def bar(self, a): print a class FooBar(Foo): @property def bar(self): # return the same value # as in the base class return self.bar # –> recursion! @bar.setter def bar(self, c): # perform the same action # as in the base class self.bar […]

如何使用inheritancebuild模RESTful API?

我有一个对象层次结构,我需要通过一个RESTful API公开,我不知道我的URL应该如何结构和他们应该返回。 我找不到任何最佳做法。 假设我有从动物inheritance的狗和猫。 我需要对狗和猫进行CRUD操作; 我也希望能够对动物做一般的操作。 我的第一个想法是做这样的事情: GET /animals # get all animals POST /animals # create a dog or cat GET /animals/123 # get animal 123 事情是/动物集合现在是“不一致的”,因为它可以返回并采取不完全相同的结构(狗和猫)的对象。 有一个集合返回具有不同属性的对象是否被认为是“RESTful”? 另一个解决scheme是为每个具体types创build一个URL,如下所示: GET /dogs # get all dogs POST /dogs # create a dog GET /dogs/123 # get dog 123 GET /cats # get all cats POST […]

通过测验了解C#中的嵌套generics类

在和同事谈论C#时,他向我展示了一些C#代码,我必须预测它的输出。 这看起来很简单,但事实并非如此。 我真的不明白为什么C#这样做。 代码: public class A<T1> { public T1 a; public class B<T2> : A<T2> { public T1 b; public class C<T3> : B<T3> { public T1 c; } } } class Program { static void Main(string[] args) { A<int>.B<char>.C<bool> o = new A<int>.B<char>.C<bool>(); Console.WriteLine(oaGetType()); Console.WriteLine(obGetType()); Console.WriteLine(ocGetType()); Console.ReadKey(); } } 输出是: System.Boolean System.Char System.Int32 […]

扩展React.js组件

我最欣赏Backbone.js的一件事就是简单而优雅的inheritance是如何工作的。 我开始熟悉React,并且不能真正find类似于这个Backbone代码的反应 var Vehicle = Backbone.View.extend({ methodA: function() { // … } methodB: function() { // … } methodC: function() { // … } }); var Airplane = Vehicle.extend({ methodC: function() { // Overwrite methodC from super } }); 在反应中,我们有mixin ,如果我们这样做的话,使用那些我们可能会接近上面的例子 var vehicleMethods = { methodA: function() { // … } methodB: function() { // […]

调用父类__init__多重inheritance,什么是正确的方法?

假设我有一个多inheritance的场景: class A(object): # code for A here class B(object): # code for B here class C(A, B): def __init__(self): # What's the right code to write here to ensure # A.__init__ and B.__init__ get called? 有两种典型的方法来编写C的__init__ : (旧式) ParentClass.__init__(self) (newer-style) super(DerivedClass, self).__init__() 但是,无论哪种情况,如果父类( A和B ) 不遵循相同的约定,那么代码将无法正常工作 (有些可能会被错过,或被多次调用)。 那么再次正确的方法是什么? 很容易说“只要一致,遵循一个或另一个”,但是如果A或B来自第三方库,那么呢? 有没有一种方法可以确保所有的父类构造函数被调用(并按正确的顺序,并且只有一次)? 编辑:看我的意思,如果我这样做: class A(object): def […]