通用实现compareto()方法可访问性 [英] Generic implementation compareto() method accessibility

查看:51
本文介绍了通用实现compareto()方法可访问性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实施通用代码来处理储蓄和签入银行帐户。此代码应按以下条件运行。



1)常用Add()和Withdraw()方法用于储蓄和签入帐户。

2)通用IComparable< T>根据余额比较任意两个账户的实现。

3)不应该有任何硬编码类型的演员。



  interface  IBankAccount 
{
decimal 余额{获得; set ; }
string AccountType { get ; set ; }
bool 添加(十进制金额);
bool 提取(十进制金额);
}

abstract class BankAccount< T> :IBankAccount,IComparable< T> 其中 T:IBankAccount
{
public 十进制余额{获取; set ; } = 0 ;

public string AccountType {获得; set ; } = string .Empty;

public bool 添加(十进制金额)
{
余额+​​ =金额;
return true ;
}

public bool 提取( decimal amount)
{
if ((余额 - 金额)< 0
{
return ;
}

余额 - =金额;
return true ;
}

public int CompareTo(T other)
{
if this .Balance == other.Balance)
{
return 1 ;
}
其他
{
返回 0 ;
}
}
}

class SavingsAccount:BankAccount< SavingsAccount>
{
public SavingsAccount()
{
this .Balance = 500 ;
this .AccountType = Savings< /跨度>;
}
}

class CheckInAccount:BankAccount< CheckInAccount>
{
public CheckInAccount()
{
this .Balance = 1000 ;
this .AccountType = CheckIn< /跨度>;
}
}

class 客户
{
public IList< IBankAccount> accounts = new 列表< IBankAccount>();
public void CreateAccount< T>()其中 T:BankAccount< T>, new ()
{
T account = new T();
accounts.Add(account);
}
}

class 计划
{
static void Main( string [] args)
{
客户customer1 = 客户();
customer1.CreateAccount< SavingsAccount>();
customer1.CreateAccount< CheckInAccount>();

客户customer2 = 客户();
customer2.CreateAccount< CheckInAccount>();

客户customer3 = 客户();
customer3.CreateAccount< SavingsAccount>();
customer3.CreateAccount< CheckInAccount>();

customer3.accounts [ 0 ]。添加( 500 );

// 如何比较customer3.accounts [0]和customer1.accounts [1因为我无法访问CompareTo()方法,所以是相等的?

Console.Read();
}
}





请建议一种访问CompareTo()方法的方法?我很好用IBankAccount接口继承IComparable< T>如下所示,如果这是简单的解决方案。



  interface  IBankAccount< ; T> :IComprable< T> 
{
}





我尝试过:



我试过继承IComparable接口的IBankAccount接口以及问题中提到的其他几个东西。但在所有情况下,我最终都使用硬编码类型转换或无法访问预期的方法。

解决方案

您的界面应该是

< pre lang =cs> interface IBankAccount< T> :IComparable< IBankAccount< T>>
{}

abstract class BankAccount< T> :IBankAccount< T>
{
public int 比较(IBankAccount< T>其他)
{
return Balance.CompareTo(other.Balance);
}
}



但是,我不确定你需要一个通用的。继承就足够了。


你抽出的代码太多了 - 呵呵?



不开玩笑,如果你写代码就好这个,你应该对OOP概念和实现非常坚定 - 只是因为一些好的做法并不意味着你必须使用它...



所以让我们看看你在这里有什么:你的客户的IBankAccounts列表 - 这个接口有一个比较方法 - 没有。所以你无法访问它 - 逻辑

你想要什么:BankAccount< t>列表客户上具有通用比较实现的实例。一切都很好。

但也许您打算添加其他BankAccount< t>实现(不太可能,与此处的目的相矛盾),所以像phil.o上面展示的那样 - 让IBankAccount< t>的每个实现者都成为还实现IComparable< ibankaccount< t>>。正如他所说,我认为这里也没有需要仿制药,只是正常的继承,虽然我不知道你打算用这个做什么(所以也许你有其他好的理由)



这有助于现在修复您的代码吗?



亲切的问候Johannes


I am trying to implement generic code to handle savings and checkin bank accounts. This code should work as per below conditions.

1) Common Add() and Withdraw() methods for savings and checkin accounts.
2) Common IComparable<T> implementation for comparing any two accounts based on balance.
3) There shouldn't be any hard-coded type casting.

interface IBankAccount
        {
            decimal Balance { get; set; }
            string AccountType { get; set; }
            bool Add(decimal amount);
            bool Withdraw(decimal amount);        
        }
    
        abstract class BankAccount<T> : IBankAccount, IComparable<T> where T : IBankAccount
        {
            public decimal Balance { get; set; } = 0;
    
            public string AccountType { get; set; } = string.Empty;
    
            public bool Add(decimal amount)
            {
                Balance += amount;
                return true;
            }
    
            public bool Withdraw(decimal amount)
            {
                if ((Balance - amount) < 0)
                {
                    return false;
                }
    
                Balance -= amount;
                return true;
            }       
    
            public int CompareTo(T other)
            {
                if (this.Balance == other.Balance)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
        }
    
        class SavingsAccount : BankAccount<SavingsAccount>
        {
            public SavingsAccount()
            {
                this.Balance = 500;
                this.AccountType = "Savings";
            }
        }
    
        class CheckInAccount : BankAccount<CheckInAccount>
        {
            public CheckInAccount()
            {
                this.Balance = 1000;
                this.AccountType = "CheckIn";
            }
        }  
        
        class Customer 
        {
            public IList<IBankAccount> accounts = new List<IBankAccount>();
            public void CreateAccount<T>() where T : BankAccount<T>, new()
            {            
                T account = new T();
                accounts.Add(account);           
            }      
        }
    
     class Program
        {
            static void Main(string[] args)
            {
                Customer customer1 = new Customer();
                customer1.CreateAccount<SavingsAccount>();
                customer1.CreateAccount<CheckInAccount>();
    
                Customer customer2 = new Customer();
                customer2.CreateAccount<CheckInAccount>();
    
                Customer customer3 = new Customer();
                customer3.CreateAccount<SavingsAccount>();
                customer3.CreateAccount<CheckInAccount>();
    
                customer3.accounts[0].Add(500);
    
                // How can I compare customer3.accounts[0] and customer1.accounts[1] equality as I can't access CompareTo() method?
    
                Console.Read();
            }
        }



Please suggest a way to access CompareTo() method? I am fine with IBankAccount interface inheriting IComparable<T> like below as well if that is easy solution.

interface IBankAccount<T> : IComprable<T>
    {
    }



What I have tried:

I tried IBankAccount interface inheriting IComparable interface and couple of other things as mentioned in the question. But in all cases either I ended up with hardcoded type casting or not able to access expected methods.

解决方案

Your interface should be

interface IBankAccount<T> : IComparable<IBankAccount<T>>
{ }

abstract class BankAccount<T> : IBankAccount<T>
{
   public int Compare(IBankAccount<T> other)
   {
      return Balance.CompareTo(other.Balance);
   }
}


But, I'm not sure you need a generic here. Inheritance is sufficient.


have you abstracted your code too much- hehe?

No jokes aside, if you write Code like this, you should be quite firm with OOP concepts and implementations - just because something is good practice doesn't mean you have to use it...

So let's see what you have here: a list of IBankAccounts on your customers - do this Interface have a compare method - no. so you can't access it - logical
What you want: a list of BankAccount<t> instances on your customers which have a your generic compare implementation. All good.
But maybe you plan to add other BankAccount<t> implementations (unlikely, contradicts the purpose here) so do like phil.o showed you above - make every implementer of IBankAccount<t> also implement IComparable<ibankaccount<t>>. As he said, I don't see any need for generics here too, just normal inheritance, though I don't know what you plan to do with this (so maybe you have other good reasons)

Does this help to fix your code now?

Kind regards Johannes


这篇关于通用实现compareto()方法可访问性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆