C#中的abstract 類和方法!!!

mark: https://www.cnblogs.com/zzy2740/archive/2005/09/20/240808.htmlhtml

 

 

 

 

C#中的abstract 類和方法!!!

 
C#中的abstract類不能被實例化,他只提供其餘類的繼承的接口

using System;
abstract class MyAbs
{
public void NonAbMethod()
{
Console.WriteLine("Non-Abstract Method");
}
}

class MyClass : MyAbs
{
}

class MyClient
{
public static void Main()
{
//MyAbs mb = new MyAbs();//不能實例化

MyClass mc = new MyClass();
mc.NonAbMethod();
}
}

一個抽象類能夠包含abstract方法,也可包含實例化方法,但繼承類(非抽象)必須實現abstract方法
using System;

abstract class MyAbs
{
public void NonAbMethod()
{
Console.WriteLine("Non-Abstract Method");
}
public abstract void AbMethod(); // 抽象方法,只有聲明,沒有實現
}

class MyClass : MyAbs//必須實現抽象方法
{
public override void AbMethod()
{
Console.WriteLine("Abstarct method");

}

class MyClient
{
public static void Main()
{
MyClass mc = new MyClass();
mc.NonAbMethod();
mc.AbMethod();
}
}

固然繼承類也能夠是抽象的

using System;

abstract class MyAbs
{
public abstract void AbMethod1();
public abstract void AbMethod2();
}

//抽象繼承類沒必要所有實現抽象方法,部分實現即可

abstract class MyClass1 : MyAbs
{
public override void AbMethod1()
{
Console.WriteLine("Abstarct method #1");

}

class MyClass : MyClass1
{
public override void AbMethod2()
{
Console.WriteLine("Abstarct method #2");
}
}

class MyClient
{
public static void Main()
{
MyClass mc = new MyClass();
mc.AbMethod1();
mc.AbMethod2();
}
}

抽象類能夠繼承自非抽象類

using System;

class MyClass1 
{
public void Method1()
{
Console.WriteLine("Method of a non-abstract class");
}
}

abstract class MyAbs : MyClass1 
{
public abstract void AbMethod1(); 
}

class MyClass : MyAbs//實例類必須實現抽象方法
{
public override void AbMethod1()
{
Console.WriteLine("Abstarct method #1 of MyClass");
}
}


class MyClient
{
public static void Main()
{
MyClass mc = new MyClass();
mc.Method1();
mc.AbMethod1();

}
}

抽象類能夠實現接口

using System;

interface IInterface
{
void Method1();
}

abstract class MyAbs : IInterface
{
public void Method1()
{
Console.WriteLine("Method implemented from the IInterface");
}
}

class MyClass : MyAbs 
{

}


class MyClient
{
public static void Main()
{
MyClass mc = new MyClass();
mc.Method1();
}
}

最後須要注意的是抽象類不能聲明爲sealed,這兩個語義是衝突的。抽象方法沒必要(也不能)聲明爲virtual,由於它缺省隱含就爲virtual!
 
 
附:在多線程加鎖使用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;多線程

namespace ConsoleApp3_1._10_lock_20191107
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Incorrect counter");ide

var c = new Counter();post

var t1 = new Thread(() => TestCounter(c));
var t2 = new Thread(() => TestCounter(c));
var t3 = new Thread(() => TestCounter(c));spa

t1.Start();
t2.Start();
t3.Start();
t1.Join();
t2.Join();
t3.Join();線程

Console.WriteLine("Total count: {0}", c.Count);
Console.WriteLine("--------------");
Console.WriteLine("Correct counter");htm

var c1 = new CounterWithLock();blog

t1 = new Thread(() => TestCounter(c1));
t2 = new Thread(() => TestCounter(c1));
t3 = new Thread(() => TestCounter(c1));繼承

t1.Start();
t2.Start();
t3.Start();
t1.Join();
t2.Join();
t3.Join();接口

Console.WriteLine("Total count: {0}", c1.Count);
Console.ReadLine();


}

static void TestCounter(CounterBase c)
{
for(int i = 0; i < 10000; i++)
{
c.Increment();
c.Decrement();

}
}

class Counter : CounterBase
{
public int Count { get; private set; }

public override void Increment()
{ Count++; }

public override void Decrement()
{ Count--; }
}

class CounterWithLock : CounterBase
{
private readonly object _syncRoot = new Object();

public int Count { get; private set; }

public override void Increment()
{
lock (_syncRoot)
{
Count++;
}
}

public override void Decrement()
{
lock (_syncRoot)
{
Count--;
}
}

}

abstract class CounterBase
{
public abstract void Increment();
public abstract void Decrement();
}

}}

相關文章
相關標籤/搜索