合并get方法和set方法的方法?

问题描述

我想知道是否可以将我制作的 GetName 和 SetName 方法合二为一,以便程序可以检查是否可以应用名称,如果可以,则提示用户,并将名称分配给所需的对象。我正在测试我的代码,即使对象的级别设置为 1,SetName 方法也允许指定名称“Woody”,即使要求至少是我在 GetName 方法中声明的级别 3。

这是我使用的代码

using System;
using System.Collections;
using System.Collections.Generic;

namespace Practice

{

    class shield
    {
        private int level;
        private int durability;
        private string name;

      public shield(int level,int durability)
        {
            this.level = level;
            this.durability = durability;
        }

        public void block()
        {
            durability -= 5;
            Console.WriteLine("Chika's {0} blocked a hit and lost 2 durability,Now having {1}",name,durability);
        }
        public string GetName()
        {
            if (level >= 3)
            {
               return name;
            }
            else
            {
                return ("You must have a level 3 shield to give it a custom name!");
            }
            
        }
        public void SetName(string newname)
        {
            if ( newname != "")
            {
                name = newname;
                Console.WriteLine("The name of your shield is Now {0}",newname);
            }
            else
            {
                Console.WriteLine("Please enter a valid name");
            }

        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            shield s1 = new shield(1,100);
            s1.block();
            s1.SetName("Woody");
      
        }  
          


        }

     
        
    }

指出任何错误或其他本可以做得更好的事情也很有帮助。

解决方法

这是一个涉足的链接:

W3Schools

这是一个快速的重构建议(但绝对非常不完美): 基本上,您需要直接使用相关属性定义 getter-setter(prop 选项卡选项卡) c# 中还有一个大写字母的新类(约定) 并且我认为如果您编写特定的get-/set-functions,您需要附加一个私有字段,否则您可能会遇到stackoverflow 问题。但其他人,可能需要纠正这一点。同样从某种意义上说,也许这个问题会找到更好的归宿@Code Review Community?

namespace GetterSetter
{
    

class Program
    {
        static void Main(string[] args)
        {
            Shield s1 = new(1,100);
            s1.block();
            s1.Name = "Woody";

            
        }
    }
}


using System;

namespace GetterSetter
{
    public class Shield
    {
        public int Level { get; set; }
        public int Durability { get; set; }
        private string name;
        public string Name 
        { get {
                if (this.Level   >= 3)
                {
                    return name;
                }
                else
                {
                    return ("You must have a level 3 shield to GET a custom name!");
                }
            }
            set {
                if (value != "")
                {
                    name = value;
                    Console.WriteLine("The name of your shield is now {0}",name);
                }
                else
                {
                    Console.WriteLine("Please enter a valid name");
                }
            }
        }

    public Shield(int level,int durability)
    {
        Level = level;
        Durability = durability;
    }

    public void block()
    {
        this.Durability -= 5;
        Console.WriteLine("Chika's {0} blocked a hit and lost 2 durability,now having {1}",Name,Durability);
    }
}

}