Presentation is loading. Please wait.

Presentation is loading. Please wait.

Yunus Özen yunus@csharpturk.net 12- Interfaces, Structures, and Enumerations Nesne Yönelimli Programlama - i Yunus Özen yunus@csharpturk.net.

Similar presentations


Presentation on theme: "Yunus Özen yunus@csharpturk.net 12- Interfaces, Structures, and Enumerations Nesne Yönelimli Programlama - i Yunus Özen yunus@csharpturk.net."— Presentation transcript:

1 Yunus Özen yunus@csharpturk.net
12- Interfaces, Structures, and Enumerations Nesne Yönelimli Programlama - i Yunus Özen

2 Interface

3 Interface Description
An interface defines a set of methods that will be implemented by a class. An interface does not, itself, implement any method. An interface is a purely logical construct that describes functionality without specifying implementation.

4 Interface Interfaces are syntactically similar to abstract classes. However, in an interface, no method can include a body. That is, an interface provides no implementation whatsoever. It specifies what must be done, but not how. Once an interface is defined, any number of classes can implement it. A class can implement any number of interfaces.

5 Interface In an interface, methods are implicitly public, and no explicit access specifier is allowed. In an interface, no method can have an implementation. Although the prefix I is not necessary, many programmers prefix interfaces with I to differentiate them from classes. Interfaces can specify properties, indexers, and events.

6 Interface Interfaces cannot have data members.
They cannot define constructors, destructors, or operator methods. In an interface, no member can be declared as static.

7 Example public interface ISeries { int GetNext(); // return next number in series void Reset(); // restart void SetStart(int x); // set starting value }

8 Implementing Interfaces
class class-name : interface-name { // class-body }

9 Implementing Interfaces
A class can implement more than one interface. When a class implements more than one interface, specify each interface in a comma-separated list. A class can inherit a base class and also implement one or more interfaces. In this case, the name of the base class must come first in the comma-separated list.

10 Implementing Interfaces
Methods are implicitly public within an interface, so their implementation must also be public. The return type and signature of the implementing method must match exactly the return type and signature specified in the interface definition.

11 Example // Implement ISeries. class ByTwos : ISeries { int start; int val; public ByTwos() { start = 0; val = 0; } public int GetNext() { val += 2; return val; public void Reset() { val = start; public void SetStart(int x) { start = x;

12 Example // Implement ISeries and add GetPrevious(). class ByTwos : ISeries { int start; int val; int prev; public ByTwos() { start = 0; val = 0; prev = -2; } public int GetNext() { prev = val; val += 2; return val; public void Reset() { val = start; prev = start - 2; public void SetStart(int x) { start = x; prev = val - 2; // A method not specified by ISeries. public int GetPrevious() { return prev;

13 Example // Use ISeries to implement a series of prime numbers. class Primes : ISeries { int start; int val; public Primes() { start = 2; val = 2; } public int GetNext() { int i, j; bool isprime; val++; for(i = val; i < ; i++) { isprime = true; for(j = 2; j <= i/j; j++) { if((i%j)==0) { isprime = false; break; if(isprime) { val = i; return val; public void Reset() { val = start; public void SetStart(int x) { start = x;

14 Using Interface References
Interface reference variable can refer to any object that implements its interface. When you call a method on an object through an interface reference, it is the version of the method implemented by the object that is executed. This process is similar to using a base class reference to access a derived class object. An interface reference variable has knowledge only of the methods declared by its interface declaration.

15 Example class SeriesDemo2 { static void Main() { ByTwos twoOb = new ByTwos(); Primes primeOb = new Primes(); ISeries ob; for(int i=0; i < 5; i++) { ob = twoOb; Console.WriteLine("Next ByTwos value is " + ob.GetNext()); ob = primeOb; Console.WriteLine("Next prime number is " + }

16 Interface Properties Like methods, properties are specified in an interface without any body. Only get or set will be present for read-only or write-only properties, respectively. No access modifiers are allowed on the accessors when a property is declared in an interface. Thus, the set accessor, for example, cannot be specified as private in an interface. Type-Name { get; set; }

17 Example public interface ISeries { // An interface property. int Next { get; // return the next number in series set; // set next number } // Implement ISeries. class ByTwos : ISeries { int val; public ByTwos() { val = 0; // Get or set value. public int Next { get { val += 2; return val; set { val = value;

18 Interface Indexers An interface can specify an indexer.
No access modifiers are allowed on the accessors when an indexer is declared in an interface.

19 Example public interface ISeries { // An interface property. int Next { get; // return the next number in series set; // set next number } // An interface indexer. int this[int index] { get; // return the specified number in series // Implement ISeries. class ByTwos : ISeries { int val; public ByTwos() { val = 0; // Get or set value using a property. public int Next { get { val += 2; return val; set { val = value; // Get a value using an index. public int this[int index] { for(int i=0; i < index; i++)

20 Interfaces can be Inherited
One interface can inherit another. The syntax is the same as for inheriting classes. When a class implements an interface that inherits another interface, it must provide implementations for all the members defined within the interface inheritance chain.

21 Explicit Implementations
// Explicitly implement an interface member. using System; interface IEven { bool IsOdd(int x); bool IsEven(int x); } class MyClass : IEven { // Explicit implementation. Notice that this member is private // by default. bool IEven.IsOdd(int x) { if((x%2) != 0) return true; else return false; // Normal implementation. public bool IsEven(int x) { IEven o = this; // Interface reference to the invoking object. return !o.IsOdd(x); class Demo { static void Main() { MyClass ob = new MyClass(); bool result; result = ob.IsEven(4); if(result) Console.WriteLine("4 is even."); // result = ob.IsOdd(4); // Error, IsOdd not exposed. // But, this is OK. It creates an IEven reference to a MyClass object // and then calls IsOdd() through that reference. IEven iRef = (IEven) ob; result = iRef.IsOdd(3); if(result) Console.WriteLine("3 is odd.");

22 Choosing Between Interface and Abstract Class
When you can fully describe the concept in terms of “what it does” without needing to specify any “how it does it,” then you should use an interface. If you need to include some implementation details, then you will need to represent your concept in an abstract class.

23 Structure Definition As you know, classes are reference types. Accessing class objects through a reference adds overhead onto every access. It also consumes space. For very small objects, this extra space might be significant. To address these concerns, C# offers the structure. A structure is similar to a class, but is a value type, rather than a reference type.

24 Structures All structures do, however, implicitly inherit System.ValueType, which inherits object. However, a structure can implement one or more interfaces. Like classes, structure members include methods, fields, indexers, properties, operator methods, and events. Structures can also define constructors, but not destructors. However, you cannot define a default (parameterless) constructor for a structure. The reason for this is that a default constructor is automatically defined for all structures, and this default constructor can’t be changed. Since structures do not support inheritance, structure members cannot be specified as abstract, virtual, or protected.

25 Structures A structure object can be created using new in the same way as a class object, but it is not required. When new is used, the specified constructor is called. When new is not used, the object is still created, but it is not initialized. Thus, you will need to perform any initialization manually.

26 Example // Define a structure. struct Book { public string Author; public string Title; public int Copyright; public Book(string a, string t, int c) { Author = a; Title = t; Copyright = c; }

27 Structure

28 Structures When you assign one structure to another, a copy of the object is made. This is an important way in which struct differs from class. When you assign one class reference to another, you are simply making the reference on the left side of the assignment refer to the same object as that referred to by the reference on the right. When you assign one struct variable to another, you are making a copy of the object on the right.

29 Example // Copy a struct. using System; // Define a structure. struct MyStruct { public int x; } // Demonstrate structure assignment. class StructAssignment { static void Main() { MyStruct a; MyStruct b; a.x = 10; b.x = 20; Console.WriteLine("a.x {0}, b.x {1}", a.x, b.x); a = b; b.x = 30;

30 Example // Use a class. using System; // Now a class. class MyClass { public int x; } // Now show a class object assignment. class ClassAssignment { static void Main() { MyClass a = new MyClass(); MyClass b = new MyClass(); a.x = 10; b.x = 20; Console.WriteLine("a.x {0}, b.x {1}", a.x, b.x); a = b; b.x = 30;

31 Why Structures? Thus, a struct does not require a separate reference variable. This means that less memory is used in some cases. Because a struct is accessed directly, it does not suffer from the performance loss that is inherent in accessing a class object. If you need to simply store a group of related data, but don’t need inheritance and don’t need to operate on that data through a reference, then a struct can be a more efficient choice.

32 Enumeration

33 Enumerations An enumeration is a set of named integer constants.
The enumeration list is a comma-separated list of identifiers. enum Meyve { Elma, Armut, Portakal, Muz}; A key point to understand about an enumeration is that each of the symbols stands for an integer value.

34 Example Console.WriteLine(Apple.RedDel + " has the value " + (int)Apple.RedDel); displays RedDel has the value 2

35 Example // Simulate a conveyor belt. using System; class ConveyorControl { // Enumerate the conveyor commands. public enum Action { Start, Stop, Forward, Reverse }; public void Conveyor(Action com) { switch(com) { case Action.Start: Console.WriteLine("Starting conveyor."); break; case Action.Stop: Console.WriteLine("Stopping conveyor."); case Action.Forward: Console.WriteLine("Moving forward."); case Action.Reverse: Console.WriteLine("Moving backward."); } class ConveyorDemo { static void Main() { ConveyorControl c = new ConveyorControl(); c.Conveyor(ConveyorControl.Action.Start); c.Conveyor(ConveyorControl.Action.Forward); c.Conveyor(ConveyorControl.Action.Reverse); c.Conveyor(ConveyorControl.Action.Stop);

36 Teşekkürler


Download ppt "Yunus Özen yunus@csharpturk.net 12- Interfaces, Structures, and Enumerations Nesne Yönelimli Programlama - i Yunus Özen yunus@csharpturk.net."

Similar presentations


Ads by Google