求帮助 c#自定义接口练习

.Net技术 码拜 7年前 (2017-04-16) 820次浏览
class Program
{
abstract class Shape
{
public Shape(string name = “NoName”)
{ PetName = name; }
public string PetName { get; set; }
public virtual void Draw()
{
Console.WriteLine(“Inside Shape,Draw()”);
}
}
public interface IPointy
{
byte Points { get; }
}
class Triangle : Shape, IPointy
{
public Triangle() { }
public Triangle(string name) : base(name) { }
public override void Draw()
{ Console.WriteLine(“Drawing {0} the Triangle”, PetName); }
public byte Points
{
get{return 3;}
}
}
class Hexagon :Shape ,IPointy
{
public Hexagon (){}
public Hexagon (string name):base(name){}
public override void Draw()
{Console.WriteLine(“Drawing {0} the Hexagon”, PetName);}
public byte Points
{
get{return 3;}
}
}
static void Main(string[] args)
{
Console.WriteLine(“*****Fun with Interfaces*****\n”);
Hexagon hex = new Hexagon();
Console.WriteLine(“Points:{0}”, hex.Points);
Console.ReadLine();
}
}
b) 建一个抽象基类数组,数组成员为 Hexgon 类对象和 Triangle 对象,用 as, is  判断某个数组成员能否支持某个接口,写出改写的程序,并写出结果
求问这道题怎么做。
解决方案

100

 Shape[] shapes = new Shape[] { new Hexagon(), new Triangle() };
            foreach (var shape in shapes)
            {
                if (shape is IPointy)
                {
                    IPointy hex = shape as IPointy;
                    Console.WriteLine(hex.Points);
                }
            }

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明求帮助 c#自定义接口练习
喜欢 (0)
[1034331897@qq.com]
分享 (0)