asddasd

导航

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

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

 

 

 

 

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");

var c = new Counter();

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

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");

var c1 = new CounterWithLock();

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();
}

}
}

 

 当主线程启动时,创建了一个Counter类的对象。该类定义了一个递增和递减的计数器,启动了三个线程,三个线程共享同一个counter实例,在同一个周期中进行一次递增和一次递减。

posted on 2019-11-08 10:09  asddasd  阅读(3329)  评论(0编辑  收藏  举报