Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>EDIT:</strong></p> <p>I have taken the body of code for each class out - If you want to see my answer, have a look at the edit revisions :)</p> <p>First off we define the <a href="http://msdn.microsoft.com/en-us/library/ms173156.aspx" rel="noreferrer">interface</a></p> <pre><code>public interface IAnimal { string WhatAmI { get; } string WhatIsMyName { get; } } </code></pre> <p>Any class that implements this interface must implement these properties. An interface is like a contract; a class implementing an interface agrees to provide an implementation of the interface's methods, properties events or indexers.</p> <p>Next, we need to define your abstract Animal class</p> <pre><code>public abstract class Animal : IAnimal { //Removed for Training, See Edit for the code } </code></pre> <p>The fact that the class is <a href="http://msdn.microsoft.com/en-us/library/sf985hc5(VS.71).aspx" rel="noreferrer">abstract</a> indicates that the class is intended only to be a base class for other classes. We have implemented both properties of the interface and also have a private field to store the animal name. In addition, we have made the <code>WhatAmI</code> property accessor abstract so that we can implement our own specific property accessor logic in each derived class and have also defined a constructor that accepts a string argument and assigns the value to the <code>_name</code> private field.</p> <p>Now, let's define our <code>Cat</code> and <code>Dog</code> classes</p> <pre><code>public class Dog : Animal { //Removed for Training, See Edit for the code } public class Cat : Animal { //Removed for Training, See Edit for the code } </code></pre> <p>Both classes inherit from <code>Animal</code> and each has a constructor that defines a string argument and passes that argument as a parameter to the base constructor. In addition, each class implements it's own property accessor for <code>WhatAmI</code>, returning a string of their type, respectively.</p> <p>For the rest of the code</p> <pre><code>public class Program { public static void DescribeAnimal(IAnimal animal) { Console.WriteLine("My name is {0}, I am a {1}", animal.WhatIsMyName, animal.WhatAmI); } static void Main(string[] args) { Dog mydog = new Dog("Spot"); Cat mycat = new Cat("Felix"); DescribeAnimal(mydog); DescribeAnimal(mycat); Console.ReadKey(); } } </code></pre> <p>the static method <code>DescribeAnimal</code> accepts an <code>IAnimal</code> as an argument and writes out the values returned by the <code>WhatIsMyName</code> and <code>WhatAmI</code> property accessors for the passed in <code>IAnimal</code>. </p> <p>Since <code>Animal</code> implements <code>IAnimal</code> and both <code>Dog</code> and <code>Cat</code> inherit from <code>Animal</code>, any <code>Cat</code> or <code>Dog</code> object can be passed as a parameter to the <code>DescribeAnimal</code> method.</p> <p>I hope that I have explained this clearly, If anyone feels my choice of words needs tightening up, please comment and I will be happy to edit my answer.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload