Exemplo n.º 1
0
        public static void LiveADay(object obj)
        {
            // declare Person class reference variable
            Person person = (Person)obj;

            // declare IPerson interface reference variable
            IPerson iPerson = (IPerson)obj;

            // declare IStudent interface reference variable
            // initialize to null because we do not know if obj is Student or Teacher
            IStudent iStudent = null;

            // notice how we can use the IPerson interface to call Eat() for both Student and Teacher
            // because that method is defined in the shared interface
            iPerson.Eat();

            // notice how we can use a Person reference to call Work()
            // because the virtual method is defined in the shared Person class
            // even though the method implementation is different between the 2 classes
            person.Work();

            // but because Party() is only a member of Student
            // as a result of inheriting IStudent
            // we need to ensure obj is a Student
            if (obj.GetType() == typeof(Student))
            {
                // we use an IStudent reference to call Party()
                iStudent = (IStudent)person;
                iStudent.Party();
            }
        }