C # Inheriting a Variable from a Variable #

I'm a little new to C #, but I have pretty extensive programming experience.

What I'm trying to do: Define the various MapTiles for the game. I defined the base MapTile class as follows:

public class MapTile { public Texture2D texture; public Rectangle mapRectangle; public MapTile(Rectangle rectangle) { this.mapRectangle = rectangle; } } 

Then I define a subclass of GrassTile as follows:

 class GrassTile : MapTile { new Texture2D texture = Main.GrassTileTexture; new public Rectangle mapRectangle; public GrassTile(Rectangle rectangle) : base(rectangle) { this.mapRectangle = rectangle; } } 

In my main class, I create a new maptile as follows:

 Maptile testTile; testTile = new GrassTile(new Rectangle(0, 0, 50, 50); 

However, when I try to make this testTile its texture ends with zero. My code works fine if I define a texture inside a MapTile, so it has nothing to do with my previous implementation.

So, how can I get GrassTile to modify the texture variable of a MapTile member? or get my main class to recognize the GrassTile texture instead of the MapTile that I used with interfaces, but I cannot declare interface member variables. Is there anything else for C # inheritance that I haven't received yet?

Thank you in advance

+4
source share
5 answers

The Child class inherits the members of the parent class. You do not need to specify them. It is also better to use properties rather than public fields.

 public class MapTile { public Texture2D Texture { get; set; } public Rectangle MapRectangle { get; set; } public MapTile(Rectangle rectangle) { MapRectangle = rectangle; } } public class GrassTile : MapTile { public GrassTile(Rectangle rectangle) : base(rectangle) { Texture = Main.GrassTileTexture; } } 
+7
source

You have a little syntax confusion. A β€œnew” keyword can be used as an operator to create something that you mostly do, or as a modifier for members to hide inherited elements and what you do in your GrassTile. You essentially redefine your members there. The correct version could be:

  class GrassTile : MapTile { public GrassTile(Rectangle rectangle) : base(rectangle) { texture = Main.GrassTileTexture; } } 

as your rectangle will be set in the base constructor.

+2
source

Inheritance makes inherited fields persist in the derived class.

 public sealed class GrassTile : MapTile { public GrassTile(Rectangle rectangle) : base(rectangle) { texture = Main.GrassTileTexture; } } 
0
source

I think the best thing here is to explain how you use the new keyword. new can be used in two modes. As an operator and as a modifier.

new "Operator"

The new operator is used to create objects and call constructors, for example:

 Class1 MyClass = new Class1(); 

new modifier

Use the new modifier to explicitly hide an element inherited from the base class. To hide an inherited element, declare it in the derived class using the same name and change it with the new modifier.

Consider the following class:

 public class MyBaseC { public int x; public void Invoke() {} } 

Declaring a member named Invoke in a derived class will hide the Invoke method in the base class, that is:

 public class MyDerivedC : MyBaseC { new public void Invoke() {} } 

However, the x field will not be affected because it is not hidden by a similar name.

A name hiding through inheritance takes one of the following forms:

  • A constant, field, property, or type introduced into a class or struct hides all the base members of the class with the same name.

  • A method introduced in a class or struct hides properties, fields, and types with the same name in the base class. It also hides all methods of the base class with the same signature.

  • An index entered in a class or struct hides all indexes of the base class with the same signature.

Because you hide the name and declare a Maptile type, but initialize the GrassTile , the AND type of the Maptile parent type never initialized your Texture member, so you see null .

0
source

A few things to improve on your code;

Firstly, the Texture property is already defined in the base MapTile class, so it does not need to be redefined in the GrassTile class; a derived class (GrassTile) inherits this element from the base class (MapTile).

Further, you might consider modifying the base class "MapTile" as abstract , since I believe that it does not have direct behavior (the texture should be provided by concrete execution of the class).

Then you can change the Texture declaration as protected , since in fact it should not be accessible outside the class hierarchy in this way, Or else, turn it into a property using get and set accessors.

Finally, the misuse of the new modifier. This modifier is intended only (in this context) so that you can override the base implementation of any behavior with the new behavior (and not inherit the behavior of the base). In this case, you declare a β€œnew” texture in GrassTile, which (not needed) will override the base instance, but ONLY when referenced through the GrassTile class.

 { MapTile m1 = new GrassTile(...); //m1.texture == null; GrassTile m2 = new GrassTile(...); //m2.texture = Main.GrassTileTexture } 
0
source

All Articles