gpt4 book ai didi

C# 模仿 Java 泛型?

转载 作者:行者123 更新时间:2023-11-30 19:56:31 29 4
gpt4 key购买 nike

在 Java 中这是有效的:

class Class1<T extends OtherType> { T t; ... }

//Inside other class that has no relation to Class1
private Class1 class1; //No type needed

public void someOtherFunction (Class1 class1) //Works
{
this.class1 = class1; //May warn about invalid type casting
class1.someFunction();
}

C# 要求在相同条件下的类型:

class Class1<T> where T : OtherType { T t; ... }

//Inside other class that has no relation to Class1
private Class1<TYPE DEMANDED> class1; //I don't know generic what type, and honestly it doesn't matter.

public void someOtherFunction (Class1 class1) //This is the definition I want to use, but C# Demands a type to class1
public void someOtherFunction<T> (Class1<T> class1) where T : OtherType //C# Demands a type to class1 so I must specify a T
{
this.class1 = class1;
class1.someFunction();
} //This function is valid but then it still needs the type to store it.

有什么办法可以省略类型吗?没有必要知道类型,那么为什么需要它呢?我无法制作 OtherType 类型的 Class1,因为泛型的要点是有一个扩展 OtherType 基类的未知类型。我可以解决它,但这绝对是最有效的解决方案,如果它是 Java,如果这不起作用,我将不得不为多个对象输入一次框架,我担心这会很快加起来。

实际代码,每个请求:

public abstract class Weapon { ... }

public abstract class WeaponProxy<T> : MonoBehaviour where T : Weapon
{
protected T weapon;

public virtual void Update()
{
...
holdingPlayer = player.getHUD().showPickup(player, this);
...
}

public abstract class GunProxy<T> : WeaponProxy<T> where T : Gun
{

}



public abstract class Weapon
{
private string weaponName;
private string weaponIdentifier;
private Player isHolding;

public string getWeaponName () { return weaponName; }
public Weapon(string weaponName, string weaponIdentifier)
{
this.weaponName = weaponName;
this.weaponIdentifier = weaponIdentifier;
}

public void playerPickedUp (Player player)
{
this.isHolding = player;
}

public void playerDropped ()
{
this.isHolding = null;
}
}

public class Gun : Weapon
{
...
}

public class HUD : MonoBehaviour
{
private WeaponProxy weapon; //C# Needs a type. Underlined in red as error

public PlayerProxy showPickup<T> (PlayerProxy player, WeaponProxy<T> weapon) where T : Weapon
{
this.weapon = weapon;
textPickupWeapon.text = "Hold '" + player.getPlayer().getControlScheme().getControlText(ControlScheme.CONTROLS.Interact) + "' to pick up " + weapon.getWeapon().getWeaponName();
...
}
}

最佳答案

您的 java 代码是“有效的”,因为 java 中的所有泛型实际上都是非泛型,因为 java 的泛型只是一个编译时技巧,没有任何运行时支持。

对于 java 运行时,键入 A<T>实际上是 A没有类型参数,因为 Java 运行时实际上根本不支持泛型。

相比之下,.NET CLR 内置了对运行时泛型类型的支持,因此它区分类型 A和一个通用类型 A<T> .

在 C# 中,如果您想要类型的非泛型版本 Class1 ,简单地声明它:

class Class1
{
//Whatever members that don't require a type parameter,

void SomeFunction() { /* ... */ } // Please use proper casing.
}

然后,如果您需要此类的通用版本:

class Class1<T>: Class1
{
T Content { get; set; }
}

现在您将能够拥有类型为 Class1 的成员在任何其他类(class)

class Example
{
Class1 instance; // Valid

public void someOtherFunction (Class1 class1) //Works
{
this.instance = class1; //Does not warn about anything because this is type safe.
class1.SomeFunction(); // Works

var content = class1.Content // Compile Error: type class1 does not have such member "Content"
}
}

请注意 C# 方法如何更安全,如果您使用的是非泛型版本,则只能访问非泛型版本中定义的类成员,不需要类型参数。相比之下,java 是完全不安全的,并且由于泛型中缺乏真正的类型安全性,可能会产生可怕的运行时错误。

关于C# 模仿 Java 泛型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33662581/

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