예제 #1
0
    static void Main()
    {
        MyDerivedClass derived = new MyDerivedClass();
        MyBaseClass    mybc    = (MyBaseClass)derived;

        derived.Print();
        mybc.Print();
    }
예제 #2
0
    static void Main()                                // Main
    {
        SecondDerived derived = new SecondDerived();  // Use SecondDerived.
        MyBaseClass   mybc    = (MyBaseClass)derived; // Use MyBaseClass.

        derived.Print();
        mybc.Print();
    }
예제 #3
0
    static void Main()
    {
        MyDerivedClass derived = new MyDerivedClass();
        MyBaseClass    mybc    = (MyBaseClass)derived;

        derived.Print();                  // Call Print from derived portion.
        mybc.Print();                     // Call Print from base portion.
    }
예제 #4
0
    static void Main()
    {
        MyDerivedClass derived = new MyDerivedClass();
        MyBaseClass    mybc    = (MyBaseClass)derived;

        derived.Print(); // Call Print from derived portion.
        mybc.Print();    // Call Print from base portion.
                         // mybc.var1 = 5; // Error: base class reference cannot
                         // access derived class members.
    }
예제 #5
0
        public void _01()
        {
            var derived = new MyDerivedClass();

            derived.Print();

            // 父类变量指向子类对象, 通过父类变量只能访问父类成员
            MyBaseClass mybc = derived;

            mybc.Print();
        }
예제 #6
0
        public void _01()
        {
            var derived = new MyDerivedClass();

            derived.Print();

            // 父类变量指向子类对象, 通过父类变量只能访问父类成员,
            // 但是如果这个成员在父类是标记为 virtual, 在子类是标记为 override, 则此时访问的是子类成员.
            MyBaseClass mybc = derived;

            mybc.Print();
        }