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 判断某个数组成员能否支持某个接口,写出改写的程序,并写出结果
求问这道题怎么做。
{
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); } }