程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> (C#)一道看似簡單卻很難答得完全正確的關於多態的問題

(C#)一道看似簡單卻很難答得完全正確的關於多態的問題

編輯:C#入門知識

[csharp] 
/*關於virtual,overried,new在方法和屬性上以this,base形式輸出的問題
 *問主程序的輸出是什麼
 */ 
 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
 
namespace ConsoleApplication2 

    class Program 
    { 
        static void Main(string[] args) 
        { 
            A a1 = new B(); 
            a1.AM1(); 
            B b1 = (B)a1; 
            b1.AM1(); 
 
            Console.WriteLine(); 
 
            A a2 = new B(); 
            a2.AM2(); 
            B b2 = (B)a2; 
            b2.AM2(); 
        } 
    } 
 
    class A 
    { 
        public virtual int i { get; protected set; } 
        public A() 
        { 
            this.i = 9; 
        } 
 
        public virtual void AM1() 
        { 
            this.i = this.i + 10000; 
            Console.WriteLine("A.this.i="+ this.i); 
        } 
 
        public virtual void AM2() 
        { 
            this.i = this.i + 10000; 
            Console.WriteLine("A.this.i=" + this.i); 
        } 
    } 
    class B : A 
    { 
        public override int i { get; protected set; } 
        public B() 
            : base() 
        { 
            this.i = 5; 
        } 
 
        public override void AM1() 
        { 
            Console.WriteLine("B.this.i=" + this.i); 
            Console.WriteLine("B.base.i=" + base.i); 
            this.i = this.i + 100; 
            base.i = base.i + 1000; 
            Console.WriteLine("B.this.i=" + this.i); 
            Console.WriteLine("B.base.i=" + base.i); 
        } 
 
        public new void AM2() 
        { 
            Console.WriteLine("B.this.i=" + this.i); 
            Console.WriteLine("B.base.i=" + base.i); 
            this.i = this.i + 100; 
            base.i = base.i + 1000; 
            Console.WriteLine("B.this.i=" + this.i); 
            Console.WriteLine("B.base.i=" + base.i); 
        } 
    } 

/* output
B.this.i=5
B.base.i=0
B.this.i=105
B.base.i=1000
B.this.i=105
B.base.i=1000
B.this.i=205
B.base.i=2000
 
A.this.i=10005
B.this.i=10005
B.base.i=0
B.this.i=10105
B.base.i=1000
*/ 
 
/*關於virtual,overried,new在方法和屬性上以this,base形式輸出的問題
 *問主程序的輸出是什麼
 *加了些log後
 */ 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
 
namespace ConsoleApplication2 

    class Program 
    { 
        static void Main(string[] args) 
        { 
            A a1 = new B(); 
            a1.AM1(); 
            B b1 = (B)a1; 
            b1.AM1(); 
 
            Console.WriteLine(); 
 
            A a2 = new B(); 
            a2.AM2(); 
            B b2 = (B)a2; 
            b2.AM2(); 
        } 
    } 
 
    class A 
    { 
        private int tmp; 
        public virtual int i 
        { 
            get 
            { 
                Console.WriteLine("      A.i.get=" + this.tmp); 
                return this.tmp; 
            } 
            protected set 
            { 
                Console.WriteLine("      A.i.set=" + value); 
                this.tmp = value; 
            } 
        } 
        public A() 
        { 
            this.i = 9; 
        } 
 
        public virtual void AM1() 
        { 
            this.i = this.i + 10000; 
            Console.WriteLine("A.this.i=" + this.i); 
        } 
 
        public virtual void AM2() 
        { 
            this.i = this.i + 10000; 
            Console.WriteLine("A.this.i=" + this.i); 
        } 
    } 
    class B : A 
    { 
        private int tmp; 
        public override int i 
        { 
            get 
            { 
                Console.WriteLine("      B.i.get=" + this.tmp); 
                return this.tmp; 
            } 
            protected set 
            { 
                Console.WriteLine("      B.i.set=" + value); 
                this.tmp = value; 
            } 
        } 
        public B() 
            : base() 
        { 
            this.i = 5; 
        } 
 
        public override void AM1() 
        { 
            Console.WriteLine("B.this.i=" + this.i); 
            Console.WriteLine("B.base.i=" + base.i); 
            this.i = this.i + 100; 
            base.i = base.i + 1000; 
            Console.WriteLine("B.this.i=" + this.i); 
            Console.WriteLine("B.base.i=" + base.i); 
        } 
 
        public new void AM2() 
        { 
            Console.WriteLine("B.this.i=" + this.i); 
            Console.WriteLine("B.base.i=" + base.i); 
            this.i = this.i + 100; 
            base.i = base.i + 1000; 
            Console.WriteLine("B.this.i=" + this.i); 
            Console.WriteLine("B.base.i=" + base.i); 
        } 
    } 

/* output
      B.i.set=9
      B.i.set=5
      B.i.get=5
B.this.i=5
      A.i.get=0
B.base.i=0
      B.i.get=5
      B.i.set=105
      A.i.get=0
      A.i.set=1000
      B.i.get=105
B.this.i=105
      A.i.get=1000
B.base.i=1000
      B.i.get=105
B.this.i=105
      A.i.get=1000
B.base.i=1000
      B.i.get=105
      B.i.set=205
      A.i.get=1000
      A.i.set=2000
      B.i.get=205
B.this.i=205
      A.i.get=2000
B.base.i=2000
 
      B.i.set=9
      B.i.set=5
      B.i.get=5
      B.i.set=10005
      B.i.get=10005
A.this.i=10005
      B.i.get=10005
B.this.i=10005
      A.i.get=0
B.base.i=0
      B.i.get=10005
      B.i.set=10105
      A.i.get=0
      A.i.set=1000
      B.i.get=10105
B.this.i=10105
      A.i.get=1000
B.base.i=1000
*/  

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved