为什么我得到了一个“;..由于其保护级别“不可访问”;这个C#代码中的编译错误

本文关键字:不可访问 访问 这个 编译 错误 代码 保护 于其 为什么 一个 | 更新日期: 2023-09-27 18:20:26

正如您所看到的,所有内容都设置为公共,但编译器说:

Shooting.inventoryWeapon.inventoryWeapon(Shooting.weapon, int)由于其保护级别而无法访问。

这个代码在课堂上射击。

public enum weapon{gun,shotgun};
public struct inventoryWeapon{
    weapon current;
    int shotAmmo;
    inventoryWeapon(weapon cur,int shAmmo){
        current=cur;
        shotAmmo=shAmmo;
    }
}
public inventoryWeapon[] Inventory;
int weaponIndex=0;
void Start(){
    Inventory=new inventoryWeapon[10];
    Inventory[weaponIndex]= new inventoryWeapon(weapon.shotgun,30);
}

为什么我得到了一个“;..由于其保护级别“不可访问”;这个C#代码中的编译错误

您需要在此处添加public

public inventoryWeapon(weapon cur,int shAmmo){
    current=cur;
    shotAmmo=shAmmo;
}

即使您已经将类定义为公共

public struct inventoryWeapon{

若你们想向外界公开你们的类成员,你们必须将你们的类的成员定义为公共的(除非你们使用了固有的)。

public inventoryWeapon(weapon cur,int shAmmo){