抽象类、接口、事件、Action、Func
2015-05-18 20:44
295 查看
抽象类负责对类的抽象,经常使用在Template等模式中
接口负责对类的行为的抽象
事件负责对两个对象的通信,而不需要建立两个对象直接联系,避免了对象的间的耦合
Action与Func在基于策略模式实现弱依赖关系,即Test(IStrategy strategy);实现对抽象方法取代真实方法的调用。
抽象类实现
public abstract class Test
{
public virtual int Add(int x,int y)
{
return x + y;
}
public abstract int Muliti(int x, int y);
}
接口实现
public interface IOperation
{
void Logger(string message);
}
public abstract class Test:IOperation
{
public virtual int Add(int x,int y)
{
return x + y;
}
public abstract int Muliti(int x, int y);
public void Logger(string message)
{
throw new NotImplementedException();
}
}
事件实现
Action与Func实现对常见异常代码的封装和调用
class _09exception
{
public static void Main1()
{
BussinessHelper.Action(() =>
{
throw new MyException("aa", "bb");
});
}
}
class BussinessHelper
{
public static void Action(Action action)
{
try
{
if (action != null)
action();
}
catch (MyException ex)
{
}
}
}
class MyException : Exception
{
public MyException(string message)
: base(message)
{
}
public MyException(string message, string title)
:base(message)
{
Console.WriteLine(message);
}
}
接口负责对类的行为的抽象
事件负责对两个对象的通信,而不需要建立两个对象直接联系,避免了对象的间的耦合
Action与Func在基于策略模式实现弱依赖关系,即Test(IStrategy strategy);实现对抽象方法取代真实方法的调用。
抽象类实现
public abstract class Test
{
public virtual int Add(int x,int y)
{
return x + y;
}
public abstract int Muliti(int x, int y);
}
接口实现
public interface IOperation
{
void Logger(string message);
}
public abstract class Test:IOperation
{
public virtual int Add(int x,int y)
{
return x + y;
}
public abstract int Muliti(int x, int y);
public void Logger(string message)
{
throw new NotImplementedException();
}
}
事件实现
public interface IOperation { void Logger(string message); } public abstract class Test:IOperation { public event Action OnRefresh; public virtual int Add(int x,int y) { if (OnRefresh != null) { Action action = OnRefresh; action(); } return x + y; } public abstract int Muliti(int x, int y); public void Logger(string message) { throw new NotImplementedException(); } }
Action与Func实现对常见异常代码的封装和调用
class _09exception
{
public static void Main1()
{
BussinessHelper.Action(() =>
{
throw new MyException("aa", "bb");
});
}
}
class BussinessHelper
{
public static void Action(Action action)
{
try
{
if (action != null)
action();
}
catch (MyException ex)
{
}
}
}
class MyException : Exception
{
public MyException(string message)
: base(message)
{
}
public MyException(string message, string title)
:base(message)
{
Console.WriteLine(message);
}
}
相关文章推荐
- 通过IL分析C#中的委托、事件、Func、Action、Predicate之间的区别与联系
- 【设计模式】抽象类、接口、委托和事件
- 工作总结-C#抽象类,接口,以及UnityAction
- 通过IL分析C#中的委托、事件、Func、Action、Predicate之间的区别与联系
- 抽象类,接口,委托与事件
- 温故而知新:Delegate,Action,Func,匿名方法,匿名委托,事件
- Delegate,Action,Func,匿名方法,匿名委托,事件
- C#中的事件,委托,抽象类,接口
- C# 我理解的接口、抽象类、以及事件
- 温故而知新:Delegate,Action,Func,匿名方法,匿名委托,事件
- Delegate,Action,Func,匿名方法,匿名委托,事件 (转载)
- Delegate,Action,Func,匿名方法,匿名委托,事件
- 温故而知新:Delegate,Action,Func,匿名方法,匿名委托,事件
- Delegate,Action,Func,匿名方法,匿名委托,事件
- 温故而知新:Delegate,Action,Func,匿名方法,匿名委托,事件
- Delegate,Action,Func,匿名方法,匿名委托,事件
- C#解惑2——Delegate,Action,Func,匿名方法,匿名委托,事件(转)
- 事件,委托,action与func文章不错的
- c#中设置属性 索引 接口 泛型 抽象类 委托 事件
- Delegate,Action,Func,匿名方法,匿名委托,事件