假设我们有这两个类: public class Derived : Base { public Derived(string s) : base(s) { } } public class Base { protected Base(string s) { } } 如何从Base的构造函数中发现Derived是调用者? 这就是我想到的: public class Derived : Base { public Derived(string s) : base(typeof(Derived), s) { } } public class Base { protected Base(Type type, string s) { } } 有没有另外一种方式,不需要传递typeof(Derived) […]
我有三个class级: class A {}; class B : virtual public A {}; class C : virtual public A {}; class D: public B, public C {}; 试图从A *到B *静态转换我得到以下错误: cannot convert from base A to derived type B via virtual base A
我的class级应该同时扩展两个class级: public class Preferences extends AbstractBillingActivity { public class Preferences extends PreferenceActivity { 怎么做? 更新 由于这是不可能的,我应该如何使用AbstractBillingActivity与首选项呢? Upd2 。 如果我使用接口,我应该创build: BillingInterface public interface BillingInterface extends PreferenceActivity, AbstractBillingActivity { } PreferenceActivity public interface PreferenceActivity { } AbstractBillingActivity public interface AbstractBillingActivity { void onCreate(Bundle savedInstanceState); } 接着 public class Preferences implements BillingInterface {
快速前进: 我写这个的目的是为了更好地理解dependency injection和IoC容器,也是为了以后我可以纠正它的错误,并用它来帮助我的几个朋友教他们。 到目前为止,我已经尝试阅读各种框架(laravel,fuel,codeigniter,symfony)的文档,我发现框架有太多不同的方面,我需要使用它,我决定尝试在尝试在框架中使用它们之前,先自己学习每个主要部分。 我花了几个小时search各种各样的意思,看着stackoverflow响应,并阅读各种文章试图了解什么是IoC,以及如何使用它来正确pipe理依赖关系,我相信我明白它在概念上是什么,但我仍然灰色关于如何正确实施。 我认为读这本书帮助我的最好方法是给出我目前对IoC容器和dependency injection的理解,然后让那些比我更好理解的人指出我的理解力不足。 我的理解: 依赖关系是当ClassA的实例需要ClassB的实例来实例化ClassA的新实例时。 dependency injection是当ClassA通过ClassB的构造函数中的参数或通过set〜DependencyNameHere〜(〜DependencyNameHere〜$ param)函数传递ClassB的实例时。 (这是我不完全确定的领域之一) 。 一个IoC容器是一个单例类(在任何给定的时间只能有一个实例实例化),在这里可以注册实例化这个类的对象的具体方法。 下面是我想要描述的一个例子的链接,以及我一直使用的IoC容器的类定义 所以在这一点上,我开始尝试使用IoC容器更复杂的情况。 到目前为止,似乎为了使用IoC容器,我仅限于几乎所有我想创build的具有IoC容器中定义的依赖关系的类的关系。 如果我想创build一个inheritance一个类的类,但是只有在以IoC容器中注册的特定方式创build父类的情况下。 例如:我想创build一个mysqli的子类,但是我想在IoC容器中注册这个类,只用我之前在IoC容器中注册的方式实例化父类。 我想不出没有重复代码的方法(因为这是一个学习项目,我试图尽量保持它的“纯”)。 以下是我想描述的更多的例子。 所以这里是我的一些问题: 我想尽量做到不违反OOP原则? 我知道在c + +我可以使用dynamic内存和复制构造函数来完成它,但我一直无法在PHP中find这种function。 (我会承认,除了__construct之外,使用其他任何魔术方法的经验都很less,但是如果我理解正确的话,可以从阅读和__clone中获得,我不能在构造函数中使用它来实例化子类,父类的实例)。 我的所有依赖类定义应该在哪里与IoC相关? (我的IoC.php应该在顶部有一堆require_once('dependencyClassDefinition.php')吗?我的直觉反应是有更好的方法,但我还没有拿出一个) 我应该在什么文件注册我的对象? 目前在类定义之后,在IoC.php文件中完成对IoC :: register()的所有调用。 在我注册一个需要依赖关系的类之前,是否需要在IoC中注册依赖关系? 由于我没有调用匿名函数,直到我真正实例化了一个在IoC中注册的对象,我猜不到,但它仍然是一个问题。 还有什么我可以忽略,我应该做的或使用? 我试图一次一个脚印,但我也不想知道我的代码是可重用的,最重要的是,对我的项目一无所知的人可以阅读并理解它。 我知道这是非常长的,只是想提前感谢任何花时间阅读的人,甚至更愿意分享他们的知识。
class A { int a = 2, b = 3; public void display() { int c = a + b; System.out.println(c); } } class B extends A { int a = 5, b = 6; } class Tester { public static void main(String arr[]) { A x = new A(); B y = new B(); […]
在Java中这样做是合法的: void spew(Appendable x) { x.append("Bleah!\n"); } 我该怎么做(语法不合法): void spew(Appendable & Closeable x) { x.append("Bleah!\n"); if (timeToClose()) x.close(); } 我希望如果可能的话,强制调用者使用Appendable和Closeable的对象,而不需要特定的types。 有多个标准类可以做到这一点,例如BufferedWriter,PrintStream等 如果我定义我自己的界面 interface AppendableAndCloseable extends Appendable, Closeable {} 这将不起作用,因为实现Appendable和Closeable的标准类不会实现我的接口AppendableAndCloseable(除非我不了解Java以及我认为的…空接口仍然增加超越其超接口的唯一性)。 我能想到的最接近的是做下面的一个: select一个接口(例如Appendable),并使用运行时testing来确保参数是其他instanceof 。 下行:编译时没有发现问题。 需要多个参数(捕获编译时正确性,但看起来很傻): void spew(Appendable xAppend, Closeable xClose) { xAppend.append("Bleah!\n"); if (timeToClose()) xClose.close(); }
我知道我可以这样做: class Foo; 但我可以转发声明一个类从另一个inheritance,如: class Bar {}; class Foo: public Bar; 一个示例用例将是co-variant引用返回types。 // somewhere.h class RA {} class RB : public RA {} …然后在另一个不包含some.h的头文件中 // other.h class RA; class A { public: virtual RA* Foo(); // this only needs the forward deceleration } class RB : public RA; // invalid but… class B { public: […]
我想将主机对象Error扩展为自定义的UploadError类。 以下示例在编译时失败: class UploadError extends Error { constructor(message: string, private code: number) { super(message); } getCode(): number { return this.code; } } 当我运行TypeScript编译器tsc出现以下错误: UploadError.ts(1,0): A export class may only extend other classes, Error is an interface. 看来Error被定义为一个接口。 如果有人知道这个实现的名字是什么,它会让我非常高兴:-) 更新:我想使用Typescriptsinheritance不是原型inheritance,就像我目前用来破解这个: function UploadError (message: string, code: number) { this.message = message; this.code = code; } UploadError.prototype = […]
在C#编程语言中, Krzysztof Cwalina在注释中指出: 我们明确地决定不添加对多重inheritance的支持[…]缺乏多重inheritance迫使我们添加接口的概念,而接口的概念反过来又对框架的演化,更深的inheritance层次结构和其他许多问题负责问题。 接口是面向对象编程语言的核心概念。 我不遵循“强迫我们添加接口的概念”的意思, Krzysztof是否意味着必须对使用否则将使用多重inheritance的接口进行某些devise决定? 或者,他的意思是说interface是由于缺乏多重inheritance而引入C#的? 你能提供一个例子吗?
我正在运行Python 2.5,因此这个问题可能不适用于Python 3.当您使用多重inheritance创build钻石类层次结构并创build派生类的对象时,Python将执行Right Thing(TM)。 它调用派生类的构造函数,然后从左到右列出父类,然后是祖父类。 我熟悉Python的MRO ; 那不是我的问题。 我很好奇从超级对象返回的对象实际上是如何pipe理的,以正确的顺序在父类中调用超级调用。 考虑这个例子代码: #!/usr/bin/python class A(object): def __init__(self): print "A init" class B(A): def __init__(self): print "B init" super(B, self).__init__() class C(A): def __init__(self): print "C init" super(C, self).__init__() class D(B, C): def __init__(self): print "D init" super(D, self).__init__() x = D() 代码做直观的事情,它打印: D init B init C […]