[英]How to use interface in unity
I made an interface that looks like this: 我制作了一个界面,如下所示:
public interface Weapon
{
void Shoot();
float damage { get; set; }
}
And I implement it in another class and for damage field I have something like this: 我在另一个类中实现了它,对于损害字段,我有这样的东西:
float Weapon.damage
{
get
{
return damage;
}
set
{
throw new NotImplementedException();
}
}
But How can I use the getter for Weapon.damage ? 但是,如何将吸气剂用于Weapon.damage? I tried something like this: Weapon.damage.get() but it didn't work
我尝试过这样的事情:Weapon.damage.get(),但是没有用
Any help will be much appreciated, Thanks! 任何帮助将不胜感激,谢谢!
I agree with Code-Apprentice - you should take a look at other examples of fundamentals of classes & interfaces. 我同意代码学徒-您应该看看类和接口基础的其他示例。 For your specific item, you could do something like the following.
对于您的特定项目,您可以执行以下操作。 I haven't tried to compile it but it should get you close
我没有尝试编译它,但它应该使您接近
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.