2015-11-06 43 views
-1

我是C#的新手,并且一起编程,并试图让它运行。我花了整整一天的时间阅读关于静态和非静态的内容,但似乎没有把它弄清楚。任何和所有的帮助将不胜感激,因为我需要明天晚上工作的代码。以下是主要和课程类:在静态主C中调用非静态#

using System; 

namespace Lab4 
{ 
    class MainClass 
    { 
     public static void Main (string[] args) 
     { 
      Console.WriteLine ("Welcome to the Course Monitoring System v1.0"); 
      Course.Menu(); 
      Course.Choice(); 
     } 
    } 
} 

这是课程类别:

using System; 
using System.Collections.Generic; 

namespace Lab4 
{ 
    public class Course 
    { 
     private string courseNumber; 
     private string courseName; 
     private int courseHours; 
     private string descript; 
     private string prefix; 
     public List<Course> school = new List<Course>(); 
     private int howmany=0; 
     private int totalcourse=0; 

     //This section is for returning and adjusting values of private data through the main program. 
     public string cn{ 
      get {return courseNumber; } 
      set {if (value != null) 
       cn = value; 
      } 
     } 
     public string name{ 
      get{ return courseName; } 
      set{if (value!="") 
       name=courseName; 
      } 
     } 
     public int hours{ 
      get {return courseHours; } 
      set {if (value != 0) 
       hours = value; 
      } 
     } 
     public string script { 
      get {return descript; } 
      set { 
       if (value != "") 
        script = value; 
      } 
     } 
     public string pfix { 
      get {return prefix; } 
      set {if (value != "") 
       pfix = value; 
      } 
     } 

     public Course (string pfix, string name, string cn, int hours, string script) 
     { 
      courseNumber = cn; 
      courseName = name; 
      courseHours = hours; 
      descript= script; 
      prefix = pfix; 
     } 

     //This portion of code overrides the string and allows it to output the information held within the constructor. 
     public override string ToString() 
     { 
      return prefix + " " + courseName+" " + courseNumber + " " + descript + " It is a " + courseHours + " hour course."; 
     } 
     //The menu application for my program 
     public static void Menu() 
     { 
      Console.WriteLine ("Please choose from one of the following options: "); 
      Console.WriteLine ("......................................................................"); 
      Console.WriteLine ("1.)Start a new Course List."); 
      Console.WriteLine ("2.)Delete Course from Current List."); 
      Console.WriteLine ("3.)Print Current Course List."); 
      Console.WriteLine ("4.)Add Course to Current List."); 
      Console.WriteLine ("5.)Shutdown Program"); 
     } 
     //This is the controller for the menu. It allows for functionality to control the tasks requested by the user. 
     public void Choice() 
     { 
      int selection=int.Parse(Console.ReadLine()); 
      while (selection >5 || selection < 1) { 
       Console.WriteLine ("Choice invalid. Please choose between 1 and 5."); 
       selection = int.Parse (Console.ReadLine()); 
      } 
      if (selection == 4) { 
       Add(); 
       Menu(); 
       Choice(); 
       Console.WriteLine(); 
      } 
      if (selection == 2) { 
       Delete(); 
       Menu(); 
       Choice(); 
       Console.WriteLine(); 
      } 
      if (selection == 3) { 
       Print(); 
       Menu(); 
       Choice(); 
       Console.WriteLine(); 
      } 
      if (selection == 1) { 
       New(); 
       Menu(); 
       Choice(); 
       Console.WriteLine(); 
      } 
      if (selection == 5) { 
       Console.WriteLine(); 
       Console.WriteLine ("Thank you for using this program for your scheduling needs."); 
      } 
     } 
     //This method when called will print a numbered list of courses refrenced by the created List 
     public void Print() 
     { 
      Console.WriteLine(); 
      Console.WriteLine ("Course Name.........Course Number.........Course Description........Course Hours"); 
      Console.WriteLine ("********************************************************************************"); 
      for (int i = 0; i < totalcourse; i++) { 
       int place = i + 1; 
       Console.WriteLine (place+".)"+school [i]); 
      } 
      Console.WriteLine(); 
     } 
     //This method will add an item to the end of the current list. 
     public void Add() 
     { 
      Console.WriteLine(); 
      Console.Write ("How many courses would you like to add at the end of your index?"); 
      int numberAdded = int.Parse(Console.ReadLine()); 
      for (int i = 0; i < numberAdded; i++) { 
       Console.WriteLine ("Please use the following templet for your entries..."); 
       Console.WriteLine ("Course Prefix, Course Name, Course Number, Course Hours, Course Description "); 
       school.Add (new Course (prefix = Console.ReadLine(), courseName = Console.ReadLine(), 
        courseNumber = Console.ReadLine(), courseHours = int.Parse (Console.ReadLine()), descript = Console.ReadLine())); 
      } 
      totalcourse = totalcourse + numberAdded; 
      Console.WriteLine(); 
     } 
     //This method will delete an Item from the list based on the position 0-x based on x-1, the output that is seen by the user. After each iteration it will 
     //also create a list so that further deletions can be managed approiatly. 
     public void Delete() 
     { 
      if (totalcourse < 1) { 
       Console.WriteLine(); 
       Console.WriteLine ("There is nothing to delete!"); 
       Console.WriteLine(); 
      }else{ 
       Console.WriteLine(); 
       Console.Write ("How many entries do you wish to remove?"); 
       int removed = int.Parse (Console.ReadLine()); 
       for (int i = 0; i < removed; i++) { 
        Console.WriteLine ("Please type the index line number of the item you wish to remove."); 
        int delete = int.Parse (Console.ReadLine()); 
        school.RemoveAt (delete - 1); 
        totalcourse = totalcourse - 1; 
        Print(); 
       } 
      } 
      Console.WriteLine(); 
     } 
     //This method is called to create a new list of Courses to be created by the user. 
     public void New() 
     { 
      Console.WriteLine(); 
      if (howmany > 0) { 
       Clear(); 
      } else { 
       Console.Write ("How many courses do you want to create? "); 
       howmany = int.Parse (Console.ReadLine()); 
       Console.WriteLine(); 
       for (int i = 0; i < howmany; i++) { 
        Console.WriteLine(); 
        school.Add (new Course (prefix = Console.ReadLine(), courseName = Console.ReadLine(), courseNumber = Console.ReadLine(), courseHours = int.Parse (Console.ReadLine()), descript = Console.ReadLine())); 
       } 
       totalcourse = totalcourse + howmany; 
       Console.WriteLine(); 
      } 
     } 
     //If there is already a list in place this method will be called and you will be asked if you wish to delete and start new. 
     public void Clear() 
     { 
      Console.WriteLine(); 
      Console.Write ("You want to discard old work and start a new list? Enter True or False...:"); 
      bool clean=bool.Parse(Console.ReadLine()); 
      if (clean == true) { 
       school.Clear(); 
       totalcourse = 0; 
       howmany = 0; 
       New(); 
      } else 
       Console.WriteLine ("No changes will be made. Exiting to Main Menu..."); 
      Console.WriteLine(); 
     } 
     /* public static void Myfour() 
     { 
      Console.WriteLine(); 
      Console.WriteLine ("These are four pre loaded courses."); 
      Course c1 = new Course ("MISSILE", 84, 3, "The Course is for missiles", "CRN"); 
      Console.WriteLine (c1); 

      Console.WriteLine(); 
     }*/ 
    } 
} 
+0

你必须使'Choice()'静态以及从'静态Main()'方法调用它。这也需要在Choice()内调用的其他方法是静态的。或者,在Main()中创建一个'Course'类的新实例并调用这些方法。 –

+0

@ArghyaC他无法将所有其他方法/字段更改为静态,而无法使“选择”为静态。 –

+0

@RonBeyer这就是我所说的。但是,是的,我提到了方法,错过了提及领域。谢谢! –

回答

0

您的课程类需要是静态的:

public class Course 

等做你的选择方法:

public static void Choice() 

静态意味着您可以使用成员(例如一个方法),而不必使用'new'创建一个类的实例。这就是你想要做的,所以方法和容器类都需要static修饰符。尽管如此,您还需要将“课程”类中的所有其他方法设置为静态。

或者说,为什么你不只是在你的Main方法创建课程类的一个实例:

Course myCourse = new Course(); 
myCourse.Menu(); 
myCourse.Choice(); 

那么你的代码将工作

+0

非常感谢Chris。我不知道我可以像这样创建一个类的实例。 –

0

这应该让你开始。我所做的是

  1. 所做的构造不带参数
  2. 创建的课程的实例在main方法并调用其方法

这决不是完整的,但应该让你开始。

using System; 
using System.Collections.Generic; 
namespace Lab4 
{ 
    public class MainClass 
    { 
     public static void Main (string[] args) 
     { 
      Console.WriteLine ("Welcome to the Course Monitoring System v1.0"); 
      var c = new Course(); 
      c.Menu(); 
      c.Choice(); 
     } 
    } 

    public class Course 
    { 
     private string courseNumber; 
     private string courseName; 
     private int courseHours; 
     private string descript; 
     private string prefix; 
     public List<Course> school = new List<Course>(); 
     private int howmany=0; 
     private int totalcourse=0; 

     //This section is for returning and adjusting values of private data through the main program. 
     public string cn{ 
      get {return courseNumber; } 
      set {if (value != null) 
       cn = value; 
      } 
     } 
     public string name{ 
      get{ return courseName; } 
      set{if (value!="") 
       name=courseName; 
      } 
     } 
     public int hours{ 
      get {return courseHours; } 
      set {if (value != 0) 
       hours = value; 
      } 
     } 
     public string script { 
      get {return descript; } 
      set { 
       if (value != "") 
        script = value; 
      } 
     } 
     public string pfix { 
      get {return prefix; } 
      set {if (value != "") 
       pfix = value; 
      } 
     } 

     public Course() 
     { 
     } 
     public Course (string pfix, string name, string cn, int hours, string script) 
     { 
      courseNumber = cn; 
      courseName = name; 
      courseHours = hours; 
      descript= script; 
      prefix = pfix; 
     } 

     //This portion of code overrides the string and allows it to output the information held within the constructor. 
     public override string ToString() 
     { 
      return prefix + " " + courseName+" " + courseNumber + " " + descript + " It is a " + courseHours + " hour course."; 
     } 
     //The menu application for my program 
     public void Menu() 
     { 
      Console.WriteLine ("Please choose from one of the following options: "); 
      Console.WriteLine ("......................................................................"); 
      Console.WriteLine ("1.)Start a new Course List."); 
      Console.WriteLine ("2.)Delete Course from Current List."); 
      Console.WriteLine ("3.)Print Current Course List."); 
      Console.WriteLine ("4.)Add Course to Current List."); 
      Console.WriteLine ("5.)Shutdown Program"); 
     } 
     //This is the controller for the menu. It allows for functionality to control the tasks requested by the user. 
     public void Choice() 
     { 
      int selection=int.Parse(Console.ReadLine()); 
      while (selection >5 || selection < 1) { 
       Console.WriteLine ("Choice invalid. Please choose between 1 and 5."); 
       selection = int.Parse (Console.ReadLine()); 
      } 
      if (selection == 4) { 
       Add(); 
       Menu(); 
       Choice(); 
       Console.WriteLine(); 
      } 
      if (selection == 2) { 
       Delete(); 
       Menu(); 
       Choice(); 
       Console.WriteLine(); 
      } 
      if (selection == 3) { 
       Print(); 
       Menu(); 
       Choice(); 
       Console.WriteLine(); 
      } 
      if (selection == 1) { 
       New(); 
       Menu(); 
       Choice(); 
       Console.WriteLine(); 
      } 
      if (selection == 5) { 
       Console.WriteLine(); 
       Console.WriteLine ("Thank you for using this program for your scheduling needs."); 
      } 
     } 
     //This method when called will print a numbered list of courses refrenced by the created List 
     public void Print() 
     { 
      Console.WriteLine(); 
      Console.WriteLine ("Course Name.........Course Number.........Course Description........Course Hours"); 
      Console.WriteLine ("********************************************************************************"); 
      for (int i = 0; i < totalcourse; i++) { 
       int place = i + 1; 
       Console.WriteLine (place+".)"+school [i]); 
      } 
      Console.WriteLine(); 
     } 
     //This method will add an item to the end of the current list. 
     public void Add() 
     { 
      Console.WriteLine(); 
      Console.Write ("How many courses would you like to add at the end of your index?"); 
      int numberAdded = int.Parse(Console.ReadLine()); 
      for (int i = 0; i < numberAdded; i++) { 
       Console.WriteLine ("Please use the following templet for your entries..."); 
       Console.WriteLine ("Course Prefix, Course Name, Course Number, Course Hours, Course Description "); 
       school.Add (new Course (prefix = Console.ReadLine(), courseName = Console.ReadLine(), 
        courseNumber = Console.ReadLine(), courseHours = int.Parse (Console.ReadLine()), descript = Console.ReadLine())); 
      } 
      totalcourse = totalcourse + numberAdded; 
      Console.WriteLine(); 
     } 
     //This method will delete an Item from the list based on the position 0-x based on x-1, the output that is seen by the user. After each iteration it will 
     //also create a list so that further deletions can be managed approiatly. 
     public void Delete() 
     { 
      if (totalcourse < 1) { 
       Console.WriteLine(); 
       Console.WriteLine ("There is nothing to delete!"); 
       Console.WriteLine(); 
      }else{ 
       Console.WriteLine(); 
       Console.Write ("How many entries do you wish to remove?"); 
       int removed = int.Parse (Console.ReadLine()); 
       for (int i = 0; i < removed; i++) { 
        Console.WriteLine ("Please type the index line number of the item you wish to remove."); 
        int delete = int.Parse (Console.ReadLine()); 
        school.RemoveAt (delete - 1); 
        totalcourse = totalcourse - 1; 
        Print(); 
       } 
      } 
      Console.WriteLine(); 
     } 
     //This method is called to create a new list of Courses to be created by the user. 
     public void New() 
     { 
      Console.WriteLine(); 
      if (howmany > 0) { 
       Clear(); 
      } else { 
       Console.Write ("How many courses do you want to create? "); 
       howmany = int.Parse (Console.ReadLine()); 
       Console.WriteLine(); 
       for (int i = 0; i < howmany; i++) { 
        Console.WriteLine(); 
        school.Add (new Course (prefix = Console.ReadLine(), courseName = Console.ReadLine(), courseNumber = Console.ReadLine(), courseHours = int.Parse (Console.ReadLine()), descript = Console.ReadLine())); 
       } 
       totalcourse = totalcourse + howmany; 
       Console.WriteLine(); 
      } 
     } 
     //If there is already a list in place this method will be called and you will be asked if you wish to delete and start new. 
     public void Clear() 
     { 
      Console.WriteLine(); 
      Console.Write ("You want to discard old work and start a new list? Enter True or False...:"); 
      bool clean=bool.Parse(Console.ReadLine()); 
      if (clean == true) { 
       school.Clear(); 
       totalcourse = 0; 
       howmany = 0; 
       New(); 
      } else 
       Console.WriteLine ("No changes will be made. Exiting to Main Menu..."); 
      Console.WriteLine(); 
     } 
     /* public static void Myfour() 
     { 
      Console.WriteLine(); 
      Console.WriteLine ("These are four pre loaded courses."); 
      Course c1 = new Course ("MISSILE", 84, 3, "The Course is for missiles", "CRN"); 
      Console.WriteLine (c1); 

      Console.WriteLine(); 
     }*/ 
    } 
} 
0

与你的类的第一个问题是,Course类包含一个List<Course> school变量。学校是另一个逻辑实体,应该在另一个班级。我认为Course类应该是它自己的类,没有任何static成员。所有Console相关函数和school变量都应该包含在一个(也许是静态的,如果这是必需的)School类。然后

main功能会打电话

School.Menu(); 
School.Choice(); 
0

你叫Course.Choice()这是一个className.methoddName()是指用于访问静态方法的方式。这意味着你将需要更改

public void Choice() { } 
public void Menu() { } 

public static void Choice() { } 
public static void Menu() { } 

但是,就像我之前说的,因为Choice()Menu()现在是静态的,因此被称为这些方法中的一切都必须是静态的以及诸如Add(),Delete(),Print()

那么做一个静态方法意味着什么? - 你不(并且不能)实例化它们来访问它们。另一种方法来此,是简单地使用new关键字创建class Course一个实例:

Course cr = new Course(); 
//This works because Course and MainClass are under the same namespace 
//Or else, you will need to do this: 
OtherNamespace.Class x = new OtherNamespace.Class() ; 

,现在你可以访问Course类中的所有非静态方法!就像这样:

cr.Menu(); 
cr.Choice(); 

创建一个实例似乎是一个更简单的方法来解决你的问题,因为所有的方法都是实例。但是你应该真正理解什么时候使用哪个更合适。你可以阅读更多关于here

即使您只询问有关调用非静态。我真的认为你在Course班上有很多改进。你在课堂里聚集了太多东西。例如,您可以创建一个新类来存储所有属性,如cn,name,hours等。这里最好将它们设置为静态属性,然后使用TheNewClass.hours可以简单地访问它们。