打算花段时间学习C#,现将每天的学习成功写成博客,以激励自己的学习。
今天学习的是C#的接口相关知识。
接口的命名规则:
.NET文档建议接口名一大写字母"I"开头。
类继承和接口的相同点与不同点:
不同点:一个类只能继承一个基类,但是可以实现多个接口;
相同点:基类可以引用继承类的实例,接口同样也可以引用一个类,比如:
Horse horse = new Horse();
//假设Horse类实现了接口ILandBound
ILandBound myHorse = horse;//合法
接口的实现分为隐式实现和显示实现,隐式实现需要在实现的方法名前用"public"修饰符,显示实现不需要任何修饰符,但是需要在方法显示前面指定接口的名字,显示实现可以让不同的接口拥有相同的方法而不冲突。
下面是一个使用接口的例子,例子中有两个接口IDraw,IColor,分别用来画图和给图形设置颜色;还有两个类Square(正方形), Circle(圆),这两个类都实现了IDraw, IColor两个接口,通过响应鼠标的左右键来绘制不同颜色的不同图案。
代码:
IDraw.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
namespace Drawing
{
interface IDraw
{
void SetLocation(int x, int y);
void Draw(Canvas canvas);
}
}
IColor.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
namespace Drawing
{
interface IColor
{
void SetColor(Color color);
}
}
Square.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
namespace Drawing
{
class Square : IDraw, IColor
{
private int sideLength;
private int locX = 0, locY = 0;
private Rectangle rect = null;
public Square(int sideLength)
{
this.sideLength = sideLength;
}
void IDraw.SetLocation(int x, int y)
{
this.locX = x;
this.locY = y;
}
void IDraw.Draw(Canvas canvas)
{
if (this.rect != null)
{
//移除之前的形状
canvas.Children.Remove(this.rect);
}
else
{
this.rect = new Rectangle();
}
//设置正放形各项参数
this.rect.Height = this.sideLength;
this.rect.Width = this.sideLength;
Canvas.SetTop(this.rect, this.locY);
Canvas.SetLeft(this.rect, this.locX);
canvas.Children.Add(rect);
}
void IColor.SetColor(Color color)
{
if (this.rect != null)
{
SolidColorBrush brush = new SolidColorBrush(color);
this.rect.Fill = brush;
}
}
}
}
Circle.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
namespace Drawing
{
class Circle : IDraw, IColor
{
private int radius;
private int locX = 0, locY = 0;
private Ellipse circle = null;
public Circle(int radius)
{
this.radius = radius;
}
void IDraw.SetLocation(int x, int y)
{
this.locX = x;
this.locY = y;
}
void IDraw.Draw(Canvas canvas)
{
if (this.circle != null)
{
canvas.Children.Remove(circle);
}
else
{
this.circle = new Ellipse();
}
this.circle.Height = this.radius;
this.circle.Width = this.radius;
Canvas.SetTop(this.circle, this.locY);
Canvas.SetLeft(this.circle, this.locX);
canvas.Children.Add(circle);
}
void IColor.SetColor(Color color)
{
if (circle != null)
{
SolidColorBrush brush = new SolidColorBrush(color);
circle.Fill = brush;
}
}
}
}
响应鼠标进行绘图的代码:
private void drawingCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
//获取鼠标所在的位置
Point mouseLocation = e.GetPosition(this.drawingCanvas);
//设置正放形大小
Square square = new Square(100);
if (square is IDraw)
{
IDraw drawSquare = square;
//设置正放形绘图位置
drawSquare.SetLocation((int)mouseLocation.X, (int)mouseLocation.Y);
//把设置好的正方形画出来
drawSquare.Draw(drawingCanvas);
}
if (square is IColor)
{
IColor squareColr = square;
//设置填充颜色
squareColr.SetColor(Colors.BlueViolet);
}
}
private void drawingCanvas_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
Point mouseLocation = e.GetPosition(this.drawingCanvas);
Circle circle = new Circle(100);
if (circle is IDraw)
{
IDraw drawCircle = circle;
drawCircle.SetLocation((int)mouseLocation.X, (int)mouseLocation.Y);
drawCircle.Draw(drawingCanvas);
}
if (circle is IColor)
{
IColor circleColr = circle;
circleColr.SetColor(Colors.HotPink);
}
}
效果图:
图书参考:《Visual C# 2010 从入门到精通》