首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对如何实现值和限制参数感到困惑

对如何实现值和限制参数感到困惑
EN

Stack Overflow用户
提问于 2020-03-02 04:42:14
回答 1查看 65关注 0票数 0

完成一些课堂作业,但我遇到了两个概念的问题。

下面是分配的任务:编写一个C# .NET框架控制台应用程序来演示C#中属性的用例。应用程序将执行以下操作以完成此目标。

  1. 日志在属性设置器中写入私有字段的每个实例。在程序退出之前,将活动日志输出到屏幕。设置每个学生的每个属性的值,以便于测试和操作此日志记录functionality.
  2. Choose至少一个属性给学生,并确保其设置器中包含除默认自动属性设置器代码之外的逻辑。这些要求不包括日志记录。参见第3章幻灯片/文本中的WatchDogAbility示例。历史上,系统允许学生限制查看他们的个人信息。在这里实现这个系统,方法是确保属性的getter在返回学生信息之前检查这个值。学生ID和名称不需要是checked.
  3. Submit,这是您在Visual中以调试模式停止程序的屏幕截图。设置“学生”对象的一个属性。在将新值写入私有字段的行上暂停。确保新值显示在图像中的屏幕上(参见下面的示例,使用Alt +打印屏幕)。输出所有学生的所有数据,比方说至少有10名学生,至少有一名学生的个人信息有限。

我不知道如何“在属性设置器中记录每个私有字段的写入实例”,也不知道如何才能满足这一要求,“历史上,系统允许学生限制查看个人信息。在这里实现这个系统,确保属性的获取者在返回学生信息之前检查这个值。学生的ID和名字不需要检查。”

下面是我到目前为止的代码:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assignment_3
{
    // Student Class
    public class Student
    {
        // Class Fields
        private int _graddate;
        public int _id, _total;
        public string _name, _major, _startdate, _state, _country, _email, _mailaddress;
        public long _phonenumber;

        // Name Propery Constructor - Read Only
        public int Id
        {
            get
            {
                return this._id;
            }
        }

        public string Name
        {
            get
            {
                return this._name;
            }
        }

        public int Total
        {
            get
            {
                return this._total;
            }
            set
            {
                this._total = value;
                Console.WriteLine($"Set Total to {value}");
            }
        }

        public int Graddate
        {
            get
            {
                return _graddate;
            }
            set
            {
                if (value < 2020)
                {
                    this._graddate = 0;
                }
                else
                {
                    this._graddate = value;
                }
            }
        }

        // Constructor
        public Student(int id, string name, string major, string startdate, int graddate, string state, string country, string email, long phonenumber, string mailaddress)
        {
            this._id = id;
            this._name = name;
            this._major = major;
            this._startdate = startdate;
            this._graddate = graddate;
            this._state = state;
            this._country = country;
            this._email = email;
            this._phonenumber = phonenumber;
            this._mailaddress = mailaddress;
        }

        public Student(int total)
        {
            this._total = total;
        }
    }

    // Main class
    public class MainClass
    {
        static void Main(string[] args)
        {
            Student perone = new Student(07211964, "Ryu", "Physical Education", "July 21, 1964", 2021, "Shimane", "Japan", "Hadoken@gmail.com", 81369207750, "285 Suzaku Castle");
            Student pertwo = new Student(02141965, "Ken", "Hospitality", "February 14, 1965", 2021, "California", "United States", "Shoryuken@gmail.com", 6503506500, "101 Battle Harbor");
            Student perthree = new Student(11031960, "E.Honda", "Culinary Arts", "November 03, 1960", 2016, "Higashi Komagata", "Japan", "KillerHeadRam@gmail.com", 810669203600, "462 Kapukon Yu");
            Student perfour = new Student(12231960, "Guile", "Aviation", "December 23, 1960", 2018, "Texas", "United States", "SonicBoom@gmail.com", 3109435470, "119 Air Force Base");
            Student perfive = new Student(03011968, "Chun-Li", "Criminology", "March 01, 1968", 2020, "North Point", "Hong Kong", "Hyakuretsukyaku@gmail.com", 85223661001, "911 Taiping Road");
            Student persix = new Student(02121966, "Blanka", "Electrical Engineering", "February 12, 1966", 2030, "Brasilia", "Brazil", "ShoutOfEarth@gmail.com", 4908001801976, "777 Amazon River Basin");
            Student perseven = new Student(06011956, "Zangief", "Russian", "June 01, 1956", 2013, "Moscow", "Russia", "RedCyclone@gmail.com", 4408005876638, "360 Big Factory");
            Student pereight = new Student(11221952, "Dhalsim", "Mental Health Services", "November 22, 1952", 2008, "New Delhi", "India", "YogaFire@gmail.com", 330800909617, "695 Marharaja's Palace");
            Student pernine = new Student(01271967, "Vega", "Psychology", "January 27, 1967", 2026, "Madrid", "Spain", "IzunaDrop@gmail.com", 34900811390, "187 Flamenco Tavern");
            Student perten = new Student(07021955, "Sagat", "Physical Education", "July 02, 1955", 2050, "Ayutthaya", "Thailand", "VanillaSagat@gmail.com", 39800928830, "311 King's Court");

            Console.WriteLine("Student ID: {0}, Student Name: {1}, Major: {2}, School Start Date: {3}, School Graduation Date: {4}, State/Territory: {5}, Country: {6}, E-Mail: {7}, Phone Number: {8}, Mail Address: {9} \n", perone.Id, perone.Name, perone._major, perone._startdate, perone.Graddate, perone._state, perone._country, perone._email, perone._phonenumber, perone._mailaddress);
            Console.WriteLine("Student ID: {0}, Student Name: {1}, Major: {2}, School Start Date: {3}, School Graduation Date: {4}, State/Territory: {5}, Country: {6}, E-Mail: {7}, Phone Number: {8}, Mail Address: {9}", pertwo.Id, pertwo.Name, pertwo._major, pertwo._startdate, pertwo.Graddate, pertwo._state, pertwo._country, pertwo._email, pertwo._phonenumber, pertwo._mailaddress);
            Console.WriteLine("Student ID: {0}, Student Name: {1}, Major: {2}, School Start Date: {3}, School Graduation Date: {4}, State/Territory: {5}, Country: {6}, E-Mail: {7}, Phone Number: {8}, Mail Address: {9}", perthree.Id, perthree.Name, perthree._major, perthree._startdate, perthree.Graddate, perthree._state, perthree._country, perthree._email, perthree._phonenumber, perthree._mailaddress);
            Console.WriteLine("Student ID: {0}, Student Name: {1}, Major: {2}, School Start Date: {3}, School Graduation Date: {4}, State/Territory: {5}, Country: {6}, E-Mail: {7}, Phone Number: {8}, Mail Address: {9}", perfour.Id, perfour.Name, perfour._major, perfour._startdate, perfour.Graddate, perfour._state, perfour._country, perfour._email, perfour._phonenumber, perfour._mailaddress);
            Console.WriteLine("Student ID: {0}, Student Name: {1}, Major: {2}, School Start Date: {3}, School Graduation Date: {4}, State/Territory: {5}, Country: {6}, E-Mail: {7}, Phone Number: {8}, Mail Address: {9}", perfive.Id, perfive.Name, perfive._major, perfive._startdate, perfive.Graddate, perfive._state, perfive._country, perfive._email, perfive._phonenumber, perfive._mailaddress);
            Console.WriteLine("Student ID: {0}, Student Name: {1}, Major: {2}, School Start Date: {3}, School Graduation Date: {4}, State/Territory: {5}, Country: {6}, E-Mail: {7}, Phone Number: {8}, Mail Address: {9}", persix.Id, persix.Name, persix._major, persix._startdate, persix.Graddate, persix._state, persix._country, persix._email, persix._phonenumber, persix._mailaddress);
            Console.WriteLine("Student ID: {0}, Student Name: {1}, Major: {2}, School Start Date: {3}, School Graduation Date: {4}, State/Territory: {5}, Country: {6}, E-Mail: {7}, Phone Number: {8}, Mail Address: {9}", perseven.Id, perseven.Name, perseven._major, perseven._startdate, perseven.Graddate, perseven._state, perseven._country, perseven._email, perseven._phonenumber, perseven._mailaddress);
            Console.WriteLine("Student ID: {0}, Student Name: {1}, Major: {2}, School Start Date: {3}, School Graduation Date: {4}, State/Territory: {5}, Country: {6}, E-Mail: {7}, Phone Number: {8}, Mail Address: {9}", pereight.Id, pereight.Name, pereight._major, pereight._startdate, pereight.Graddate, pereight._state, pereight._country, pereight._email, pereight._phonenumber, pereight._mailaddress);
            Console.WriteLine("Student ID: {0}, Student Name: {1}, Major: {2}, School Start Date: {3}, School Graduation Date: {4}, State/Territory: {5}, Country: {6}, E-Mail: {7}, Phone Number: {8}, Mail Address: {9}", pernine.Id, pernine.Name, pernine._major, pernine._startdate, pernine.Graddate, pernine._state, pernine._country, pernine._email, pernine._phonenumber, pernine._mailaddress);
            Console.WriteLine("Student ID: {0}, Student Name: {1}, Major: {2}, School Start Date: {3}, School Graduation Date: {4}, State/Territory: {5}, Country: {6}, E-Mail: {7}, Phone Number: {8}, Mail Address: {9}", perten.Id, perten.Name, perten._major, perten._startdate, perten.Graddate, perten._state, perten._country, perten._email, perten._phonenumber, perten._mailaddress);

            Console.WriteLine("Instances of writing of a private field within a setter: {0}", Student.Total);
        }
    }
}

谢谢你的帮助和解释!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-02 05:44:39

这应该有助于你开始:

代码语言:javascript
复制
public class Student
{
    private static List<string> _log = new List<string>();

    public string _major;
    public string _startdate;
    public string _state;
    public string _country;
    public string _mailaddress;
    public long _phonenumber;

    private int _id;
    public int Id { get { return _id; } }

    public string _name;
    public string Name { get { return _name; } }

    private int _graddate;
    public int Graddate
    {
        get { return _graddate; }
        set
        {
            if (value < 2020)
            {
                this._graddate = 0;
                _log.Add($"Set {_id}.Graddate to `0` (attempted `{value}`)");
            }
            else
            {
                this._graddate = value;
                _log.Add($"Set {_id}.Graddate to `{value}`");
            }
        }
    }

    public string _email;
    public string Email
    {
        get { return _email; }
        set
        {
            this._email = value;
            _log.Add($"Set {_id}.Email to `{value}`");
        }
    }

    public static string GetLog()
    {
        return String.Join(Environment.NewLine, _log);
    }

    public Student(int id, string name, string major, string startdate, int graddate, string state, string country, string email, long phonenumber, string mailaddress)
    {
        this._id = id;
        this._name = name;
        this._major = major;
        this._startdate = startdate;
        this._graddate = graddate;
        this._state = state;
        this._country = country;
        this._email = email;
        this._phonenumber = phonenumber;
        this._mailaddress = mailaddress;
    }
}

public class MainClass
{
    static void Main(string[] args)
    {
        Student persix = new Student(02121966, "Blanka", "Electrical Engineering", "February 12, 1966", 2030, "Brasilia", "Brazil", "ShoutOfEarth@gmail.com", 4908001801976, "777 Amazon River Basin");
        Student perten = new Student(07021955, "Sagat", "Physical Education", "July 02, 1955", 2050, "Ayutthaya", "Thailand", "VanillaSagat@gmail.com", 39800928830, "311 King's Court");

        persix.Email = "qaz@bar.com";
        perten.Email = "foo@bar.com";

        Console.WriteLine("Instances of writing of a private field within a setter: {0}", Student.GetLog());

    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60482636

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档