Hiding properties in C # derived classes

I have four classes that share some arrangement of four properties. I am currently setting the base class as abstractwith every property marked as virtual. Then, in each of the four derived classes, I redefine the properties that it uses, and ignoring the rest.

The problem is that I can still access all the properties in each of the derived classes, regardless of whether I redefined it from a property abstract virtualin the base class.

I have a feeling that I'm approaching this from the wrong angle. Is there a way to explicitly hide or block properties, or is there a better approach.

+3
source share
5 answers

I believe that you should reconsider your inheritance hierarchy. Consider the following heuristics:

If two or more classes use only shared data (there is no common behavior), then the shared data should be placed in the class that will be contained in each shared class.

If two or more classes have common data and behavior (i.e. methods), then these classes must inherit from a common base class that captures this data and methods.

If two or more classes have a common interface (i.e. messages, not methods), then they should inherit from a common base class only if they will be used polymorphically.

+9
source

, . ( ) , . , , , .

//Level1
public abstract class Employee
{
public string Name{get;set;}
public abstract double CalculateSalary();

}

//Level2
public abstract class CalssAEmployee:Employee
{
public int NumberOfWorkingHours{get;set;}

}

public abstract class ClassBEmployee:Employee
{
public int NumberOfSales{get;set;}
}
+2

/ , .

, , .

, .

, , , .

+1

, - , . , , :

public abstract class Client : Entity
{
    public virtual string Name { get; set; }
    public virtual string  Comments { get; set; }
    public virtual string  Requirement { get; set; }
    public virtual string  Complaints { get; set; }
 }

public class GreatClient : Client
{
    public virtual List<GreatOrder>  Orders { get; set; }
}

public class WebClient : Client
{
    public virtual List<BadOrder> Orders { get; set; }
}

public static class ClientHelper
{
    public static IEnumerable<Order> GetOrders(this Client client)
    {
        var result = new Dictionary<Type, Func<Client>>
                         {
                             {typeof (GreatClient), () => { return ((GreatClient) client).Orders;}},
                             {typeof (WebClient), ()=> { return ((WebClient) client).Orders;}}
                         };

        return result[dto.GetType()].Invoke();
    }
}

//Client code
Client client = new WebClient();
client.GetOrders();

, Nap. , , .

+1

.

if you have an abstract class with virtual properties, it can be protected or publicly, and this is what the outside world will see. When you extend / inherit this class, you can override these properties, but not mark them as private. Private is the only way to hide a property from a derived class. The presence of private property in an abstract class will hide it from children. But this is clearly not the case in your scenario.

0
source

Source: https://habr.com/ru/post/1717556/


All Articles