简体   繁体   中英

C# “Rename” Property in Derived Class

When you read this you'll be awfully tempted to give advice like "this is a bad idea for the following reason..."

Bear with me. I know there are other ways to approach this. This question should be considered trivia.

Lets say you have a class "Transaction" that has properties common to all transactions such as Invoice, Purchase Order, and Sales Receipt.

Let's take the simple example of Transaction "Amount", which is the most important monetary amount for a given transaction.

public class Transaction
{
    public double Amount { get; set; }

    public TxnTypeEnum TransactionType { get; set; }
}

This Amount may have a more specific name in a derived type... at least in the real world. For example, the following values are all actually the same thing:

  • Transaction - Amount
  • Invoice - Subtotal
  • PurchaseOrder - Total
  • Sales Receipt - Amount

So now I want a derived class "Invoice" that has a Subtotal rather than the generically-named Amount. Ideally both of the following would be true:

  1. In an instance of Transaction, the Amount property would be visible.
  2. In an instance of Invoice, the Amount property would be hidden, but the Subtotal property would refer to it internally.

Invoice looks like this:

public class Invoice : Transaction
{
    new private double? Amount
    {
        get
        {
            return base.Amount;
        }
        set
        {
            base.Amount = value;
        }
    }

    // This property should hide the generic property "Amount" on Transaction
    public double? SubTotal
    {
        get
        {
            return Amount;
        }
        set
        {
            Amount = value;
        }
    }

    public double RemainingBalance { get; set; }
}

But of course Transaction.Amount is still visible on any instance of Invoice.

Thanks for taking a look!

Thanks for all the help.

OK, of course you cannot "hide" public properties on the base class when the derived class IS a base instance. Somewhere deep in my brain I already knew that. Doh!

I wound up getting the syntactic sugar to behave the way I wanted for the consumer by using a third class called TransactionBase. This class is abstract and contains the shared, non-aliased stuff that exists for all transactions like currency, exchange rate, created/modified date and time, transaction date, etc... in addition to aliased stuff like Amount.

Here, I just show the Amount property in question:

public abstract class TransactionBase
{
    protected virtual double Amount { get; set; }
}

Then Transaction looks like this:

public class Transaction : TransactionBase
{
    public new double Amount 
    { 
        get
        {
            return base.Amount;
        }
        set
        {
            base.Amount = value;
        }
    }
}

And Invoice:

public class Invoice : TransactionBase
{
    public double SubTotal
    {
        get
        {
            return Amount;
        }
        set
        {
            Amount = value;
        }
    }
}

And access works the way I wanted:

var transaction = new Transaction();

// This line works fine:
var transactionAmount = transaction.Amount;



var invoice = new Invoice();

// This line works fine:
var invoiceSubtotal = invoice.SubTotal;

// This line won't compile.
// Error: TransactionBase.Amount is inaccessible due to its protection level.
var invoiceAmount = invoice.Amount;

So the answer to my original question was, "no" you cannot hide public inherited members. The above solution fakes it with accessing the protected member directly in the derived types, but it still sort of sucks. Too much typing.

Of course, now that I fiddled and piddled with all that, I'm seeing that a better solution throws out the protected members altogether and saves me some typing. By the way, YES I am embarrassed that I didn't jump immediately to this solution.

EDIT: Actually, the first appraoch in my answer might be better. With the 2nd one, I'd lose the "Amount" or "Subtotal" when casting from a Transaction to an Invoice.

public abstract class TransactionBase
{
    // There are some shared properties here.
}

public class Transaction : TransactionBase
{
    public double Amount { get; set; }
}

public class Invoice : TransactionBase
{
    public double SubTotal { get; set; }
}

In short, you can't do this.

In long, you can emulate this by coding to interfaces!

public class Transaction
{
    public double Amount { get; set; }
}
public interface IInvoice
{
    public double? SubTotal { get; set; }
}
public class Invoice : Transaction, IInvoice
{
    public double? SubTotal
    {
        get
        {
            return Amount;
        }
        set
        {
            Amount = value ?? 0.0f;
        }
    }
}

The exact behavior you're looking for doesn't make sense syntactically. If Invoice inherits Transaction , then it is a kind of transaction, and the compiler requires it to inherit all of its properties.

The general behavior you're looking for is, I think, encapsulation, which can be accomplished with interfaces.

public interface IInvoice
{
    double? Amount { get; set; }
}

public interface ITransaction
{
    double? SubTotal { get; set; }
}

Require the consumers of your code to use these interfaces, and then the implementation details are hidden to them.


So now, the classes can behave the way you want. SubTotal will still be visible to the class (Invoice), but will be hidden to the interface (IInvoice).

public class Transaction : ITransaction
{
    public double? SubTotal { get; set; }
}

public class Invoice : IInvoice
{
    public double? Amount
    {
        get { return base.SubTotal; }
        set { base.SubTotal = value; }
    }
}

I would recommend using composition over inheritance in this instance. The main reason is that the base implementation of Transaction seems to possibly never be used in the way intended through inheritance. You can hide the transaction as a protected or private member of the Invoice and expose / manipulate it using the public properties of Invoice.

One such example could look like:

public class Invoice
{
        private readonly Transaction _transaction; 
        public Invoice():this(new Transaction())
        {
        }
        public Invoice(Transaction transaction)
        {
            _transaction = transaction;
        }

        // This property should hide the generic property "Amount" on Transaction
        public double? SubTotal
        {
            get
            {
                return _transaction.Amount;
            }
            set
            {
                _transaction.Amount = value ?? 0.0f;
            }
        }

        public double RemainingBalance { get; set; }
    }

How about using an implicit conversion operator?

class Program
{
    static void Main(string[] args)
    {
        var transaction = new Transaction();
        var transactionAmount = transaction.Amount;

        var invoice = new Invoice();
        var invoiceSubTotal = invoice.SubTotal;

        Transaction fromInvoiceToTrans = invoice;
        var fromInvoiceToTransAmount = fromInvoiceToTrans.Amount;

    }
}

public class Transaction
{
    public decimal Amount {get; set;}
}

public class Invoice
{
    public decimal SubTotal
    {
        get;
        set;
    }

    public static implicit operator Transaction(Invoice invoice)
    {
        return new Transaction
        {
            Amount = invoice.SubTotal
        };
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM