Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在unity中使用接口_C#_Unity3d - Fatal编程技术网

C# 如何在unity中使用接口

C# 如何在unity中使用接口,c#,unity3d,C#,Unity3d,我制作了一个如下所示的界面: public interface Weapon { void Shoot(); float damage { get; set; } } float Weapon.damage { get { return damage; } set { throw new NotImplementedException(

我制作了一个如下所示的界面:

public interface Weapon
{
    void Shoot();
    float damage { get; set; }
}
   float Weapon.damage
    {
        get
        {
            return damage;
        }

        set
        {
            throw new NotImplementedException();
        }
    }
我在另一个类中实现它,对于损坏字段,我有如下内容:

public interface Weapon
{
    void Shoot();
    float damage { get; set; }
}
   float Weapon.damage
    {
        get
        {
            return damage;
        }

        set
        {
            throw new NotImplementedException();
        }
    }
但是我怎样才能用吸气剂造成武器伤害呢?我试过这样的方法:武器。伤害。获取(),但不起作用


任何帮助都将不胜感激,谢谢

我同意代码学徒-您应该看看类和接口基础的其他示例。对于您的特定项目,您可以执行以下操作。我还没有试着编译它,但它会让你接近它

public interface IWeapon
{
    void Shoot();
    float Damage { get; }
}

public class Sword : IWeapon
{
    public void Shoot() { } //does nothing
    private float _damage { get; set; }
    public float Damage { get { return _damage; } }
    public Sword(int damage)
    {
        _damage = damage;
    }
}

我建议您学习一些C#的基础知识,特别是关于类和接口的知识。在Unity中使用具有属性的接口可能会适得其反,因为您经常希望通过inspector调整变量的值。Unity默认只序列化字段,而不是属性。不道德我认为你忘记了S.O.L.I.D。原则。Unity可能有一些使调试变得困难的细节,但您不应该让这些细节影响您的体系结构。也许您需要一个.Unity对象的名称空间层,可以轻松地序列化
public-float-Damage{get;private-set;}