Kotlin 多类型的泛型约束
在Kotlin中,我可以创建类型为Kotlin 多类型的泛型约束,kotlin,generics,Kotlin,Generics,在Kotlin中,我可以创建类型为T的泛型约束;例如: interface Foo { val a: String } class Baz<T : Foo>(x: T) { init { println(x.a) } } interface Foo { a: string; } interface Bar { b: number; } class Baz<T extends Foo & Bar> { constructor(
T
的泛型约束;例如:
interface Foo {
val a: String
}
class Baz<T : Foo>(x: T) {
init {
println(x.a)
}
}
interface Foo {
a: string;
}
interface Bar {
b: number;
}
class Baz<T extends Foo & Bar> {
constructor(x: T) {
console.log(x.a);
console.log(x.b);
}
}
public interface IFoo
{
string A { get; }
}
public interface IBar
{
string B { get; }
}
public class Baz<T> where T : IFoo, IBar
{
public Baz(T x)
{
Console.WriteLine(x.A);
Console.WriteLine(x.B);
}
}
C#中的等价物是使用IFoo
和IBar
的通用约束;例如:
interface Foo {
val a: String
}
class Baz<T : Foo>(x: T) {
init {
println(x.a)
}
}
interface Foo {
a: string;
}
interface Bar {
b: number;
}
class Baz<T extends Foo & Bar> {
constructor(x: T) {
console.log(x.a);
console.log(x.b);
}
}
public interface IFoo
{
string A { get; }
}
public interface IBar
{
string B { get; }
}
public class Baz<T> where T : IFoo, IBar
{
public Baz(T x)
{
Console.WriteLine(x.A);
Console.WriteLine(x.B);
}
}
公共接口IFoo
{
字符串A{get;}
}
公共接口IBar
{
字符串B{get;}
}
公共类Baz,其中T:IFoo,IBar
{
公共市集(T x)
{
控制台写入线(x.A);
控制台写入线(x.B);
}
}
Kotlin是否支持与此等效的功能?类Baz(x:T),其中T:Foo,T:Bar{
class Baz<T>(x: T) where T : Foo, T : Bar {
init {
println(x.a)
println(x.b)
}
}
初始化{
println(x.a)
println(x.b)
}
}
Baz(x:T)类,其中T:Foo,T:Bar{
初始化{
println(x.a)
println(x.b)
}
}
哇……好吧,我没想到会这样!哇…好吧,我没想到!除息的