Interfaces
كما نعلم ان لغات مثل ال C# و Java لاتدعم ال Multiple inheritance فكل ماتستطيع عمله هو Inheritance من One class بعكس ال C++ مثلا.
فالمهم اللغات بتدعم إستخدام ال Interfaces وبتسمحلك انك تعمل Inheritance من اكثر من Interface داخل نفس ال Class .
ال Interface زى الClass بس من غير implementation يعنى Signature بس ، بتشمل عدة اشياء مثل Method و Properties ولكن بمجرد إستخدامك ليها يجب عليك انك تعمل implement للكود فى ال Class .
يفضل لما تيجى تسمى ال Interface تستخدم حرف ال I كبداية مثال
IMyInterface1, ImyInterface, ... etc
Declaring the interfaces
كود:
public interface ImyInterface1 { void print(); void println(); } public interface ImyInterface2 { void Draw(); void Draw3D(); }
كود:
public class myClass : ImyInterface1, ImyInterface2 //Using interfaces { //implementing the code for the methods public void print() { Console.WriteLine("Method print() is implemented "); } public void println() { Console.WriteLine("Method println() is implemented "); } public void Draw() { Console.WriteLine("Method Draw() is implemented "); } public void Draw3D() { Console.WriteLine("Method Draw3D() is impelmented "); } }
كود:
class Program { static void Main(string[] args) { myClass myObject = new myClass(); myObject.Draw(); myObject.Draw3D(); myObject.print(); myObject.println(); } }
تقدر تعمل Inheritance داخل ال Interface نفسها بحيث إنك تملاها بالميثودز من Interface تانية .
البروجكت اللى تم الشرح عليه مرفق .
تعليق