public class Shape
{
public virtual void Draw()
{
Console.WriteLine("base class drawing");
}
}
public class Rectangle :Shape
{
public override void Draw()
{
Console.WriteLine("Drawing a Rectangle");
}
}
public class Square :Rectangle
{
public override void Draw()
{
Console.WriteLine("Drawing a Square");
base.Draw();
}
}
class Program
{
static void Main(string[]args)
{
System.Collections.Generic.List<Shape> shapes =new List<Shape>();
shapes.Add(new Rectangle());
shapes.Add(new Square());
foreach(Shape s in shapes)
{
s.Draw();
}
Console.ReadKey();
/*运行结果
Drawing a Rectangle
Drawing a Square
Drawing a Rectangle
*/
}
}
public new void Draw()
{
Console.WriteLine("Drawing a Square");
base.Draw();
}
/*运行结果 Drawing a Rectangle Drawing a Rectangle */
public class Rectangle :Shape
{
public sealed override voidDraw()
{
Console.WriteLine("Drawing a Rectangle");
}
}
static void Main(string[] args)
{
System.Collections.Generic.List<Shape>shapes =new List<Shape>();
shapes.Add((Shape)new Rectangle());
shapes.Add((Shape)new Square());
foreach(Shape s inshapes)
{
s.Draw();
}
Console.ReadKey();
/*运行结果
Drawing a Rectangle
Drawing a Square
Drawing a Rectangle
*/
}
public abstract classShape
{
public abstract void Draw();
}
public class Rectangle :Shape
{
public override void Draw()
{
Console.WriteLine("Drawing a Rectangle");
}
}
public class Square :Rectangle
{
public override void Draw()
{
Console.WriteLine("Drawing a Square");
base.Draw();
}
}
class Program
{
static void Main(string[]args)
{
System.Collections.Generic.List<Shape>shapes =new List<Shape>();
shapes.Add(new Rectangle());
shapes.Add(new Square());
foreach(Shape s in shapes)
{
s.Draw();
}
Console.ReadKey();
}
}
public interface IShape
{
void Draw();
}
public class Rectangle :IShape
{
public void Draw()
{
Console.WriteLine("Drawing a Rectangle");
}
}
public class Square: IShape
{
public void Draw()
{
Console.WriteLine("Drawing a Square");
}
}
class Program
{
static void Main(string[]args)
{
System.Collections.Generic.List<IShape>shapes =new List<IShape>();
shapes.Add(new Rectangle());
shapes.Add(new Square());
foreach(IShape s inshapes)
{
s.Draw();
}
Console.ReadLine();
}
}
public interface IShape
{
void Draw();
}
public class Shape:IShape
{
void IShape.Draw()
{
Console.WriteLine("Shape IShape.Draw()");
}
public virtual void Draw()
{
Console.WriteLine("Shape virtual Draw()");
}
}
public class Rectangle :Shape,IShape
{
void IShape.Draw()
{
Console.WriteLine("Rectangle IShape.Draw()");
}
public newvirtual void Draw()
{
Console.WriteLine("Rectangle virtual Draw()");
}
}
public class Square :Rectangle
{
public override void Draw()
{
Console.WriteLine("Square override Draw()");
}
}
class Program
{
static void Main(string[]args)
{
Square squre = new Square();
Rectangle rect = squre;
Shape shape = squre;
IShape ishape = squre;
squre.Draw();
rect.Draw();
shape.Draw();
ishape.Draw();
Console.ReadLine();
}
}
/*运行结果:
Square override Draw()①
Square override Draw()②
Shape virtual Draw()③
Rectangle IShape.Draw()④
*/
public class Base
{
public virtual void Write(int num)
{
Console.WriteLine("int:" + num.ToString());
}
}
public class Derived :Base
{
public override void Write(int num)
{
Console.WriteLine("derived:" + num.ToString());
}
public void Write(double num)
{
Console.WriteLine("derived double:" + num.ToString());
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有