Visual Studio 由于其保护级别而无法访问?

gcuhipw9  于 2022-12-30  发布在  其他
关注(0)|答案(2)|浏览(539)

我开始学习C#,我试着创建一个类,我看了一个视频,我做了完全相同的事情。
但我不明白为什么我会得到这个错误“学生。学生(字符串,整型,双精度)'是不可访问的,由于其保护级别。”

  1. static void Main(string[] args)
  2. {
  3. Student Student1 = new Student("Aya", 00001, 4.0);
  4. Console.WriteLine(Student1.getName());
  5. Student1.setGPA(3.5);
  6. Console.WriteLine(Student1.getGPA());
  7. }
  8. }
  9. public class Student
  10. {
  11. private String Name;
  12. private int StudentId;
  13. private double GPA;
  14. private Student(String Name, int StudentId, double GPA)
  15. {
  16. this.Name = Name;
  17. this.StudentId = StudentId;
  18. this.GPA = GPA;
  19. }
  20. public String getName()
  21. {
  22. return this.Name;
  23. }
  24. public int getStudentId()
  25. {
  26. return this.StudentId;
  27. }
  28. public double getGPA()
  29. {
  30. return this.GPA;
  31. }
  32. public void setGPA(double GPA)
  33. {
  34. this.GPA = GPA;
  35. }
  36. }
  37. }
wfauudbj

wfauudbj1#

你的构造函数是私有的,所以不能在类外访问它。另外一件事是你可以使用属性,例如你可以替换:

  1. private String Name;
  2. public String getName()
  3. {
  4. return this.Name;
  5. }

用这个

  1. public string Name {get; set; }
hgc7kmma

hgc7kmma2#

构造函数必须是公共才能访问它
https://dotnetfiddle.net/S3aW62

相关问题