gpt4 book ai didi

c# - Java 到 C# : Extends in Generic

转载 作者:太空宇宙 更新时间:2023-11-03 11:38:06 25 4
gpt4 key购买 nike

我正在尝试将此 Java (Android) 代码转换为 C# (MonoDroid),但我不理解 <Item extends OverlayItem>

public class BalloonOverlayView<Item extends OverlayItem> extends FrameLayout

最佳答案

它正在为类型参数添加一个约束。它类似于 C# 中的 where 子句。

在 Java 中,您有:

public class BalloonOverlayView<Item extends OverlayItem> extends FrameLayout

其中 Item 是类型参数,必须子类化或实现类型 OverlayItem。在 C# 中,这将被写为:

public class BalloonOverlayView<Item> : FrameLayout where Item : OverlayItem

您可以看到约束是如何移动到末尾的,但其他方面类似。非常多common practice in C# to name type parameters prefixed with a T ,所以我会像这样推荐 TItem 这个名字:

public class BalloonOverlayView<TItem> : FrameLayout where TItem : OverlayItem

这有助于阐明类型参数和普通类型之间非常重要的区别。

关于何时使用类型约束的讨论,I go into this at length in a previous answer.

关于c# - Java 到 C# : Extends in Generic,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10778084/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com