-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem4.cs
More file actions
47 lines (36 loc) · 1.09 KB
/
Problem4.cs
File metadata and controls
47 lines (36 loc) · 1.09 KB
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
using System.Diagnostics;
/// <summary>
/// C# technical interview problem #4
/// </summary>
public static class Problem4
{
public const string WhatTheCowSays = "Moo! Moo! Moo!";
/// <summary>
/// The cow keeps saying "Bessie makes a noise." but that's not what we want. What we wanted was for it to say
/// "Moo! Moo! Moo!". Fix the code to get the right behavior and explain why we didn't get what
/// we expected at first. Do not remove the cast to LivingThing.
/// </summary>
public static void TestLivingThingSpeak()
{
var cow = new Cow() { Name = "Bessie" };
var livingThing = cow as LivingThing;
var sound = livingThing.Speak();
Debug.Assert(sound == WhatTheCowSays, $"Expected '{WhatTheCowSays}' but got '{sound}'");
Console.WriteLine(sound);
}
}
public class Cow : LivingThing
{
public new string Speak()
{
return Problem4.WhatTheCowSays;
}
}
public class LivingThing
{
public string Name { get; set; } = "";
public string Speak()
{
return $"{Name} makes a noise.";
}
}