2015-11-06 62 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可以簡單地訪問它們。