简体   繁体   English

不同子类的相同方法

[英]Same Method for different Subclasses

I have a C# -Class Point with two Subclasses ColorPoint and AmountPoint . 我有一个带有两个子ColorPoint AmountPointAmountPointC#Point

Point-Class 点级

public class Point
{
    double x; // Position x
    double y; // Position y

    public Point(double pos_x, double pos_y) // Constructor
    {
        this.x = pos_x;
        this.y = pos_y;
    }
}

public class ColorPoint : Point
{
    double color; // White value (0 to 255)
}

public class AmountPoint : Point
{
    int amount; // Amount of Persons standing at this point
}

Now I want two things. 现在我想要两件事。

  1. I want to have a method AdaptMeshPoints that accepts both ColorPoint and AmountPoint input-lists and I can change the common parameters of both (which are the parameters in Point ) 我想有一个方法AdaptMeshPoints接受两个ColorPointAmountPoint输入列表和本人可以改变两者的共同参数(这是在参数Point

  2. I want to tell the method AdaptMeshPoints , which parameter of the Subclass it should print out. 我想告诉方法AdaptMeshPoints ,应该打印出子类的哪个参数。

This should than look something like this: 这应该看起来像这样:

public class main
{
    public main()
    {
        List<ColorPoint> colorList = new List<ColorPoint>(4);
        AdaptMeshPoints<ColorPoint>(colorList, color);
    }

    public List<var> AdaptMeshPoints<var>(List<var> pointList, varType whatToPrint)
    {
        pointList[0].x = 45;
        Console.WriteLine(pointList[0].whatToPrint);
    }
}

I'm assuming this is C# from the text in your question, even though your question is tagged with both C# and Java. 我假设您问题中的文本是C#,即使您的问题同时被C#和Java标记了。

To be able to set pointList[0].x , you need to tell the compiler that T will always be a Point (or something which inherits from it). 为了能够设置pointList[0].x ,您需要告诉编译器T始终是Point (或从Point继承的东西)。 Do this with a generic type constraint ( where T : Point ). 使用泛型类型约束( where T : Point )执行此操作。

You can also pass a delegate which describes which property you want to print: 您还可以传递一个委托,该委托描述您要打印的属性:

public main()
{
    List<ColorPoint> colorList = new List<ColorPoint>(4);
    AdaptMeshPoints(colorList, x => x.color.ToString());
}

public List<T> AdaptMeshPoints<T>(List<T> pointList, Func<T, string> whatToPrint)
    where T : Point
{
    pointList[0].x = 45;
    Console.WriteLine(whatToPrint(pointList[0]));
}

To 1. Make a function that takes Point[]. 要1.创建一个采用Point []的函数。 Casting is automatically done if there is "no danger of Data loss" and such polymorphy cases are where this applies. 如果“没有数据丢失的危险”,并且这种情况适用于多态情况,则自动进行转换。

(I am not 100% certain if this is Polymorphy or falls more into the area of co- and Contra-variance. However the rules here are intentionally very similar). (我不确定这是多态的还是属于协方差和反方差领域。100%不确定。但是这里的规则有意地非常相似)。

To 2. This can be solved at least by: 到2。这至少可以通过以下方法解决:

  • A extra parameter and a switch/case block 一个额外的参数和一个开关/案例块
  • Handing the function anotehr function or interface that holds the actuall code to access the variable 处理保存实际代码的函数anotehr函数或接口以访问变量
  • Hand the code a Lambda or anonymous function to do the actuall work. 将代码交给Lambda或匿名函数来完成实际工作。

So you got to "pick your Posion". 因此,您必须“选择您的位置”。

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

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