oop - Is the function of interfaces primarily for using functions without knowing how a class is built? -


As I understand the interface, they are contracts, I explain it as contract term, that is what is specified in the interface Pre-open, close, read, write for an interface handling files).

But is there a reason to redeem a difficult time that you will need an interface that tells what the class should do exactly, will not you know that you have written in the interface specification?

The only reason that I can look for the interface is in big projects, where you want to use a class without actually knowing it has been created, you need to use it by looking at the interface Will be allowed to know about

I wonder why I should use the interface in projects (or if I want to) that I am only working on it. I am quite convinced that there are more uses for this which I do not see.

I believe most of my beliefs and interpretations and

Writing a group of implementing classes is writing. You may have a pistol, a rifle and a machine gun, then, suppose that you decide to use these classes in such a manner that you want to fire () the action on each of these guns. Can:

  Private Pistol Page 01; Private pistol p02; Private Rifle R 001; Private machine gun MG 01; Public Zero Fire All () {p01.fire (); P02.fire (); R01.fire (); Mg01.fire (); }  

That type of waste is because you have to change the code in some places if you add or remove guns. Or worse, suppose you want to be able to add and remove guns at the runtime: it becomes even harder.

Let's create an interface that will apply to each of the above guns, now we can do it.

private gun [] firearms; Public Zero Fire All () {for (int i = 0; i & lt; firearms.length; ++ i) {firearms [i] .fire (); }}

This is a little better change, will not you?


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -