Should I use protected fields or public properties?

Started by
4 comments, last by SiCrane 11 years, 5 months ago
I have an abstract class Character


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CW_Project2
{
abstract class Character
{
int HP;
int SP;
int EP;
int Attack;
int Defense;
int Endurance;
int Speed;
int Affinity;
public Character()
{
Attack = 5;
Defense = 5;
Endurance = 5;
Speed = 5;
Affinity = 0;
setBasePoints();
}
public Character(int initialAttack, int initialDefense, int initialEndurance, int initialSpeed, int initialAffinity)
{
Attack = initialAttack;
Defense = initialDefense;
Endurance = initialEndurance;
Speed = initialSpeed;
Affinity = initialAffinity;
setBasePoints();
}
public Character(Attributes loadAttributes, int loadHP, int loadSP, int loadEP)
{
Attack = loadAttributes.Attack;
Defense = loadAttributes.Defense;
Endurance = loadAttributes.Endurance;
Speed = loadAttributes.Speed;
Affinity = loadAttributes.Affinity;
HP = loadHP;
SP = loadSP;
EP = loadEP;
}
private void setBasePoints()
{
Random rnd = new Random();
HP = (((Defense + Endurance) * 10) / 8);
SP = (((Attack + Endurance) * 10) / 7);
EP = (((rnd.Next(10) + Endurance) * 10) / 6);
}
pu
}
}


And a Player class that inherits Character:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CW_Project2
{
class Player : Character
{
long playerID;
CharacterClass playerClass;

public Player()
{
}
public Player(int playerAttack, int playerDefense, int playerEndurance, int playerSpeed, int playerAffinity)
: base(playerAttack, playerDefense, playerEndurance, playerSpeed, playerAffinity)
{

}

public Player(CharacterClass initialClass, int bonusAttack, int bonusDefense, int bonusEndurance, int bonusSpeed, int bonusAffinity)
: this(initialClass.getClassAttributes().Attack + bonusAttack,
initialClass.getClassAttributes().Defense + bonusDefense,
initialClass.getClassAttributes().Endurance + bonusEndurance,
initialClass.getClassAttributes().Speed + bonusSpeed,
initialClass.getClassAttributes().Affinity + bonusAffinity)
{
playerClass = initialClass;
playerID = DateTime.Now.Ticks;
}
}
}


Now if I want the Player class to access the Characters class private fields, should I just make the fields Protected or create Public Properties? Basically the scenario that I am thinking of is when the Player levels up, I'll need to add points to those attributes.

Beginner in Game Development?  Read here. And read here.

 

Advertisement
When writing the getters/setters for properties you can specify their access modifier.

Such as:


public int HP
{
get { return HP;}
protected set { HP = value;}
}


or any combination thereof. The example above allows anyone to access the players HP but only sub-classes can set it. Pretty handy.
In C# 3 and higher, you can use this shorthand to avoid having to create a field in addition to the property.


public int HP { get; protected set; } // 'public' in this case modifies the getter.
A protected field in C# is pretty much never the right design choice for pretty much the same reason that a protected member variable in C++ is almost never the right decision: if you have non-public members that means your class has class invariants. If you make a field protected then all derived classes are responsible for maintaining the class invariant, which means that if you have a bug the number of places you need to look for the problem is multiplied significantly.
So what would be the right/correct approach for my problem? Because it seems like you are disagreeing with the solutions above.

Beginner in Game Development?  Read here. And read here.

 

No, the two other posters are talking about properties, not fields. Properties can have code that maintain class invariants attached to them, unlike direct access with fields.

This topic is closed to new replies.

Advertisement