流/无限流接口定义

时间:2018-03-20 01:44:07

标签: c# class

我想做的是以下内容:

public interface IRemove<out T>
{
    T Remove();
    bool IsCompleted { get; }
}

public interface IRemoveInfinite<out T> : IRemove<T>
{
    bool IsCompleted { get { return false; } } 
}

但这是无效的,因为我无法在C#接口中实现。

考虑到以下限制:

  1. 每个IRemoveInfinite也自动IRemove
  2. 当被视为IRemoveInfinite时,确保无法成功编译IsCompleted IRemove曾设置为false的文件。
  3. 允许在已有父级的类上实现这些接口。
  4. 我希望能够在编译时强制接收无限流,如果某些函数无法处理完成它们,则可能需要无限流。
  5. 我如何编写这样的接口或以其他方式封装它?

1 个答案:

答案 0 :(得分:2)

接口是合同声明,但您尝试将此合同的实现插入其中。

也许您应该使用抽象类来定义行为:

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>Client App | InLine</title>

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.7/angular.js"></script>
  <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
  <script src="script.js"></script>
</head>

<body  ng-app="inLineClient">
  <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
    <a class="navbar-brand" href="#">InLine</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarText" aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarText">
      <span class="navbar-text">
          Welcome Back, User
        </span>
    </div>
  </nav>
  <div class="container">
    <div class="row">
      <h1 class="display-3">Current Conference: Conference</h1>
    </div>
    <div ng-controller="lineController">

      <div class="row" ng-repeat="line in linesList track by $index" ng-if="$index % 3 == 0">
        <div class="col-md-4" ng-repeat="i in [$index, $index + 1, $index + 2]" ng-if="linesList[i] != null">
          <div class="card">
            <div class="card-header">
              {{line.name}}
            </div>
            <div class="card-body">
              <p class="card-text">{{line.description}}</p>
              <ul class="list-group list-group-flush">
                <li class="list-group-item">Time: {{line.time}}</li>
                <li class="list-group-item">Number of People: {{line.numPeople}}</li>
                <li class="list-group-item"><a href="#" class="card-link">Card link</a></li>
              </ul>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</body>

</html>

如果您不能使用继承,请使用类的组合:

public interface IRemove<out T>
{
    T Remove();

    bool IsCompleted { get; }
}

public abstract class RemoveInfinite<T> : IRemove<T>
{
    public bool IsCompleted
    {
        get { return false; }
    }

    public abstract T Remove();
}