Abstrakt osztályok

Filed Under (kód) by nameless on 21-07-2009

Tagged Under :

Abstrakt osztályok (kód) :

using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Shapes
{
    abstract class Shape
    {
        protected string shapeName;
 
        public Shape()
        { shapeName = "Nameless"; }
 
        public Shape(string name)
        { shapeName = name; }
 
        public abstract void Draw();
 
 
        public string PetName
        {
            get { return shapeName; }
            set { shapeName = value; }
        }
    }
 
    class Circle : Shape
    {
        public Circle() { }
        public Circle(string Name) : base(Name) { }
        public override void Draw()
        {
            Console.WriteLine("Drawing {0} the Circle", shapeName);
        }
    }
 
    class Hexagon : Shape
    {
        public Hexagon() { }
        public Hexagon(string Name) : base(Name) { }
 
        public override void Draw()
        {
            Console.WriteLine("Drawing {0} the Hexagon", shapeName);
        }
    }
 
     class Program
    {
        static void Main(string[] args)
        {
            Shape[] myShape = { new Hexagon(), new Circle(), new Hexagon("Mick"), new Circle("Beth"), new Hexagon("Linda")};
 
            foreach (var s in myShape)
                s.Draw();
 
            Console.ReadKey();
        }
}

Egy másik kód:

using System;
 
namespace Shapes
{
    abstract class Shape
    {
        protected string ShapaName;
        public Shape()
        { ShapaName = "nameless"; } 
 
        public Shape(string name)
        {
            ShapaName = name;
        }
 
        public abstract void Draw();
    }
 
    class Circle : Shape
    {
        public Circle()
            : base()
        { }
 
        public Circle(string name)
            : base(name)
        { }
 
        public override void Draw()
        {
            Console.WriteLine("I'm in the Circle, it's name is: {0}",ShapaName);
        }
    }
 
    class Hexagon : Shape
    {
        public Hexagon()
            : base()
        { }
 
        public Hexagon(string name)
            : base(name)
        { }
 
        public override void Draw()
        {
            Console.WriteLine("I'm in the Hexagon, it's name is: {0}",ShapaName);
        }
 
    }
 
    class MainClass
    {
        public static void Main()
        {
            Shape[] myShapes = { new Circle("Circi"), new Hexagon("Hexa"), new Circle("Circi2"),new Hexagon() };
 
            foreach (var i in myShapes)
                i.Draw();
 
            Console.ReadKey();
        }
    }
}

Post a comment