简体   繁体   English

对类中的不同变量运行相同的方法

[英]Running the same method for different variables in a class

I have a class with about 10 or more different boolean values that record whether a user has done a particular action that will give them a specific reward (eg send a message to someone). 我有一类具有大约10个或更多个不同的布尔值的类,这些值记录用户是否执行了特定的操作,这些操作将给予他们特定的奖励(例如,向某人发送消息)。

here is the code for one method for ONE bool/action: 这是一种bool / action的一种方法的代码:

private ReqRewardResult setMsgSent(RewardClass reward, RewardInfo info)
{
    if (reward.msgSent)
        return ReqRewardResult.RewardAlreadyGiven;

    reward.msgSent = true;
    reward.earned += info.msgSentReward;
    return ReqRewardResult.ReqSuccess;
}

I have tried to create a generic method for this but it seems you can't pass a class variable as a reference? 我试图为此创建一个通用方法,但似乎您不能将类变量作为引用传递?

private ReqRewardResult setRewardAction(ref bool bAction, RewardClass reward, int reward)
{
    if (bAction)
        return ReqRewardResult.RewardAlreadyGiven;

    bAction = true;
    reward.earnedTokens += reward;
    return ReqRewardResult.ReqSuccess;
}

I have then looked at a couple of methods such as using a delegate function... but this is then kinda pointless as i'd have to repeat several lines again... 然后,我研究了几种方法,例如使用委托函数...但是这有点没有意义,因为我不得不再次重复几行...

I have also seen you could use Reflection... but this is really slow and as this is a web app i'd rather use more repeated code if it improves the overall speed... 我也看到过您可以使用Reflection ...,但这确实很慢,并且因为这是一个Web应用程序,所以我希望使用更多重复的代码,以提高整体速度...

The question: Is there anyway to have a class function that can repeat for several variables of the same type without any performance hit? 问题:总有没有一个类函数可以重复多个相同类型的变量而不会影响性能?

NOTE: This class is data that is loaded from a database and is unique to each user (there could be millions of users) 注意:此类是从数据库加载的数据,并且对于每个用户都是唯一的(可能有数百万个用户)

Many Thanks, 非常感谢,

Phil. 菲尔

class RewardCredit
{
   public bool Rewarded { get; set; }

   public int Points { get; set; }
}

You could use a Dictionary<string, RewardCredit> , fill it with reward names as strings and have a function like 您可以使用Dictionary<string, RewardCredit> ,将奖励名称作为字符串填充,并具有如下功能

void ApplyReward(string rewardName)
{
    if (!rewards.ContainsKey(rewardName))
    {
        return;
    }

    RewardCredit credit = rewards[rewardName];
    if (!credit.Rewarded)
    {
        tokens += credit.Points;
        credit.Rewarded = true;
    }
}

You'd then end up using it like so 然后您将最终像这样使用它

Dictionary<string, RewardCredit> rewards = new Dictionary<string, RewardCredit>
{
    { "Message", new RewardCredit { Points = 10 } },

};

ApplyReward("Message");

First it looks like you have a finish and known set of possible user actions that can be rewarded with "tokens" if done by a user. 首先,您似乎已经完成并知道了一组可能的用户操作,如果用户执行了这些操作,则可以使用“令牌”进行奖励。 This can be implemented with the following class: 可以使用以下类来实现:

public sealed class UserAction
{
    public static UserAction SendMessage = new UserAction(3);
    public static UserAction PhonedFriend = new UserAction(4);
    public static UserAction LikedPost = new UserAction(1);
    public static UserAction CommentedOnPost = new UserAction(2);
    private UserAction(int tokens) => Tokens = tokens;
    public int Tokens { get; }
}

Notes: 笔记:

  • The constructor is made private and only the few instances of this class that must exist are available with predefined names and tokens 构造函数设为私有,并且只有预定义的名称和标记可提供该类中必须存在的少数几个实例
  • The class is sealed to avoid being inherited, so that it effectively is impossible to create other instances than the one you provided 该类经过密封,以避免被继承,因此实际上无法创建除您提供的实例之外的其他实例。

Then, so far I understand, each user may have done each action (or not) but must be rewarded no more than once for each. 到目前为止,据我所知,每个用户可能已执行(或未执行)每个操作,但每个用户所获得的奖励不得超过一次。 This make me think that you need to store a set of these UserAction s into a User . 这使我认为您需要将一组这些UserAction存储到User You also need a way to add an action to a user (when he effectively does in on the UI, for example) and also be able to know how much tokens each user has earned. 您还需要一种向用户添加操作的方法(例如,当用户有效地在UI上操作时),并且还需要知道每个用户获得了多少令牌。 The following class implements such behavior: 下面的类实现了这种行为:

public sealed class User
{
    private readonly HashSet<UserAction> _doneActions;
    public User() => _doneActions = new HashSet<UserAction>();
    public ReqRewardResult AddAction(UserAction action) => _doneActions.Add(action) ? ReqRewardResult.ReqSuccess : ReqRewardResult.RewardAlreadyGiven;
    public int EarnedTokens => _doneActions.Sum(ua => ua.Tokens);
}

Note: 注意:

  • I'm taking advantage of the HashSet.Add() 's return value to determine if the action had already been done in the past or not 我利用HashSet.Add()的返回值来确定该操作是否在过去已经完成

Advantages of this "object-thinky" solution: 这种“对象思维”解决方案的优点:

  • No more booleans and if , which I believe makes your code more maintainable and clear 没有更多的布尔值和if ,我相信这会使您的代码更易于维护和清晰
  • Simplicity to add a new UserAction (one LoC) 简单添加新的UserAction (一个LoC)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的 class 中似乎有 2 个同名的不同变量 - There seems to be 2 different variables with the same name in my class 具有相同名称的类和方法变量的行为 - Behavior with Class and Method Variables with the same names 使用不同的变量同时运行多个相同的线程 - Running multiple of same thread at same time with different variables 将相同基类的不同对象传递给方法 - passing different objects of same base class to method 基类的相同方法的不同返回类型 - Different return type for same method of a base class 从使用该方法外的变量的其他类中调用方法 - Calling a method from a different class that uses variables outside the method 我们可以注入具有不同变量的相同 class 的多个实例吗? - Can we inject multiple instances of the same class with different variables? 在同一 class -c# 中使用来自另一个方法的变量 - Using a variables from another method in the same class -c# 在 C# 中使用不同的参数多次运行相同的方法 - Running the same method multiple times with different parameters in c# C#(Unity3d)中同一Monobehaviour类中不同类的相同类变量的不同集合 - Different sets of the same class variables within the same Monobehaviour class in c# (Unity3d)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM