"methodios" <mr********@gma il.com> a écrit dans le message de news:
11************* *********@g14g2 00...legr oups.com...
| Why would someone cast an object to an interface? Instead of
| isDoc.Status, can't we just use doc.Status ?
In the given example, yes you could do just that, but interfaces are
designed, amongst other things, to allow commoin behaviour across
hierarchies. IOW, you dont have to derive all your classes from one base
class to get a common behaviour. It also allows you to "derive from"
(implement) more than one interface.
So, you can not only have Document implement IStorable, but any other class
that you desire to have the Read, Write and Status behaviour. Thus I can
then have a list of IStorable items and treat them all the same, regardless
of what their real class is.
Also, I can implement the interface explicitly, thereby setting the
properties and methods to be essentially private to clients of the class.
public class Document : IStorable
{
void IStorable.Read( ) {...}
void IStorable.Write (object obj) {...}
public int IStorable.Statu s
{
get { return status;}
set { status = value; }
}
...
}
Joanna
--
Joanna Carter [TeamB]
Consultant Software Engineer