On 12/09/2012 10:23 AM, deed wrote:
> interface I
> {
> void setX(int x);
> int getX();
> }
>
> class C : I
> {
> int x, y;
>
> void setX(int x) { this.x = x; }
> int getX() { return x; }
> void setY(int y) { this.y = y }
> int getY() { return y; }
> }
>
>
> void main()
> {
> auto obj = new C; // Want new C to instantiate obj with static type I.
> obj.setX(3); // Ok, sets x to 3.
> obj.getY(); // Error, not specified in the interface.
> }
new always returns an object of the actual type the programmer
requested. It is up to the programmer what interface the object needs to
be used with:
C obj = new C;
Now the code works because getY() is being called on a C, which does
have the definition of that function.
> - Is it possible to enforce, from within the class, a certain static
> interface type or ancestor type when instantiating with new? If so, how
> is it done?
Not possible.
> - Also, is it possible for a class to implement multiple interfaces and
> choose static type among them when instantiated, based on static class
> state or arguments passed to the constructor?
Again, it is always the same type of object. That object can be used by
its many interfaces.
Ali