- 论坛徽章:
- 24
|
回复 1# 边学CPP边骂CPP
以后这么写比较好。 
- public class Shape
- {
- // A few example members
- public int X { get; private set; }
- public int Y { get; private set; }
- public int Height { get; set; }
- public int Width { get; set; }
- // Virtual method
- public virtual void Draw()
- {
- Console.WriteLine("Performing base class drawing tasks");
- }
- }
- class Circle : Shape
- {
- public override void Draw()
- {
- // Code to draw a circle...
- Console.WriteLine("Drawing a circle");
- base.Draw();
- }
- }
- class Rectangle : Shape
- {
- public override void Draw()
- {
- // Code to draw a rectangle...
- Console.WriteLine("Drawing a rectangle");
- base.Draw();
- }
- }
- class Triangle : Shape
- {
- public override void Draw()
- {
- // Code to draw a triangle...
- Console.WriteLine("Drawing a triangle");
- base.Draw();
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- // Polymorphism at work #1: a Rectangle, Triangle and Circle
- // can all be used whereever a Shape is expected. No cast is
- // required because an implicit conversion exists from a derived
- // class to its base class.
- System.Collections.Generic.List<Shape> shapes = new System.Collections.Generic.List<Shape>();
- shapes.Add(new Rectangle());
- shapes.Add(new Triangle());
- shapes.Add(new Circle());
- // Polymorphism at work #2: the virtual method Draw is
- // invoked on each of the derived classes, not the base class.
- foreach (Shape s in shapes)
- {
- s.Draw();
- }
- // Keep the console open in debug mode.
- Console.WriteLine("Press any key to exit.");
- Console.ReadKey();
- }
- }
- /* Output:
- Drawing a rectangle
- Performing base class drawing tasks
- Drawing a triangle
- Performing base class drawing tasks
- Drawing a circle
- Performing base class drawing tasks
- */
复制代码 |
|