1 / 11

Wydział Elektroniki Kierunek: AiR

Wydział Elektroniki Kierunek: AiR. Zaawansowane metody programowania Wykład 6. Interfejsy. Interfaces http://msdn.microsoft.com. Attributes. Much of the C# language enables the programmer to specify declarative information about the entities defined in the program .

lilka
Download Presentation

Wydział Elektroniki Kierunek: AiR

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Wydział ElektronikiKierunek: AiR Zaawansowane metody programowania Wykład 6

  2. Interfejsy Interfaces http://msdn.microsoft.com

  3. Attributes • Much of the C# language enables the programmer to specify declarative information about the entities defined in the program. • C# enables programmers to invent new kinds of declarative information, called attributes. • Programmers can then attach attributes to various program entities, and retrieve attribute information in a run-time environment. • Attributes are defined through the declaration of attribute classes, which may have • positionaland • named parameters. • Attributes are attached to entities in a C# program • using attribute specifications, and • can be retrieved at run-time as attribute instances.

  4. Attributes, cont. • Attributeclasses • A class that derives from the abstract class System.Attribute, whether directly or indirectly, is an attribute class. The declaration of an attribute class defines a new kind of attribute that can be placed on a declaration. • By convention, attribute classes are named with a suffix of Attribute. Uses of an attribute may either include or omit this suffix. • Attributespecification • Attribute specification is the application of a previously defined attribute to a declaration. • An attribute is a piece of additional declarative information that is specified for a declaration. • Attributeinstances • An attribute instance is an instance that represents an attribute at run-time. • An attribute is defined with an attribute class, positional arguments, and named arguments. • An attribute instance is an instance of the attribute class that is initialized with the positional and named arguments.

  5. Interfejs – podstawowe własności • An interface defines a contract. A class or struct that implements an interface must adhere (dotrzymywać)to its contract. • The declaration takes the following form: [attributes] [modifiers] interface identifier [:base-list] {interface-body}[;]

  6. Interfejs – podstawowe własności, cd [attributes] [modifiers] interface identifier [:base-list] {interface-body}[;] • attributes(Optional) Additional declarative information • modifiers(Optional) The allowed modifiers are new and the four access modifiers • identifierThe interface name • base-list(Optional) A list that contains one or more explicit base interfaces separated by commas • interface-bodyDeclarations of the interface members

  7. Interfejs – podstawowe własności, cd2 • An interface can be a member of a namespace or a class and can contain signatures of the following members: • Methods • Properties • Indexers • Events • An interface can inherit from one or more base interfaces. • Example: the interface IMyInterface inherits from two base interfaces, IBase1 and IBase2:interface IMyInterface: IBase1, IBase2 { void MethodA(); void MethodB(); }

  8. Interfejs – podstawowe własności, cd3 • Interfaces can be implemented by classes and structs. The identifier of the implemented interface appears in the class base list. For example:class Class1: Iface1, Iface2 { // class members } • When a class base list contains a base class and interfaces, the base class comes first in the list. For example:class ClassA: BaseClass, Iface1, Iface2 {// class members }

  9. C# Delegacje Delegate(s) • Delegata to obiekt, który przechowuje referencje do metody. • Podczas wywoływania tego obiektu delegatywywoływana jest metoda, której referencja była przechowywana. • Aby móc przypisać metodę do delegaty musi ona spełniać pewien warunek, tym warunkiem jest zgodność deklaracji metody i delegaty. • Ogólna deklaracja delegaty: delegatetyp_zwracanynazwa_delegaty(parametry) ; • Delegatedeklaruje się zawsze po deklaracjach przestrzeni nazw, nie jest ona składnikiem klas. • Bardzo prosty przykład, w którym to tworzony jest obiekt takiej delegaty:

  10. class Pokaz{ public staticvoidMain() {Klasa_aA = newKlasa_a(4); delegata d1 = new delegata(A.metoda); delegata d2 = new delegata(Klasa_b.Liczba); d1(3); d2(7); }} C# Delegacje -Przykład using System; delegatevoid delegata(int liczba); classKlasa_a{ public int a; public Klasa_a(inta){ this.a= a;} public void metoda(int cyferka) { Console.WriteLine(cyferka * a);}} classKlasa_b{ public staticvoid Liczba(int c){Console.WriteLine(c);}}

  11. C# Delegacje –Przykład 1 - komentarz • Referencje do metody przesyła się jakby to był parametr konstruktora delegaty:delegata d1 = new delegata(A.metoda); • Parametr dla metody przesyła się dopiero podczas wywołania delegaty: d1(3); • Sednem wykorzystania delegat są łańcuchy wywołań, dzięki którym można wywołać kilka delegat pod rząd. Przykład! • Aby utworzyć łańcuch wywołań należy najpierw dla każdej metody utworzyć delegate, następnie należy utworzyć jedną delegate do której to przypisuje się referencję pierwszego wywołania potem za pomocą złożonych operatorów przypisania dodaje się kolejne. • Aby odjąć jakąś metodę stosuje się minus czyli -= (złożony operator przypisania).

More Related