无法调用我的类中的方法.cs



我需要在我的学生班上编写2种方法,以执行以下

HASPASS()如果学生有一年标记> = 40或错误如果标记为40

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;
            }
        }
    }

i然后需要上面的方法,以在get按钮中使用以下

get:使用学生班方法的值来更新文本框。这student.haspassed()方法应用于更新通行/失败标签。这学生详细信息摘要应使用student.tostring()。

进行更新。

,但是我在编码它方

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

为了为了可见的方法,您需要创建类Student的实例。ex,

Student _student = new Student();
bool _x = _student.hasPassed();

如果您希望成员在不实例化的情况下访问,请使成员静态,

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

但请记住,静态成员无法看到非静态成员。在这种情况下,它会因为找不到 yearmark而赢得编译。

相关内容

最新更新