C # Database Inheritance

This problem was on / off, and I wrote 4 wrappers. I am sure that I am doing it wrong, and I need to find out where my understanding comes from.

Let's say I use the Rectangle class to represent metal bars. (it works better than animals)

So, the base class is called "Bar".

private class Bar
{
   internal Rectangle Area;
}

So. Now we make a rectangle, say 300 units per 10 units.

private Bar CreateBar()
{
    Bar1 = new Bar1();
    Bar1.Area = new Rectangle(new Point(0,0), new Size(300,10));
    return Bar1;
}

Fantastic, so we have a basic panel.

Now let me say that we want this bar to have material - say, steel. So.,.

private class SteelBar : Bar
{
    string Material;
}

So, if I did it.,.

private SteelBar CreateSteelBar()
{
    SteelBar SteelB = new SteelB();
    Bar B = CreateBar();
    SteelB = B;
    SteelB.Material = "Steel";
    return SteelB;
}

, , CreateSteelBar, , CreateBar. , 300 10 . .

- , , . , , , , , , - .

, , SteelBar = CreateBar();, .

+5
8

, (CreateBar), :

public Bar()
{
    this.Area = new Rectangle(new Point(0,0), new Size(300,10));
}

SteelBar :

public SteelBar()
{
    this.Material = "Steel";
}

, . , :

public SteelBar()
    : base()
{
    this.Material = "Steel";
}

, , .

. #.

+16

:

Bar B = CreateBar();
SteelB = B;

B - . SteelBar. , (, , ,...). :

SteelBar sb = new SteelBar();
Bar b = sb;

a SteelBar , .

, , SteelBar = CreateBar()

, . ; CreateBar , SteelBar.

+3

pycruft, . , SteelBar , Material. .

, , factory, SteelBar. Bar. Factory Pattern.

, , .

+1

,

, , - Fruit apple. - "is-a". apple is-a Fruit, a Fruit a apple, orange, banana...

, SteelBar Bar, Bar SteelBar, WoodenBar, ,

+1

:

SteelBar = B;

Bar, SteelBar.

0

, , :

Bar B = CreateBar();
SteelBar = B;

, SteelBar Bar ( ), .

:

SteelBar B = CreateBar();
Bar = (Bar)B;

SteelBar Bar, , WoodenBar.

0

, , ... . IMeasureable IPhysical, ...

BaseClass GUI, , .

, DisplayObject, IMeasurable IPhysical...

0

:

public class Super {
}

public class Sub : Super {
}

If you create a Super object, you cannot assign it to Sub, because it is not "Sub". OO programming allows you to “process” objects in terms of less derived classes, but this simple fact still remains:

You have created Super Non Sub.

The only way to create Sub from Super would be to create new values ​​for the Sub and copy properties. A tool like AutoMapper works well to do this quickly, otherwise you will get a bunch of lines of mundane fragile code.

0
source

All Articles