repl.it
@kironroy/

interfaces2

C#

better explanation

fork
loading
Files
  • main.cs
main.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System;
using System.Collections.Generic;

namespace InterfaceDemo
{
    // Interface is a contract, this is what a class will do
    // Classes implement interfaces

    //  ** Interfaces area contract  Use them as types when you need common functionality 

    class Program
    {
        static void Main(string[] args)
        {
            // if we have differnt things that are not quite the same and can't inherient from each other
            // but still want to store them in a list together, we can use an interface

            // another reason we use interfaces if we want to create a contract that others use

            // ** List of similar objects below **

            List<IComputerController> controllers = new List<IComputerController>(); 

            Keyboard keyboard = new Keyboard(); // initiating a keyboard object with the Class Keyboard
            GameController gameController = new GameController(); // initiating game controller with Class GameController
            BatteryPoweredGameController battery = new BatteryPoweredGameController();
            BatteryPoweredKeyboard batteryKeyboard = new BatteryPoweredKeyboard();

            controllers.Add(keyboard);
            controllers.Add(gameController);
            // this will work because batterypowered inherits from GameController
            // and GameController uses the IComputerController interface

            controllers.Add(battery);

            foreach (IComputerController controller in controllers)
            {
                // controller. (only uses what the IComputerController interface has (this is the contract) 
            }

            List<IBatteryPowered> powereds = new List<IBatteryPowered>();

            powereds.Add(battery);
            powereds.Add(batteryKeyboard);

            using (GameController gc = new GameController())
            {

            }

                Console.ReadLine();
        }
    }

    // interface is just a contract, doesnt do any work, unlike a base class
    // just write void no need to write public void
    public interface IComputerController : IDisposable
    {
        // these methods are different that the methods in Keyboard and  Gamecontroller,
        // this is why we make the interface

        void Connect();
        void CurrentKeyPressed();
    }

    public class Keyboard : IComputerController
    {
        public void Connect()
        {

        }

        public void CurrentKeyPressed()
        {

        }

        public void Dispose()
        {

        }

        public string ConnectionType { get; set; }
    }

    public interface IBatteryPowered
    {
        int BatteryLevel { get; set; }
    }

    // implement base class first than interfaces
    public class BatteryPoweredKeyboard : Keyboard, IBatteryPowered
    {
        public int BatteryLevel { get; set; }
    }

    public class GameController : IComputerController, IDisposable
    {
        public void Connect()
        {

        }

        public void CurrentKeyPressed()
        {

        }

        public void Dispose()
        {
            // do whatever shutdown tasks needed 
        }

        
    }

    // implement base class first than interfaces
    public class BatteryPoweredGameController : GameController, IBatteryPowered
    {
        public int BatteryLevel { get; set; }

    }
}