Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deriving encapsulated classes with different type

Tags:

c#

inheritance

I would like to override a property with a different type.

Let's say I have a Class Point3D that inherit from Point2D and a Class Drawing3D that inherit from Drawing2D.

I'd like both classes Drawing2D, and Drawing3D to have the same property name Points which would be a List of Point2D for Drawing2D, and a List of Point3D for Drawing3D.

Having the same property name, I was hoping that Drawing3D would benefit of Drawing2D methods on the property Points. The thing is I can't use override as the types are different. And the keyword new doesn't work as Drawing3D seems to have 2 properties Points (a list of Point2D, and a list of Point3D). Using the base method on a Drawing3D, it will go through the list of Point2D.

My 2D classes:

class Point2D
{
    public int X { get; set; }
    public int Y { get; set; }

    public Point2D(int x, int y)
    {
        X = x;
        Y = y;
    }

    public virtual void Print()
    {
        Console.WriteLine($"2D Point: [{X}, {Y}]");
    }
}

class Drawing2D
{
    public string Name { get; set; }
    public virtual List<Point2D> Points { get; set; }

    public Drawing2D(string name)
    {
        Name = name;
        Points = new List<Point2D>();
    }

    public void Print()
    {
        Console.WriteLine($"Drawing name: {Name}");
        foreach (var point in Points)
            point.Print();
    }
}

My 3D classes based on the 2D classes

class Point3D:Point2D
{
    public int Z { get; set; }

    public Point3D(int x, int y, int z):base(x,y)
    {
        Z = z;
    }

    public override void Print()
    {
        Console.WriteLine($"3D Point: [{X}, {Y}, {Z}]");
    }
}

class Drawing3D : Drawing2D
{
    public new List<Point3D> Points { get; set; }

    public Drawing3D(string name) : base(name)
    {
        Points = new List<Point3D>();
    }
}

And my main program

class Program
{
    static void Main(string[] args)
    {
        Drawing3D myDrawing = new Drawing3D("Sketch");
        myDrawing.Points.Add(new Point3D(1, 2, 3));
        myDrawing.Points.Add(new Point3D(4, 5, 6));
        myDrawing.Print();

        Console.ReadKey();
    }
}

The result is that it doesn't print any point. The base method Print() go through the List of Point2D, not Point3D.

Hope you guys can help me. I like to have elegant code, may be my approach isn't the right one...

Cheers

like image 350
Florian Hauguel Avatar asked Feb 20 '26 19:02

Florian Hauguel


1 Answers

If you have to do for above example then you can do something like this.

public abstract class Point
    {
       public abstract void Print();
    }

    class Point2D : Point
    {
        public int X { get; set; }
        public int Y { get; set; }

        public Point2D(int x, int y)
        {
            X = x;
            Y = y;
        }

        public override void Print()
        {
            Console.WriteLine($"2D Point: [{X}, {Y}]");
        }
    }

    class Point3D : Point2D
    {
        public int Z { get; set; }

        public Point3D(int x, int y, int z) : base(x, y)
        {
            Z = z;
        }

        public override void Print()
        {
            Console.WriteLine($"3D Point: [{X}, {Y}, {Z}]");
        }
    }


    class Drawing<T> where T:Point
    {
        public string Name { get; set; }
        public virtual List<T> Points { get; set; }

        public Drawing(string name)
        {
            Name = name;
            Points = new List<T>();
        }

        public void Print()
        {
            Console.WriteLine($"Drawing name: {Name}");
            foreach (var point in Points)
                point.Print();
        }
    }

    class Drawing3D : Drawing<Point3D>
    {
        public Drawing3D(string name) : base(name)
        {
            Name = name;            
        }
    }

    class Drawing2D : Drawing<Point2D>
    {
        public Drawing2D(string name) : base(name)
        {
            Name = name;
        }
    }
like image 190
dotnetstep Avatar answered Feb 23 '26 09:02

dotnetstep