簡體   English   中英

C#模仿Java通用?

[英]C# mimic Java generic?

在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 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完全不安全,並且由於在泛型中缺乏真正的類型安全性而可能產生可怕的運行時錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM