About Monkey 2 › Forums › Monkey 2 Programming Help › What is an interface? (POO)
This topic contains 4 replies, has 4 voices, and was last updated by NoOdle 1 week ago.
-
AuthorPosts
-
February 8, 2019 at 2:40 am #16057
Going deeper into this object-oriented programming, I wondered what an interface is, and what its possible uses are, some simple explanation to understand.
Thank you.
February 8, 2019 at 8:07 pm #16058– https://en.wikipedia.org/wiki/Interface_(object-oriented_programming)
An Interface contains declarations of methods, the parameters, and the return type.
Any class that implements the Interface is required to implement **everything**
from the Interface, exactly like it was declared.
It is like a contract/agreement and you can trust that the specific methods
are implemented in the classes.February 8, 2019 at 8:34 pm #16059The best example in monkey2 is IContainer interface.
Stack and lists for example implements IContainer and can therfore be used with eachin loops.
You can create you own container that implements icontainer and you’ll be able to use it with eachin loops.An interface force you to respect what was planned in some way. And if you forget to implement a part of it, you’ll have a compile error.
Interfaces are usefull for generic type constrains too. (using the ‘where’ keyword)
https://www.codingame.com/playgrounds/2290/demystifying-c-generics/generics-constraintsFebruary 9, 2019 at 6:02 am #16060Small example:
Monkey
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758#Import "<std>"#Import "<mojo>"Function Main()New mojo.app.AppInstanceLocal console := New ConsoleLocal printer := New PrinterLocal message := New MessageHelloWorld( console )HelloWorld( printer )HelloWorld( message )End#RemInterface IOutput#EndInterface IOutputMethod PrintLn( text:String )End#RemFunction HelloWorldParameter 1: Takes any object (instance of a class)that implements IOutput#EndFunction HelloWorld( obj:IOutput )obj.PrintLn( "Hello World!" )End#Remclass Console#EndClass Console Implements IOutputMethod PrintLn( s:String )Print sEndEnd#Remclass Printer#EndClass Printer Implements IOutputMethod PrintLn( s:String )Print "Printing to printer... ~n~t"+sEndEnd#Remclass Message#EndClass Message Implements IOutputMethod PrintLn( s:String )std.requesters.Notify( "Message", s )EndEndFebruary 21, 2019 at 10:12 pm #16080An interface allows you to add specific functionality to any class, avoiding deep/complex class hierarchies. You can store instances of any class that implement the same interface in a collection and invoke any of the interface methods.
Sorry if that makes no sense, I’m not the best at explaining things. Here’s a quick untested example:
Monkey
123456789101112131415161718192021222324252627282930313233343536373839404142434445#Import "<std>"Using std.collectionsInterface IDrawableMethod Draw : Void()End InterfaceClass Dog Implements IDrawableMethod Draw : Void()Print "Draw a dog"End MethodEnd ClassClass Button Implements IDrawableMethod Draw : Void()Print "Draw a button"End MethodEnd ClassFunction Main()Local dog : Dog = New Dog()Local btn : Button = New Button()Local drawList : List< IDrawable > = New List< IDrawable >()drawList.AddLast( dog )drawList.AddLast( btn )For Local this : IDrawable = Eachin drawListthis.Draw()NextEnd Function -
AuthorPosts