类扩展(player.STR到player.Stats.STR)

本文关键字:STR player Stats 扩展 | 更新日期: 2023-09-27 18:21:19

这是我的示例类Player

public class Player {
    public int STR;
    public int INT;
    public int DEX;
    public int VIT;
    public Player() {
        STR = 1;
        INT = 1;
        DEX = 1;
        VIT = 1;
    }
    public Player Stats {
        get {
            return STR;
            return INT;
            return DEX;
            return VIT;
        }
        set {
            STR = value;
            INT = value;
            DEX = value;
            VIT = value;
        }
    }
}
public Player player = new Player();

我通过调用:player.STR
来使用它我想通过调用:player.Stats.STR
来使用它它在代码中工作,但编译后我出现了以下错误:

Assets/PlayerAI.cs(62,33): error CS0029: Cannot implicitly convert type `int' to `PlayerAI.Player'

如何解决此问题?这个代码出了什么问题?

类扩展(player.STR到player.Stats.STR)

您需要返回一个包含这些统计信息的类。创建另一个名为Statistics的类。将属性添加到类Player,然后按照您显示的方式访问它。

public class Player
    {
        public class Statistics
        {
            public int STR;
            public int INT;
            public int DEX;
            public int VIT;
        }
        public Player()
        {
            this.Stats = new Player.Statistics()
            {
                STR = 1,
                INT = 1,
                DEX = 1,
                VIT = 1
            };
        }
        public Statistics Stats {get;set;}
    }