Exemplo n.º 1
0
    public static void Main()
    {
        FlyingSquirrel rocky = new FlyingSquirrel();
        Eagle          sam   = new Eagle();

        List <IWalking> birds = new List <IWalking>();

        //type squirrel , class squirrel
        birds.Add(sam);
        //type eagle, class eagle
        birds.Add(rocky);

        // the foreach will loop through rocky-type FlyingSquirrel , sam-type Eagle
        foreach (var thing in birds)
        {
            //each thing has a type or class, will refer to the method fly in that specific class
            // fly method consoles a message
            // thing.Fly();
            thing.Walk();
        }
        sam.Fly();
        sam.Walk();
        sam.AirSpeed = 5;
        Console.WriteLine($"sam's airspeed {sam.AirSpeed}");
    }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            // Naming conventions in c#:
            //  PascalCase (TitleCase)
            //   for classes, methods, properties, namespaces
            //  CamelCase for local variables
            Dog dog = new Dog();

            dog.Bark();

            // Using fields and properties
            // Using getters and setters with private field.
            dog.SetWeight(6);
            System.Console.WriteLine(dog.GetWeight());

            dog.Name = "Fido";
            System.Console.WriteLine(dog.Name);

            dog.Breed = "Golden Retriever";

            dog.GoTo("The park");

            Console.WriteLine("Hello World!");

            // ********

            IAnimal animal = new Dog();

            animal = new Eagle();
            // This is okay since both classes are an IAnimal type
            //  But you are not allowed to do dog/eagle specific
            //  things via this variable

            //animal.Fly(); // Will not work.
            // You can cast objects to specific types
            // Will fail at run-time if object is not the
            //  correctly casted type.
            Eagle e = (Eagle)animal;

            e.Fly();

            // Superclass = base class = parent class
            // subclass = derived class = child class


            DisplayName(new Dog());
            DisplayName(new Eagle());
        }