Exemplo n.º 1
0
    void Start()
    {
        Fruit_polymorphism myApple = new Apple();

        _fruits.Add(myApple);
        Fruit_polymorphism myRipeApple = new RipeApple();

        _fruits.Add(myRipeApple);

        // Notice that the Apple version of the methods
        // override the fruit versions. Also notice that
        // since the Apple versions call the Fruit version with
        //t he "base" keyword, both are called.

        // Overriding is also useful in a polymorphic situation.
        // Since the methods of the Fruit class are "virtual" and
        // the methods of the RipeApple class are "override", when we
        // upcast an RipeApple into a Fruit, the RipeApple version of the
        // Methods are used

        foreach (var apple in _fruits)
        {
            apple.Taste();
        }
    }
Exemplo n.º 2
0
    void Start()
    {
        /*
         * Notice here how the variable "myFruit" is of type
         * Fruit but is being assigned a reference to an Apple. This
         * works because of Polymorphism. Since an Apple is a Fruit,
         * this works just fine. While the Apple reference is stored
         * in a Fruit variable, it can only be used like a Fruit
         */
        Fruit_polymorphism myFruit = new Apple();

        myFruit.SayHello();
        myFruit.Chop();

        /*
         * This is called downcasting. The variable "myFruit" which is
         * of type Fruit, actually contains a reference to an Apple. Therefore,
         * it can safely be turned back into an Apple variable. This allows
         * it to be used like an Apple, where before it could only be used
         * like a Fruit.
         */
        Apple myApple = (Apple)myFruit;

        myApple.SayHello();
        myApple.Chop();

        // Notice how each Humanoid variable contains
        // a reference to a different class in the
        // inheritance hierarchy, yet each of them
        // calls SayHello() method.

        // Apple myRipApple  = new RipeApple();
        Fruit_polymorphism myRipApple = new RipeApple();

        myRipApple.SayHello();
    }