- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我一直在研究接口(interface)和泛型。
我想要的是 Foo<T>
成为 Foo<T, int>
的简写这明智吗?以下是一种公认的做法吗?
using System.Diagnostics;
namespace MyNamespace
{
public class Foo<T> : Foo<T, int> where T : class
{
}
public class Foo<T, U> where T : class
{
public void Bar()
{
Debug.WriteLine("this: " + GetType().Name + ", T: " + typeof(T).Name + ", U: " + typeof(U).Name);
}
}
class Program
{
static void Main(string[] args)
{
var fooT = new Foo<string>();
var fooTU = new Foo<string, int>();
fooT.Bar();
fooTU.Bar();
}
}
}
我的实际问题是...是否可以从 Foo<T, int>
向上(向下?)转换到 Foo<T>
其中 T
是同一类型。这么说是否有意义,或者这与协变/逆变有关(我仍然无法理解)?
对我来说,一个有点无知的人,在这个例子中Foo<string>
和 Foo<string, int>
是相同的类型,但当然是 ((Foo<string>) fooTU).Bar();
不起作用!
最佳答案
What my actual question is... is it possible to up (down?) cast from a
Foo<T, int>
to aFoo<T>
where T is the same type. Does it even make sense to say this, or is this something to do with co/contravariance (which still eludes me)?
您可以从 Foo<string, int>
转换至 Foo<string>
如果实际对象的类型是Foo<T>
.它们具有相同名称的事实在这里完全无关紧要,所以让我们更改它:
class Foo<T, U> {}
class Bar<T> : Foo<T, int>
现在:
Foo<string, int> foo = new Foo<string, int>();
Bar<string> bar = (Bar<string>) foo; // Bang, throws
但是:
Foo<string, int> foo = new Bar<string>();
Bar<string> bar = (Bar<string>) foo; // This is fine
泛型在这里非常无关紧要,真的……这是正常的转换规则。
关于C# 通用速记和向上转型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37423343/
哪个更好?使用像这样的速记: padding:5px 10px 15px 20px; 或者像这样使用手写体: padding-top:5px; padding-right:10px; padding-
我目前在编辑 manuscript.tex 文件时遇到问题。到目前为止,我一直使用文本简写 $$;然而,出现了一些问题,它要求我将所有 $...$ 替换为 \(\)。 我认为带有替换操作的“sed”应
我想要一个简单的 if 速记来检查一个数组是否有一个特定的键,如果有则取消设置它。 $test = array("hi" => "123"); isset($test["hi"]) ? unset($
我在 F# 中映射记录列表并获取命名值: type Person = { FirstName: string; LastName: string } let people = [ { Firs
因此,我有一对类型类,我将经常一起使用它们,并且我想避免每次都指定它们。基本上,而不是把 :: (Ord a, Fractional a, Ord b, Fractional b, ... Ord z
有没有更优雅的写法? : var AllOperation = $('#menu > li.operation'); var Operation1= AllOperation[0]; $(Operat
基本上我想这样做: x ? console.log("true") : x=55 && console.log("changed!!") 如果x为false,它会将值更改为55和console.log
而不是在方法的开头声明一个列表,添加到它然后返回它 - 我确信有一些可以写在循环中的速记返回语句,例如,保存额外的代码(声明等),但我忘记了。有人知道我的意思吗? 最佳答案 使用 yield : pu
我发现自己经常写这样的东西而且看起来太罗嗦了: obj = my_dict.get('obj') if obj: var = obj 有更好的方法吗?也许在一行中? 最佳答案 get 函数接受
多年来我一直在使用 PHP 进行编程,我一直想知道是否有一种方法可以“预连接”一个字符串。示例: $path = '/lib/modules/something.php'; $server = $_S
在将值附加到数组时,是否有 JavaScript(甚至在 coffeescript 中).push() 的简写?很像 php 的 $array[] = 'added to array';。 最佳答案
我读了this tutorial关于在 CSS 选择器中使用正则表达式并试图推断:是否有 CSS 速记来执行以下操作?我想选择所有类为“foo”的 div,这些类有一个附加类“a”、“b”、“c”或“
如何将 rotateX(50deg) rotateY(20deg) rotateZ(15deg) 组合成简写 rotate3d()? 最佳答案 rotateX(50deg) 等价于rotate3d(1
如何将 rotateX(50deg) rotateY(20deg) rotateZ(15deg) 组合成简写 rotate3d()? 最佳答案 rotateX(50deg) 等价于rotate3d(1
我有一个像这样的简单对象(或数组)... stdClass Object ( [people] => Array ( [0] => stdClass Object ( [nam
我有一个变量,如果该变量是一个对象,我想在该对象上调用一个方法,如果不是,我什么也不想做。 我想知道是否有任何理由不应该这样做。 var foo = null; /////////////////
我想知道是否有任何简写方式可以在 JavaScript 中创建一个 promise,或者有什么方法可以将 .then 添加到普通函数中。示例: dbl = a => a | 0 ? a * 2 : !
是否有以下 JavaScript bool 三元表达式的简写语法: var foo = (expression) ? true : false 最佳答案 当然,您只想将表达式转换为 bool 值: v
这个问题在这里已经有了答案: One-liner to take some properties from object in ES 6 (13 个答案) How to get a subset o
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭8 年前。 Improve
我是一名优秀的程序员,十分优秀!