你如何让get / set工作? [英] How do you make the get/set work?

查看:150
本文介绍了你如何让get / set工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的家伙我正在尝试学习如何创建类并使用get / set。我已经玩了几个小时的代码,让我自己很困惑,我不知道哪个方向了。有人能指出我正确的方向使这段代码有效吗? 这是我更新的代码。它会让我输入两个班级和作业数量,但程序结束了......有人知道为什么吗?



Ok guys I'm trying to learn how to create classes and use the get/set. I have played with this code for hours and have gotten myself so confused,, I don't know which way is up. Can someone point me in the right direction to make this code work? This is my updated code. It will let me enter both classes and number of assignments but the program ends there....anyone know why?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmithMichaelFixedDebugNine01
{
    class Assignments
    {

        static void Main(string[] args)
        {
            HomeworkAssignment course1 = new HomeworkAssignment();
            HomeworkAssignment course2 = new HomeworkAssignment();
            string entry;


            // Get info for first class
            Console.Write("What class do you have homework for? ");
            entry = Console.ReadLine();
            course1.entry();
            Console.Write("How many exercises must you complete? ");
            entry = Console.ReadLine();
            course1.entry();


            // Get info for another class
            Console.Write("What class do you have homework for? ");
            entry = Console.ReadLine();
            course2.entry();
            Console.Write("How many exercises must you complete? ");
            entry = Console.ReadLine();
            course2.entry();

        }
        class HomeworkAssignment
        {
            
          // 10 minutes to complete each exercise
           internal void entry()
            {

            }
        }
        
        const int TIME_PER_EXERCISE = 10;
        private int numberOfExercises = 0;
        public int NumberOfExcercises
        {
            get
            {
                return numberOfExercises;
            }
            set
            {
                numberOfExercises = value;

            }
        }
        private int timeToComplete = 0;
        public int TimeToComplete
        {
            get
            {
                return timeToComplete;
            }
            set
            {
                timeToComplete = numberOfExercises * TIME_PER_EXERCISE;

            }


        }

    }
}

推荐答案

C#中有两种形式的属性:自动和可变备份。

自动属性使系统处理后备变量:

There are two forms of Property in C#: automatic and variable backed.
An automatic property leaves the system to handle the backing variable:
public int MyProperty { get; set; }



您可以直接通过酒店名称获取价值的唯一方式。



变量支持的属性是不同的:


And the only way you can get access to the value is directly via the property name.

A variable backed property is different:

private int _MyProperty = 666;
public int MyProperty
   {
   get { return _MyProperty; }
   set { _MyProperty = value; }
   }

在外部,它与自动属性相同,但在类中,您可以访问直接保存数据的变量:

Externally it is identical to an automatic property, but inside the class you can access the variable that holds the data directly:

...
int x = _MyProperty;
...



您要做的就是将两者结合起来,这意味着每次尝试使用该属性时,它都会最终在无限循环中尝试从Getter或Setter内部调用Getter或Setter - 最终,你的应用程序用完了堆栈和崩溃。



所以试试这:


What you are trying to do is combine the two, and that means every time you try and use the property, it ends up in an infinite loop trying to call the Getter or Setter from inside the Getter or Setter - and eventually, you app runs out of stack and crashes.

So try this:

private int _TimeToComplete = 0;
public int TimeToComplete
    {
        get {return _TimeToComplete; }
        set { _TimeToComplete = NumberOfExercises * TIME_PER_EXERCISE;}
    }


使用获取设置访问者时。您需要使用额外的变量。你的例子很可能会创建一个 StackOverflowException



试试这个例子:



When using get and set accessor. You need to use an extra variable. Your example will most likely create a StackOverflowException

Try this example:

private int m_myInt = 0;
public MyInt
{
  get { return m_myInt; }
  set { m_myInt = value; }
}





您在物业中分配和读取的位置不能是物业本身。



The location you assign to and read from in the property cannot be the property itself.


在查询者更新解决方案1 ​​+ 2之后的代码后添加:



注意你仍然在属性获取器中使用属性名称和-setter(粗体+带下划线):

Added after the inquirer updated the code after Solutions 1+2:

Note you're still using the property name in the property-getter and -setter (bold+underlined):
private int timeToComplete = 0;
public int TimeToComplete
{
    get
    {
        return TimeToComplete;
    }
    set
    {
        TimeToComplete = numberOfExercises * TIME_PER_EXERCISE;
    }
}





它应该是这样的:



It should be like this:

private int timeToComplete = 0;
public int TimeToComplete
{
    get
    {
        return timeToComplete;
    }
    set
    {
        timeToComplete = numberOfExercises * TIME_PER_EXERCISE;
    }
}





您的新错误运营商'*'的解释不能应用于操作数输入int和object。关于 TimeToComplete = numberOfExercises * TIME_PER_EXERCISE;



你有两个 TIME_PER_EXERCISE -constants已定义。一个是 int - 但这不是编译器在这里使用的那个;)



编辑:

补充说明:您正在从控制台读取的字符串条目中执行任何操作,并且你的方法HomeworkAssignment.entry()什么都不做。我假设应该传递一些值..



The explanation of your new error "Operator '*' cannot be applied to operands of type int and object." regarding TimeToComplete = numberOfExercises * TIME_PER_EXERCISE;:

You have two TIME_PER_EXERCISE-constants defined. One is an int - but that's not the one that's being used by the compiler here ;)


Further notes: You're doing nothing with the string entry that you're reading from the console, and your method HomeworkAssignment.entry() does nothing. I assume there should be some value being passed..


这篇关于你如何让get / set工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆