Scroll to see replies
public abstract class Robot<T> where TpeechEngine
{
protected void Initialise(T engine){
engine.Initialise();
Console.Out.WriteLine("Initialising robot");
}
}
public interface SpeechEngine {
void Initialise();
}
public class AlexaVoicepeechEngine
{
public void Initialise()
{
Console.Out.WriteLine("Initialising Alexa speech engine");
}
}
public class SiriVoice : SpeechEngine
{
public void Initialise()
{
Console.Out.WriteLine("Initialising Siri speech engine");
}
}
public class MrRobot : Robot<SpeechEngine>
{
SpeechEngine voice;
public MrRobot(SpeechEngine voiceArg){
voice = voiceArg;
}
public void Greet()
{
Initialise(voice);
Console.Out.WriteLine("Hello world");
}
}
class Program
{
static void Main(string[] args)
{
MrRobot alexa = new MrRobot(new AlexaVoice());
alexa.Greet();
MrRobot siri = new MrRobot(new SiriVoice());
siri.Greet();
Console.ReadKey();
}
}
// Using a C# feature called 'generics' because you see the use of the type 'T' between the less than and greater than signs. The 'where' expression: this forces that a certain generic paramater T has to conform to something, in this case you can only pass in SpeechEngine types (where T is a type). This promotes less errors because you can't pass in the wrong type or call a wrong method.We want to make this class abstract so that you can't instantiate a Robot. We want the Robot class to contain the commonalities shared by its children classes.
public abstract class Robot<T> where TpeechEngine
{
protected void Initialise(T engine){ // A generic and flexible Initialise() method. It's protected so that only child classes can call it.
engine.Initialise(); // We call the Initialise() method of whatever SpeechEngine variety (type) was passed in. We see below that this prints out a SpeechEngine-specific text.
Console.Out.WriteLine("Initialising robot"; // Whenever we call the Initialise(engine) method, "Initialising robot" will be printed to the console output.
}
}
// Whatever uses the SpeechEngine interface, must have an Initialise() method. Again, this way you are promoting less errors (for example, forgetting to call that method, or giving it a different, non-standard name).
public interface SpeechEngine {
void Initialise();
}
public class AlexaVoicepeechEngine // An Alexa Voice is a Speech Engine
{
public void Initialise() // Mandatory implementation, therefore conformity and standardization. We are using the method that we got from the interface we inherited.
{
Console.Out.WriteLine("Initialising Alexa speech engine";
}
}
public class SiriVoice : SpeechEngine // The same goes for the Siri Voice
{
public void Initialise()
{
Console.Out.WriteLine("Initialising Siri speech engine";
}
}
// MrRobot is a Robot that has a SpeechEngine. Seeing MrRobot extends the Robot class (inherits it, and therefore inherits its members) As you can see, MrRobot is the class we want to actually instantiate because it has no 'abstract' keyword restricting it from instantiation
public class MrRobot : Robot<SpeechEngine>
{
// interface for polymorphism: which voice do we want? Conceptually, we are saying that a MrRobot robot has a speech engine that belongs to it (composition, like an engine belongs to a car).
SpeechEngine voice;
public MrRobot(SpeechEngine voiceArg){
// When we create our MrRobot, we also give it a speech engine
voice = voiceArg; // Our robot will have the passed-in SpeechEngine variety so that it will greet us in that voice.
voice = voiceArg;
}
public void Greet()
{
// We call our SpeechEngine's Initialise(voice) method that we got through inheritance from our Robot class, who has access to the SpeechEngine's Initialise(T engine) method
Initialise(voice);
Console.Out.WriteLine("Hello world";
}
}
class Program // Our main program that will execute our logic that we put together
{
static void Main(string[] args) // The main entry point that will cause the compiler to locate it and execute our program
{
MrRobot alexa = new MrRobot(new AlexaVoice()); // We create a MrRobot that gets passed in a new AlexaVoice anonymous instance.
alexa.Greet(); // We ask alexa, a MrRobot, to greet us with its AlexaVoice SpeechEngine
MrRobot siri = new MrRobot(new SiriVoice()); // We do the same for the SiriVoice
siri.Greet();
Console.ReadKey(); // We read the next key that the user types into the console. This is a way to artificially pause a running program because the program waits until the user presses a key.
}
}
I wrote my comments to the code in my quote above.// Using a C# feature called 'generics' because you see the use of the type 'T' between the less than and greater than signs. The 'where' expression: this forces that a certain generic paramater T has to conform to something, in this case you can only pass in SpeechEngine types (where T is a type). This promotes less errors because you can't pass in the wrong type or call a wrong method.We want to make this class abstract so that you can't instantiate a Robot. We want the Robot class to contain the commonalities shared by its children classes.
public abstract class Robot<T> where TpeechEngine
{
protected void Initialise(T engine){ // A generic and flexible Initialise() method. It's protected so that only child classes can call it.
engine.Initialise(); // We call the Initialise() method of whatever SpeechEngine variety (type) was passed in. We see below that this prints out a SpeechEngine-specific text.
Console.Out.WriteLine("Initialising robot"; // Whenever we call the Initialise(engine) method, "Initialising robot" will be printed to the console output.
}
}
// Whatever uses the SpeechEngine interface, must have an Initialise() method. Again, this way you are promoting less errors (for example, forgetting to call that method, or giving it a different, non-standard name).
public interface SpeechEngine {
void Initialise();
}
public class AlexaVoicepeechEngine // An Alexa Voice is a Speech Engine
{
public void Initialise() // Mandatory implementation, therefore conformity and standardization. We are using the method that we got from the interface we inherited.
{
Console.Out.WriteLine("Initialising Alexa speech engine";
}
}
public class SiriVoice : SpeechEngine // The same goes for the Siri Voice
{
public void Initialise()
{
Console.Out.WriteLine("Initialising Siri speech engine";
}
}
// MrRobot is a Robot that has a SpeechEngine. Seeing MrRobot extends the Robot class (inherits it, and therefore inherits its members) As you can see, MrRobot is the class we want to actually instantiate because it has no 'abstract' keyword restricting it from instantiation
public class MrRobot : Robot<SpeechEngine>
{
// interface for polymorphism: which voice do we want? Conceptually, we are saying that a MrRobot robot has a speech engine that belongs to it (composition, like an engine belongs to a car).
SpeechEngine voice;
public MrRobot(SpeechEngine voiceArg){
// When we create our MrRobot, we also give it a speech engine
voice = voiceArg; // Our robot will have the passed-in SpeechEngine variety so that it will greet us in that voice.
voice = voiceArg;
}
public void Greet()
{
// We call our SpeechEngine's Initialise(voice) method that we got through inheritance from our Robot class, who has access to the SpeechEngine's Initialise(T engine) method
Initialise(voice);
Console.Out.WriteLine("Hello world";
}
}
class Program // Our main program that will execute our logic that we put together
{
static void Main(string[] args) // The main entry point that will cause the compiler to locate it and execute our program
{
MrRobot alexa = new MrRobot(new AlexaVoice()); // We create a MrRobot that gets passed in a new AlexaVoice anonymous instance.
alexa.Greet(); // We ask alexa, a MrRobot, to greet us with its AlexaVoice SpeechEngine
MrRobot siri = new MrRobot(new SiriVoice()); // We do the same for the SiriVoice
siri.Greet();
Console.ReadKey(); // We read the next key that the user types into the console. This is a way to artificially pause a running program because the program waits until the user presses a key.
}
}
I wrote my comments to the code in my quote above.1.
Time
2.
Finding a space to experiment with HTML and CSS
Last reply 1 month ago
Free website hosting providers with no strings attached?Posted 3 months ago
iDEA silver citizen badge three. Milkshake shack coding problem.Last reply 4 months ago
How do you practice programming java for beginners?Last reply 8 months ago
hows the employment market for tech work in the UK?Last reply 1 month ago
Free website hosting providers with no strings attached?Posted 3 months ago
iDEA silver citizen badge three. Milkshake shack coding problem.Last reply 4 months ago
How do you practice programming java for beginners?Last reply 8 months ago
hows the employment market for tech work in the UK?