如何将带有对象引用的功能传递给另一个要执行的功能

我想这是我尝试做的一件相当简单的事情,但我可能在这里遗漏了一些东西。

说我有几个函数,除了一行外,它们都做 ALMOST 相同的事情,这肯定需要一个“基本”函数或类似的东西,其中 ONE 另一行将传递给它并执行,其余代码将继续执行。

这是我的例子:

比方说,我有一些函数可以对Car对象执行操作并验证操作是否成功:

public void SoundHorn()
{
            var response = _car.Horn(1000);
            if(response.Status != 0)
            {
                throw new Exception("Operation failed!");
            }
}

public void TurnOnHazardLights()
{
            var response = _car.ToggleHazards(true);
            if(response.Status != 0)
            {
                throw new Exception("Operation failed!");
            }
}

您可以看到,唯一的区别是要执行的实际功能,所有功能的验证都是相同的。

我最终想要拥有的是“基本”功能:

private void PerformOperation(??? operation)
{
            var response = operation.Invoke();
            if(response.Status != 0)
            {
                throw new Exception("Operation failed!");
            }
}

其他功能如下:

public void TurnOnHazarLights()
    {
                PerformOperation(_car.togglehazards);
    }

我知道委托,动作和功能,似乎它们需要我传递函数才能运行自身,而没有对象引用,因此我无法使用以上语法。

我想我错过了上面提到的这些类之一的用法。

您能对此启发一下吗?

谢谢!

pypzx 回答:如何将带有对象引用的功能传递给另一个要执行的功能

代表确实是您所需要的。在这种情况下,由于所有操作都在function fillAnyForm(selectors,values) { if(!isArray(selectors)) throw 'Parameter "selectors" must be an array' if(!isArray(values)) throw 'Parameter "values" must be an array' nPromises = Math.min(selectors.length,values.length) promises = [] for(let i = 0; i < nPromises; i++) { promises.push(new Promise(/* This is the part I don't get */)) } return Promise.all(promises); } 对象上操作并返回内容,因此您需要一个Car,其中Func<Car,Something>Something的类型,而其他则返回:

Horn

您可以编写两个专门的操作,如下所示:

private void PerformOperation(Func<Car,Something> operation)
{
        var response = operation.Invoke(_car);
        if(response.Status != 0)
        {
            throw new Exception("Operation failed!");
        }
}
,

应该是

 private void PerformOperation(Func<int> operation)

您定义

 public void TurnOnHazarLights() => PerformOperation(()=>_car.ToggleHazards(true));
 public void SoundHorn() => PerformOperation(() => m_car.SoundHorn(100))

或将替换为您的Response类的类型。

,

也许我正在监督某些事情,但是为什么这样不起作用:

<div class="container holidayContent">
        <div class="row">
            <h1>Holidays</h1>
        </div>
        <div class="row">
            <?php
                $sql = "SELECT holiday.*,users.* FROM holiday INNER JOIN users ON holiday.user_id = users.user_id WHERE users.user_id = '" . $_SESSION['userId'] . "'";
                $result = $conn->query($sql);

                if ($result->num_rows > 0) {
                    // output data of each row 
                ?>

                <table class="table table-striped table-bordered table-hover" id="dataTables-example">
                    <thead>
                        <tr>
                            <th>Name</th>
                            <th>Start Date</th>
                            <th>End Date</th>
                            <th>Reason</th>
                            <th>Validated</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php while ($row = mysqli_fetch_array($result)) { ?>
                        <tr>
                            <td><?php echo $row['first_name'] . ' ' . $row['last_name']; ?></td>
                            <td><?php echo $row['start_date']; ?></td>
                            <td><?php echo $row['end_date']; ?></td>
                            <td><?php echo $row['reason']; ?></td>
                            <td><?php if ($row['validated'] == 0) {
                                echo '<a><i class="fas fa-check-circle"></i></a>';
                                echo '<a><i class="fas fa-times-circle"></i></a>';
                            } else if ($row['validated'] == 1) {
                                echo '<i class="fas fa-check-circle"></i>';
                            } else {
                                echo '<i class="fas fa-times-circle"></i>';
                            } ?></td>
                        </tr>
                    <?php } ?>
                    </tbody>
                </table>

                <?php } else {
                    echo "You have not asked for any days off.";
                }

                $conn->close();
                ?>
            </div>
        </div>

然后您将通过

进行调用
private void PerformOperation(Func<Response> operation)
{
            var response = operation();
            if(response.Status != 0)
            {
                throw new Exception("Operation failed!");
            }
}
本文链接:https://www.f2er.com/3116954.html

大家都在问