using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace MyCS
{
public class Class1
{
public void function()
{
Cats cat = new Cats();
cat.CatToJiao();
}
public void function2()
{
Cat cat = new Cat();
Mouse mouse1 = new Mouse("mouse1", cat);
Mouse mouse2 = new Mouse("mouse1", cat);
Master master = new Master(cat);
cat.Cry();
}
}
#region 方式1
public interface observer
{
void Response();
}
public interface Subject
{
void Amiat(observer obs);
}
public class Mouse : observer
{
private string name;
public Mouse(string name, Subject subj)
{
this.name = name;
subj.Amiat(this);
}
public void Response()
{
Console.WriteLine(name + " run!");
}
}
public class Master : observer
{
public Master(Subject subj)
{
subj.Amiat(this);
}
public void Response()
{
Console.WriteLine(" Host Waken!");
}
}
public class Cat : Subject
{
private ArrayList objservers;
public Cat()
{
this.objservers = new ArrayList();
}
public void Amiat(observer obs)
{
this.objservers.Add(obs);
}
public void Cry()
{
Console.WriteLine("Cat cryed!");
foreach (observer obs in this.objservers)
{
obs.Response();
}
}
}
#endregion
#region 方式2
public delegate void SubToEventHandler();
public abstract class Sutobject
{
public event SubToEventHandler sutoEvent;
protected void ToEvents()
{
if (this.sutoEvent != null)
{
this.sutoEvent();
}
}
}
public class Cats : Sutobject
{
private string name;
public void CatToJiao()
{
Console.WriteLine(" cat 猫叫");
ToEvents();
}
}
public abstract class Objectservers
{
public Objectservers(Sutobject sub)
{
sub.sutoEvent += new SubToEventHandler(sub_sutoEvent);
}
public abstract void sub_sutoEvent();
}
public class MouseClass : Objectservers
{
public string Name;
public MouseClass(string name, Sutobject sub)
: base(sub)
{
this.Name = name;
}
public override void sub_sutoEvent()
{
Console.WriteLine(Name + " 老鼠反应开始跑了");
}
}
public class RenClass : Objectservers
{
public string Name;
public RenClass(string name, Sutobject sub)
: base(sub)
{
this.Name = name;
}
public override void sub_sutoEvent()
{
Console.WriteLine(Name + " 人醒来了");
}
}
#endregion
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace MY_WCMD_CS
{
class Program
{
private static int num = 100;
public static Mutex mutex = new Mutex();
static void TestFuenction()
{
Program class1 = new Program();
//创建线程
Thread first = new Thread(new ThreadStart(new Program().MethodOne));
Thread two = new Thread(new ThreadStart(new Program().MethodTwo));
//启动线程
first.Start();
two.Start();
while (true)
{
int ch = Console.Read();
if (ch == ‘q‘)
{
first.Abort();
two.Abort();
break;
}
}
Console.WriteLine("主线程退出!");
Console.ReadLine();
Console.ReadLine();
}
//纯种方法一
public void MethodOne()
{
while (true)
{
Program.mutex.WaitOne();
if (num > 0)
{
Thread.Sleep(2000);
Console.WriteLine("2 现在售第" + num + "张票.");
num--;
}
Program.mutex.ReleaseMutex();
}
}
//线程方法二
public void MethodTwo()
{
while (true)
{
Program.mutex.WaitOne();
if (num > 0)
{
Thread.Sleep(2000);
Console.WriteLine("1 现在售第" + num + "张票.");
num--;
}
Program.mutex.ReleaseMutex();
}
}
}
/************************************************************************/
/* */
/************************************************************************/
public interface IFen
{
}
public static class IFen接口扩展
{
//
public static void Fen<T>(this T t) where T : IFen
{
Console.WriteLine();
}
}
public class A类
{
public virtual void 方法1()
{
Console.WriteLine("我是A类的方法1");
}
}
public class B类:IFen
{
public void 方法x()
{
Console.WriteLine("");
}
}
public class C类 : A类,IFen
{
public override void 方法1()
{
Console.WriteLine("我是A类的子类,方法1");
}
public void 方法x()
{
this.Fen();
}
}
}