简体   繁体   中英

Abstract method with generic type

Im trying to make that: 我的模特

I'm little confused. I want to make a function called Update(T other) , that the parameter "other" type is the class type. I think, with a generic interface implemented in abstract class it will work, but it does not work. :/

How can I get an abstract method with a generic type parameter, and specify that type in the inherited classes? Is that possible? Is my approach correct?

Code:

public interface IUpdateable<T>
{
    void Update(T pOther);
}

public abstract class Instruction_Template : IUpdateable<Instruction_Template>
{
    public abstract void Update(??? pOther);
}

public class Work_Instruction_Template : Instruction_Template
{
    public void Update(Work_Instruction_Template pOther)
    {
       //logic...
    }
}

Thanks!

Using curiously recurring template pattern .

abstract class Instruction_TP<T> 
    where T : Instruction_TP<T>
{
    public abstract void Update(T instruction);
}

class Process_Instruction_TP : Instruction_TP<Process_Instruction_TP>
{
    public override void Update(Process_Instruction_TP instruction)
    {
        throw new NotImplementedException();
    }
}

abstract class NC_Instruction_TP<T> : Instruction_TP<T>
    where T : NC_Instruction_TP<T>
{ }

class Drill_Instruction_TP : NC_Instruction_TP<Drill_Instruction_TP>
{
    public override void Update(Drill_Instruction_TP instruction)
    {
        throw new NotImplementedException();
    }
}

What is the problem?

public interface IstructionTP<T>
    where T : class
{
    void Update(T entity);
}

public class ProcessIstructionTP : IstructionTP<ProcessIstructionTP>
{
    public void Update(ProcessIstructionTP entity) 
    {
        ...
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM