Chương 7: Working with Interfaces pptx

34 296 0
Chương 7: Working with Interfaces pptx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Chapter 7. Working with Interfaces Hoang Anh Viet VietHA@it-hut.edu.vn Hanoi University of Technology 1 MicrosoftMicrosoft Objectives “In this chapter you will learn how to define types that support multiple behaviors, how to discover these behaviors at runtime, and how to selectively hide particular behaviors using explicit interface implementation. In addition to examining a number of predefined .NET interface types, you will also learn how to make use of custom interfaces to build an ad hoc event architecture.”  Interfaces Define Types  Defining Interfaces  Implementing Interfaces  Interface Member Matching Rules  Explicit Interface Implementation with Value Types  Versioning Considerations  Contracts  Choosing Between Interfaces and Classes 2 MicrosoftMicrosoft Roadmap  7.1 Interfaces Define Types  7.2 Defining Interfaces  7.3 Implementing Interfaces  7.4 Interface Member Matching Rules  7.5 Explicit Interface Implementation with Value Types  7.6 Versioning Considerations  7.7 Contracts  7.8 Choosing Between Interfaces and Classes 3 MicrosoftMicrosoft 7.1 Interfaces Define Types  An interface declaration defines a reference type  Within variables of this type, you can store a reference to an object that implements the contract of the interface type  Example: declare interface IUIControl 4 public interface IUIControl { void Paint(); } public class Button : IUIControl { public void Paint() { // Paint the Button } } public class ListBox : IUIControl { public void Paint() { // Paint the Listbox } } This interface defines a contract, which states that any type that implements this interface must implement the Paint() method. MicrosoftMicrosoft Roadmap  7.1 Interfaces Define Types  7.2 Defining Interfaces  7.3 Implementing Interfaces  7.4 Interface Member Matching Rules  7.5 Explicit Interface Implementation with Value Types  7.6 Versioning Considerations  7.7 Contracts  7.8 Choosing Between Interfaces and Classes 5 MicrosoftMicrosoft 7.2 Defining Interfaces  What is an Interface?  Defines the signatures for a set of public members that will be available through an object instance  Classes provide implementation details  Classes may implement multiple interfaces 6 MicrosoftMicrosoft Why use Interfaces?  Ensure consistency in similar classes  Classic example: data access  Allow client applications to write generic code against an interface  Implementation can be completely with minimum carnage on the client  Remember: code to the interface – not the implementation! 7 MicrosoftMicrosoft 7.2 Defining Interfaces  What Can Be in an Interface?  Interface = purely abstract class; only signatures, no implementation.  May contain methods, properties, indexers and events (no fields, constants, constructors, destructors, operators, nested types).  Interface members are implicitly public abstract (virtual).  Interface members must not be static. 8 public delegate void DBEvent( IMyDatabase sender ); public interface IMyDatabase : ISerializable, IDisposable { void Insert( object element ); //method int Count { get; } //property object this[ int index ] { get; set; } //indexer event DBEvent dbChanged; //event } MicrosoftMicrosoft 7.2 Defining Interfaces  Interface Inheritance and Member Hiding  Interfaces support multiple inheritance from other interfaces in the syntactic sense 9 public interface IUIControl { void Paint(); } public interface IEditBox : IUIControl { } public interface IDropList : IUIControl { } public class ComboBox : IEditBox, IDropList { public void Paint() { // paint implementation for ComboBox } } ComboBox implements both of those interfaces IEditBox and IDropList, it must implement the union of all the methods declared in the interfaces it directly implements, plus the interfaces those interfaces implement recursively MicrosoftMicrosoft 7.2 Defining Interfaces  Interface Inheritance and Member Hiding  Sometimes, you need to declare a method in an interface that hides a method in an inherited interface. You must use the new keyword if you want to keep the compiler from complaining about it with a warning.  Eg. When the IEditBox interface declares the Paint method using the new keyword, it is said to hide the Paint method declared in IUIControl. 10 [...]... } } 11 Roadmap         7.1 Interfaces Define Types 7.2 Defining Interfaces 7.3 Implementing Interfaces 7.4 Interface Member Matching Rules 7.5 Explicit Interface Implementation with Value Types 7.6 Versioning Considerations 7.7 Contracts 7.8 Choosing Between Interfaces and Classes Microsoft 12 7.3 Implementing Interfaces  There are 2 way for implementing interfaces   Microsoft Implicit implementations... your interface definitions live within a versioned assembly, you may define a newer version of the same interface, even with the same name, in an assembly with the same name but with a new version number The assembly loader will resolve and load the proper assembly at run-time 25 Roadmap         7.1 Interfaces Define Types 7.2 Defining Interfaces 7.3 Implementing Interfaces 7.4 Interface Member... food."); } } Microsoft is 30 Roadmap         7.1 Interfaces Define Types 7.2 Defining Interfaces 7.3 Implementing Interfaces 7.4 Interface Member Matching Rules 7.5 Explicit Interface Implementation with Value Types 7.6 Versioning Considerations 7.7 Contracts 7.8 Choosing Between Interfaces and Classes Microsoft 31 7.8 Choosing Between Interfaces and Classes  Rules     Microsoft If modeling... Types Implementing Interfaces  Value types can implement interfaces: make side effect   Microsoft If you cast a value type to an interface type, you’ll incur a boxing penalty Even worse, if you modify the value via the interface reference, you’re modifying the boxed copy and not the original 19 Roadmap         7.1 Interfaces Define Types 7.2 Defining Interfaces 7.3 Implementing Interfaces 7.4... picks { static void Main() the one with the best match for the type { SomeValue val1 = new SomeValue(1); SomeValue val2 = new SomeValue(2); Console.WriteLine(val1.CompareTo(val2)); } } 23 Roadmap         7.1 Interfaces Define Types 7.2 Defining Interfaces 7.3 Implementing Interfaces 7.4 Interface Member Matching Rules 7.5 Explicit Interface Implementation with Value Types 7.6 Versioning Considerations... then reference this separate assembly If possible, prefer classes over interfaces: This can be helpful for the sake of extensibility 32 Summary     This chapter introduced you to interfaces and how you can model a well-defined, versioned contract using an interface Along with showing you the various ways that classes can implement interfaces, we also described the process that the C# compiler follows... don’t become part of the public interface Explicit implementation provides some flexibility, especially when implementing two interfaces that have methods with the same name in them 13 7.3.1 Implicit Interface Implementation  When a concrete type implements the methods in inherited interfaces, and those methods are marked public, it’s known as implicit interface implementation public interface IUIControl... Classes  Rules     Microsoft If modeling an is-a relationship, use a class: If it makes sense to name your contract with a noun, then you should probably model it with a class If modeling an IMPLEMENTS relationship, use an interface: If it makes sense to name your contract with an adjective, as if it is a quality, then you should probably model it as an interface Consider wrapping up your interface... Explicit Interface Implementation with Value Types 7.6 Versioning Considerations 7.7 Contracts 7.8 Choosing Between Interfaces and Classes Microsoft 26 7.7 Contracts  A programming contract is no different than any other contract   Microsoft You usually define a contract to facilitate communication between two types in your design For example, suppose you have a virtual zoo with some animals An instance... matching up interface methods to implementations in the implementing class We described interfaces from the perspective of reference types and value types—specifically, how expensive boxing operations can cause you pain when using interfaces on value types Finally, we spent some time comparing and contrasting the use of interfaces and classes when modeling contracts between types in your design Microsoft . also learn how to make use of custom interfaces to build an ad hoc event architecture.”  Interfaces Define Types  Defining Interfaces  Implementing Interfaces  Interface Member Matching. Implementation with Value Types  Versioning Considerations  Contracts  Choosing Between Interfaces and Classes 2 MicrosoftMicrosoft Roadmap  7.1 Interfaces Define Types  7.2 Defining Interfaces  7.3. method. MicrosoftMicrosoft Roadmap  7.1 Interfaces Define Types  7.2 Defining Interfaces  7.3 Implementing Interfaces  7.4 Interface Member Matching Rules  7.5 Explicit Interface Implementation with Value Types  7.6

Ngày đăng: 02/08/2014, 09:20

Từ khóa liên quan

Mục lục

  • Chapter 7. Working with Interfaces

  • Objectives

  • Roadmap

  • 7.1 Interfaces Define Types

  • Slide 5

  • 7.2 Defining Interfaces

  • Why use Interfaces?

  • Slide 8

  • Slide 9

  • Slide 10

  • ComboBox.cs

  • Slide 12

  • 7.3 Implementing Interfaces

  • 7.3.1 Implicit Interface Implementation

  • 7.3.2 Explicit Interface Implementation

  • 7.3.3 Overriding Interface Implementations in Derived Classes FancyComboBox.cs

  • 7.3.3 Overriding Interface Implementations in Derived Classes FancyComboBox2.cs

  • 7.3.3 Overriding Interface Implementations in Derived Classes FancyComboBox3.cs

  • 7.3.4 Beware of Side Effects of Value Types Implementing Interfaces

  • Slide 20

Tài liệu cùng người dùng

Tài liệu liên quan