c# abstract

iDotNetSpace發表於2008-09-27

abstract

可以修飾類、方法、屬性、索引器、事件;當一個類被abstract修飾則這個類只能被繼承;不能被初始化;可以在子類中複寫父類的方法;如

  abstract class shapsClass

{

     int i;

     public int Area();

class Square:shapsClass

{

     public int Area()

     {

          return i*i;

     }

}

同時abstract 還可以修飾他的屬性

public abstract class BaseClass

{

protected int _x=100;

protected int _y=110;

public abstract int x{get;}

public abstract int y {get;}

public abstract void AbstractMothd();

}

public class DerivedClass:shapsClass

{

     public override void AbstractMothd()

     {

           _x++;

          _y++;    

      }

     public override int x

     {

          get

          {

               return x+10;

          }

      }

     public override int y

     {

          get

          {

               return y+10;

          }

     }

 

      static void Main()
        {
            DerivedClass o = new DerivedClass();
            o.AbstractMethod();
            Console.WriteLine("x = {0}, y = {1}", o.X, o.Y);
        }

}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-465891/,如需轉載,請註明出處,否則將追究法律責任。

相關文章