I have two main concepts related to the interface that I need in order to have a better understanding.
1) How to use interfaces if I want to use only part of the interface methods in this class? For example, my FriendlyCat class inherits from Cat and implements ICatSounds. ICatSounds provides MakeSoftPurr () and MakeLoudPurr () and MakePlayfulMeow (). But it also provides MakeHiss () and MakeLowGrowl () - both of which I don't need for my FriendlyCat class.
When I try to implement only some of the methods open by the interface, the compiler complains that others (which I do not need) were not implemented.
Is the answer to this that I should create an interface that contains only the methods that I want to expose? For example, from my CatSounds class will I create IFriendlyCatSounds? If so, then what happens when I want to use other methods in a different situation? Do I need to create a different user interface? It doesn't seem like a good design to me.
It seems I should be able to create an interface with all the relevant methods (ICatSounds), and then choose which methods I use based on the implementation (FriendlyCat).
2) My second question is pretty simple, but still confusing for me. When I implement the interface (using Shift + Alt + F10), I get the interface methods with "throw new NotImplementedException ();" in organism. What else do I need to do other than referring to the interface method that I want to expose in my class? I am sure that this is a great conceptual pack, but similarly to inheriting from the base class, I want to access the methods displayed by the interface without adding or changing. What does this compiler expect me to implement?
- EDIT -
# 1, .
# 2. ,
. ? , ICatSounds
MakeSoftPurr() MakeLoudPurr(),
CatSounds , . :
public class FriendlyCat: Cat, ICatSounds
{
...
public void ICatSounds.MakeLoudPurr()
{
throw new NotImplementedException();
}
public void ICatSounds.MakeSoftPurr()
{
throw new NotImplementedException();
}
}
, ,
- ? - :
FriendlyCat fcat = new FriendlyCat();
fcat.MakeSoftPurr();
, , ,
. ,
, ,
, ?
...