Interface Agenda 2 Declaring Interfaces Implementing Multiple Interfaces

  • Slides: 51
Download presentation
Interface

Interface

Agenda • • 2 Declaring Interfaces Implementing Multiple Interfaces Virtual Implementing Interface Methods. NET

Agenda • • 2 Declaring Interfaces Implementing Multiple Interfaces Virtual Implementing Interface Methods. NET Build-IN Interfaces

Agenda • • 3 Declaring Interfaces Implementing Multiple Interfaces Virtual Implementing Interface Methods. NET

Agenda • • 3 Declaring Interfaces Implementing Multiple Interfaces Virtual Implementing Interface Methods. NET Build-IN Interfaces

Declaring Interfaces Syntax: interface ISample { void Method 1(); float Method 2(); object Method

Declaring Interfaces Syntax: interface ISample { void Method 1(); float Method 2(); object Method 3(); . . . string Method. N(); int Property 1 { set; get; } event Delegate. Name Event. Name; } 5 'I ' מקובל ששם ממשק מתחיל באות

Declaring Interfaces : לדוגמה interface IPrint { void Print(); byte Num { get; }

Declaring Interfaces : לדוגמה interface IPrint { void Print(); byte Num { get; } } 6

Declaring Interfaces : לדוגמה class App { static void Main(string[] args) { Sample s

Declaring Interfaces : לדוגמה class App { static void Main(string[] args) { Sample s = new Sample(123); s. Print(); IPrint p = new Sample(45); p. Print(); } } 8

Declaring Interfaces : " מדוע ממשקים – "חוזה public interface ICnc. Machine { void

Declaring Interfaces : " מדוע ממשקים – "חוזה public interface ICnc. Machine { void Load. Blue. Print(); void Translate. Blue. Print(); void Start. Process(); } 15

Declaring Interfaces : " חוזה " – ממשקים מדוע Saw. Machine : ICnc. Machine

Declaring Interfaces : " חוזה " – ממשקים מדוע Saw. Machine : ICnc. Machine 16 public class { public void Load. Blue. Print() {. . . } public void Translate. Blue. Print() {. . . } public void Start. Process() {. . . } }

Declaring Interfaces : " חוזה " – ממשקים מדוע Rotter : ICnc. Machine 17

Declaring Interfaces : " חוזה " – ממשקים מדוע Rotter : ICnc. Machine 17 public class { public void Load. Blue. Print() {. . . } public void Translate. Blue. Print() {. . . } public void Start. Process() {. . . } }

Declaring Interfaces : " חוזה " – ממשקים מדוע ICnc. Machine 18 public class

Declaring Interfaces : " חוזה " – ממשקים מדוע ICnc. Machine 18 public class Drill. Machine : { public void Load. Blue. Print() {. . . } public void Translate. Blue. Print() {. . . } public void Start. Process() {. . . } }

Declaring Interfaces : " מדוע ממשקים – "חוזה public class Carpentry. Workshop { private

Declaring Interfaces : " מדוע ממשקים – "חוזה public class Carpentry. Workshop { private ICnc. Machine[] machines = new ICnc. Machine[100]; private int counter; public void Add. Machine(ICnc. Machine new_machine) { machines[counter++] = new_machine; } public void Create. Furniture() { for(int i=0; i<counter; i++) { machines[i]. Load. Blue. Print(); machines[i]. Translate. Blue. Print(); machines[i]. Start. Process(); } } 19 }

Declaring Interfaces Main : " מדוע ממשקים – "חוזה public class App { public

Declaring Interfaces Main : " מדוע ממשקים – "חוזה public class App { public static void Main() { Carpentry. Workshop carpentry = new Carpentry. Workshop(); carpentry. Add. Machine(new Saw. Machine()); carpentry. Add. Machine(new Drill. Machine()); carpentry. Add. Machine(new Rotter()); carpentry. Create. Furniture(); } } 20

Declaring Interfaces : " מדוע ממשקים – "חוזה : בעל המפעל יחליט להוסיף למערך

Declaring Interfaces : " מדוע ממשקים – "חוזה : בעל המפעל יחליט להוסיף למערך הייצור מכונת אריזה חדשה public class Packing. Machine : ICnc. Machine { public void Load. Blue. Print() {. . . } public void Translate. Blue. Print(){. . . } public void Start. Process(){. . . } } 21

Agenda • • 23 Declaring Interfaces Implementing Multiple Interfaces Virtual Implementing Interface Methods. NET

Agenda • • 23 Declaring Interfaces Implementing Multiple Interfaces Virtual Implementing Interface Methods. NET Build-IN Interfaces

Implementing Multiple Interfaces class Sample : IPrint , IMath {. . . public Sample()

Implementing Multiple Interfaces class Sample : IPrint , IMath {. . . public Sample() {. . . } public Sample(byte n 1, byte n 2, byte n 3) {. . . } public void Print() {. . . } public int Sum() { return m_Num 1+m_Num 2+m_Num 3; } public float Avg() { return (float)Sum()/3; } 25} : המשך

Implementing Multiple Interfaces. ממשק בעצמו יכול לרשת ממשק אחד או יותר public interface IBase.

Implementing Multiple Interfaces. ממשק בעצמו יכול לרשת ממשק אחד או יותר public interface IBase. Report { void Create. Report(); void Write. Report(); void Read. Report(); } public interface ICustomers. Report: IBase. Report { void Report. By. Month(); void Report. By. Product(); void Report. By. Pr. Group(); } 27 : לדוגמה

Implementing Multiple Interfaces חייבת לממש את כל מה , מחלקה המממשת ממשק נגזר .

Implementing Multiple Interfaces חייבת לממש את כל מה , מחלקה המממשת ממשק נגזר . שמוגדר הן בממשק הבסיס והן בממשק הנגזר public class Customer : Person, ICustomers. Report { private int Customer. ID; private object Purchases; public void Report. By. Month() {. . . } public void Report. By. Product() {. . . } public void Report. By. Pr. Group() {. . . } public void Create. Report() {. . . } public void Write. Report() {. . . } public void Read. Report() {. . . } } 29 : לדוגמה

Implementing Multiple Interfaces Explicitly Implementing Interface Methods 31 public interface IPrint 1 { void

Implementing Multiple Interfaces Explicitly Implementing Interface Methods 31 public interface IPrint 1 { void Print(); } public interface IPrint 2 { void Print(); } class Sample: IPrint 1, IPrint 2 { public Sample() {. . . } public Sample(int num) {. . . } void IPrint 1. Print() {. . . } void IPrint 2. Print() {. . . } } : לדוגמה

Implementing Multiple Interfaces Explicitly Implementing Interface Methods. Casting - קריאה לאחת מהמתודות מחייבת שימוש

Implementing Multiple Interfaces Explicitly Implementing Interface Methods. Casting - קריאה לאחת מהמתודות מחייבת שימוש ב : לדוגמה Application 32 class { static void Main(string[] args) { Sample s = new Sample(33); ((IPrint 1)s). Print(); ((IPrint 2)s). Print(); IPrint 1 i 1 = new Sample(12); i 1. Print(); IPrint 2 i 2 = new Sample(45); i 1. Print(); } }

Agenda • • 34 Declaring Interfaces Implementing Multiple Interfaces Virtual Implementing Interface Methods. NET

Agenda • • 34 Declaring Interfaces Implementing Multiple Interfaces Virtual Implementing Interface Methods. NET Build-IN Interfaces

Virtual Implementing : לדוגמה public interface IReports { void Html. Report(); void Xml. Report();

Virtual Implementing : לדוגמה public interface IReports { void Html. Report(); void Xml. Report(); } 36

Virtual Implementing public class Base : IReports { public virtual void Html. Report() {

Virtual Implementing public class Base : IReports { public virtual void Html. Report() { Console. Write. Line("Base class : Report"); } public virtual void Xml. Report() { Console. Write. Line("Base class : Report"); } } 37 : המשך Generate Html Generate XML

Virtual Implementing public class Derived : Base, IReports { public override void Html. Report()

Virtual Implementing public class Derived : Base, IReports { public override void Html. Report() { Console. Write. Line("Derived class : Html Report"); } public override void Xml. Report() { Console. Write. Line("Derived class : XML Report"); } } 38 : המשך Generate

Agenda • • 39 Declaring Interfaces Implementing Multiple Interfaces Virtual Implementing Interface Methods. NET

Agenda • • 39 Declaring Interfaces Implementing Multiple Interfaces Virtual Implementing Interface Methods. NET Build-IN Interfaces

. NET Build-IN Interfaces IComparable public class Circle : IComparable {. . . //

. NET Build-IN Interfaces IComparable public class Circle : IComparable {. . . // IComparable implementation public int Compare. To(object obj) { Circle c = null; if(obj is Circle) c = (Circle)obj; else return 1; return this. m_Radius - c. m_Radius; 44 } } : דוגמה

. NET Build-IN Interfaces class App { static void Main(string[] args) { Random rnd

. NET Build-IN Interfaces class App { static void Main(string[] args) { Random rnd = new Random(); Circle[] arr = new Circle[10]; for(int i=0; i<10; i++) arr[i] = new Circle(rnd. Next(200), rnd. Next(50)); Console. Write. Line("Unsorted array. "); foreach (Circle c in arr) Console. Write. Line(c. Calc. Area()); Array. Sort(arr); Console. Write. Line("Sorted Array. "); foreach (Circle c in arr) Console. Write. Line(c. Calc. Area()); } } 45 IComparable : המשך

. NET Build-IN Interfaces IDisposable מחלקה המכילה משאבים לא מנוהלים תממש את הממשק public

. NET Build-IN Interfaces IDisposable מחלקה המכילה משאבים לא מנוהלים תממש את הממשק public class Sample : IDisposable { public Sample(){. . . } ~Sample() {. . . } public void Do. Something() {. . . } public void Dispose() { Console. Write. Line( "Calling the Dispose Method. . . " ); Console. Write. Line( "Erasing Resources. . . " ); GC. Suppress. Finalize(this); } } 47 : לדוגמה

. NET Build-IN Interfaces IDisposable. מתבצעת בצורה ישירה או עקיפה Dispose הפעלת המתודה :

. NET Build-IN Interfaces IDisposable. מתבצעת בצורה ישירה או עקיפה Dispose הפעלת המתודה : צורה ישירה public class Sample. Main { static public void Main() { Sample t = new Sample(); t. Do. Something(); t. Dispose(); } } 48

. NET Build-IN Interfaces IDisposable. מתבצעת בצורה ישירה או עקיפה Dispose הפעלת המתודה public

. NET Build-IN Interfaces IDisposable. מתבצעת בצורה ישירה או עקיפה Dispose הפעלת המתודה public class Sample. Main { static public void Main() { using (Sample t = new Sample() ) { t. Do. Something(); } } } 49 : צורה עקיפה . Dispose ביציאה מהבלוק תופעל המתודה , מגדיר בלוק using משפט

Agenda • • 50 Declaring Interfaces Implementing Multiple Interfaces Virtual Implementing Interface Methods. NET

Agenda • • 50 Declaring Interfaces Implementing Multiple Interfaces Virtual Implementing Interface Methods. NET Build-IN Interfaces

www. corner. co. il – הוצאת ספרים מקוונת Books Corner 51

www. corner. co. il – הוצאת ספרים מקוונת Books Corner 51