【问题标题】:call methods from my class.cs从我的 class.cs 调用方法
【发布时间】:2012-10-20 15:20:56
【问题描述】:

我需要在我的学生班中编写 2 个方法来执行以下操作

hasPassed() 如果学生的年级 >= 40 或 如果标记为 ,则为 false

toString() 应该返回一个包含 班级内举行的学生详细信息 例如 “12345 Basil Fawlty,1946 年 8 月 23 日”

这是我为上述方法提供的代码,我对上述方法的要求是否正确?

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

namespace CourseWork
{
    public class Student
    {
        private static string firstname;
        private string secondname;
        private string dateofbirth;
        private string course;
        private int matricnumber;
        private double yearmark;

      public bool hasPassed()
        {
            if (yearmark >= 40)
                return true;
            else
                return false;
        }

      public void toString()
      {
          firstname = "Basil";
          secondname = "Fawlty";
          dateofbirth = "23/08/1946";
          course = "MA Hotel Management";
          matricnumber = 12345;
          yearmark = 55;
      }

        public Student()
        {
        }

        public string FirstName
        {
            get { return firstname; }
            set { firstname = value; }
        }

        public string SecondName
        {
            get { return secondname; }
            set { secondname = value; }
        }
        public string DateOfBirth
        {
            get { return dateofbirth; }
            set { dateofbirth = value; }
        }

        public string Course
        {
            get { return course; }
            set { course = value; }
        }
        public int MatricNumber
        {
            get { return matricnumber; }
            set
            {
                if (value <= 99999 && value >= 10000)
                {
                    matricnumber = value;
                }
                else
                {
                    Console.WriteLine("Invalid Matric Number: {0}", value);
                }

              matricnumber = value;
            }
        }
        public double YearMark
        {
            set
            {
                if (value <= 100 && value >= 0)
                {
                    yearmark = value;
                }
                else
                {
                    Console.WriteLine("Invalid Year Mark: {0}", value);
                }
              yearmark = value;
            }

        }
    }

然后我需要在执行以下操作的获取按钮中使用上述方法

Get:使用 Student 类方法的值来更新文本框。这 Student.hasPassed() 方法应该用于更新通过/失败标签。这 应使用 Student.toString () 更新学生详细信息摘要。

但我在编码时遇到了问题,我无法从我的学生班级调用 hasPassed() 方法或 toString() 方法

所以我做错了什么但看不到它是什么 任何想法如何解决这个问题?

我有一个设置按钮,基本上可以让我在学生班级中保存更新值,虽然我不认为那是正确保存它们,但直到我让 Get 按钮工作我才知道我使用了 Student student = new student() get 按钮中的 set 按钮我需要使用 toString 方法在 txt 框和标签中显示例如 12345 Basil Fawlty, 23/08/194,然后我需要在 Get 按钮中使用 hasPassed() 方法所以当年标记 >= 40 时,另一个标签会显示 Pass or fail if

【问题讨论】:

    标签: c# visual-studio-2010 class button methods


    【解决方案1】:

    我没有完全阅读您的问题,因为有很多错误。

    例如

    public void toString()
          {
              firstname = "Basil";
              secondname = "Fawlty";
              dateofbirth = "23/08/1946";
              course = "MA Hotel Management";
              matricnumber = 12345;
              yearmark = 55;
          }
    

    你的对象在哪里?

    你应该像这样创建一个对象: 学生 stu = new Student();

    小心点,让你的问题更容易理解!

    看看: https://stackoverflow.com/questions/902994/how-to-ask-programming-questions-correctly

    【讨论】:

      【解决方案2】:
      1. firstName 变量是静态的。这将使 Student 的所有实例共享相同的名字,这是不正确的。每个学生对象都应该有自己的名字。

      2. 类的实例变量是私有的,无法设置。您可能想要创建一个将这些变量作为参数的构造函数。

        public Student(string firstName, string secondName, ...) 
        {
            this.firstName = firstName;
            this.secondName = secondName;
            ...
        }
        
      3. hasPassed() 方法是正确的。您可以通过实例化 Student 类的实例并在实例化的对象上调用 hasPassed() 来验证行为是否有效。

        double goodYearMark = 85;
        Student goodStudent = new Student("Basil", "Fawlty", ..., goodYearMark);
        Console.WriteLine("Good Student Passed? " + goodStudent.hasPassed());
        
        double badYearMark = 35;
        Student badStudent = new Student("Bad", "Student", ..., badYearMark);
        Console.WriteLine("Bad Student Passed? " + badStudent.hasPassed());
        
      4. ToString() 方法应该返回一个字符串值。 .NET 中的每个对象都有一个 ToString() 方法,您可以使用 override 关键字覆盖默认行为。 请参阅MSDN documentation for the Object.ToString Method

        public override string ToString() 
        {
            return string.format("{0} {1}, {2}", firstName, secondName, dateOfBirth);
        }
        

      上面的代码示例可能无法编译,因为我将它们直接输入到响应窗口中,但希望它们可以作为有用的指导。希望这会有所帮助!

      【讨论】:

      • 我没有注意到属性设置器。您可以忽略这部分:“类的实例变量是私有的,无法设置。”
      • 谢谢是非常有帮助的,遗憾的是唯一教你方法,额外的类,并没有真正教我们如何编码。所以再次感谢,帖子非常有帮助
      【解决方案3】:

      再读一遍 toString 要求,你做错了。当您现在在代码中调用 toString 时,现有值会发生什么变化?

      另外,检查最后两个属性设置器。目前您并未阻止用户设置无效值。

      您还需要创建一个类的实例,并在其上设置可以从 toString 返回的初始值。

      祝你好运,你快到了:-)

      【讨论】:

      • tbh 我真的不知道该怎么做,现在在过去 2 天的工作之后,我有足够的时间试图弄清楚该怎么做谢谢你的帮助
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-12
      • 1970-01-01
      • 1970-01-01
      • 2012-04-24
      • 2020-11-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多