Project 4. CS OOP

 
using System;

class APet{
    public virtual string Say (){
        return "I am a pet, not an animal.";
    }
}

class ACat : APet{
    public override string Say (){
        return "Meow";
    }
}

class ADog : APet{
    public override string Say(){
        return "Woof";
    }
}

class ARooster: APet{
    public override string Say (){
        return "Cock-a-doodle-doo";
    }
}

class ADonkey: APet{
    public override string Say(){
        return "Hee-haw";
    }
}

class BremerStadtMus {
    static string[] result = new string[4];

    static void Model() {
        APet Acat= new ACat();
        APet Adog= new ADog();
        APet[] aps= new APet [4]; 
        aps[0] =Acat; 
        aps[1]=Adog;
        aps[2] =new ARooster(); 
        aps[3] =new ADonkey();
        int j=0;
        for (int i = 0; i < aps.Length; i++){
            j= (i < aps.Length - 1) ? i + 1 : 0;
            result[i] = aps[i].GetType().Name + " says "; 
            result[i] += aps[j].Say();
        }
    }

    static void View() {
        foreach (string s in result){
            Console.WriteLine(s); 
        }
    }

    public static void ModelView() {
        Console.WriteLine("ModelView");
        Model();
        View();
    }
}

public class HelloWorld
{
    public static void Main(string[] args)
    {
        BremerStadtMus.ModelView();
    }
}

Projects 4.1 Back