Реализуйте интерфейс с общими методами

Следующее сообщение в блоге может быть полезно для Вас: Фиксация окна изменяет размеры события в IE

, Это предоставляет этот код:

Sys.Application.add_load(function(sender, args) {
    $addHandler(window, 'resize', window_resize);
});

var resizeTimeoutId;

function window_resize(e) {
     window.clearTimeout(resizeTimeoutId);
     resizeTimeoutId = window.setTimeout('doResizeCode();', 10);
}

56
задан jamesaharvey 28 August 2009 в 02:18
поделиться

3 ответа

You should rework your interface, like so:

public interface IOurTemplate<T, U>
        where T : class
        where U : class
{
    IEnumerable<T> List();
    T Get(U id);
}

Then, you can implement it as a generic class:

public class OurClass<T,U> : IOurTemplate<T,U>
        where T : class
        where U : class
{
    IEnumerable<T> List()
    {
        yield return default(T); // put implementation here
    }

    T Get(U id)
    {

        return default(T); // put implementation here
    }
}

Or, you can implement it concretely:

public class OurClass : IOurTemplate<string,MyClass>
{
    IEnumerable<string> List()
    {
        yield return "Some String"; // put implementation here
    }

    string Get(MyClass id)
    {

        return id.Name; // put implementation here
    }
}
97
ответ дан 26 November 2019 в 17:19
поделиться

I think you probably want to redefine your interface like this:

public interface IOurTemplate<T, U>
    where T : class
    where U : class
{
    IEnumerable<T> List();
    T Get(U id);
}

I think you want the methods to use (re-use) the generic parameters of the generic interface in which they're declared; and that you probably don't want to make them generic methods with their own (distinct from the interface's) generic parameters.

Given the interface as I redefined it, you can define a class like this:

class Foo : IOurTemplate<Bar, Baz>
{
    public IEnumerable<Bar> List() { ... etc... }
    public Bar Get(Baz id) { ... etc... }
}

Or define a generic class like this:

class Foo<T, U> : IOurTemplate<T, U>
    where T : class
    where U : class
{
    public IEnumerable<T> List() { ... etc... }
    public T Get(U id) { ... etc... }
}
11
ответ дан 26 November 2019 в 17:19
поделиться

-- Edit

The other answers are better, but note you can have VS implement the interface for you, if you are confused as to how it should look.

Process described below.

Well, Visual Studio tells me it should look like this:

class X : IOurTemplate<string, string>
{
    #region IOurTemplate<string,string> Members

    IEnumerable<T> IOurTemplate<string, string>.List<T>()
    {
        throw new NotImplementedException();
    }

    T IOurTemplate<string, string>.Get<T, U>(U id)
    {
        throw new NotImplementedException();
    }

    #endregion
}

Note that all I did was write interface, then click on it, and wait for the little icon to popup to have VS generate the implementation for me :)

2
ответ дан 26 November 2019 в 17:19
поделиться
Другие вопросы по тегам:

Похожие вопросы: