2012-08-14 14 views
4

正如你們可以從代碼中看到我創建三類汽車,新模型,主。所有的基本方法都是在車類中,我創建了繼承類來嘗試(繼承)。因爲你可以看到我在做什麼只是用newmodel繼承類的newrims()方法輸出car類的wheel()方法來創建一個完整的句子。需要使代碼更準確的建議。替代或良好的方式,以防止類輸出

namespace ConsoleApplication4 
{ 
    public class car 
    { 
    public static void wheel() 
    { 
     Console.WriteLine("The wheels are rolling"); 
    } 
    public static void doors() 
    { 
     Console.WriteLine("The Doors are automatic"); 
    } 
    public static void engine() 
    { 
     Console.WriteLine("The engine of car is runing"); 
    } 
    public static void oil() 
    { 
     Console.WriteLine("the oil is full in tank"); 
    } 
    } 
    public class newmodel : car 
    { 
    public static void newrims() 
    { 
     car.wheel(); 
     Console.WriteLine("And The new rims are rocking"); 
    } 

    } 

    class Program 
    { 
     static void Main() 
     { 
     while (true) 
     { 
      Console.WriteLine("Press A to Roll the Wheels baby"); 
      Console.WriteLine("Press B to Open/Close the Doors"); 
      Console.WriteLine("Press C to Start the Car Engine"); 
      Console.WriteLine("Press D to Check the Oil in tank"); 
      Console.WriteLine("Press E to Rims/wheels"); 
      Console.WriteLine("Press F to Exit the vehicle"); 
      char c = Convert.ToChar(Console.ReadLine()); 
      switch (c) 
      { 
       case 'a': 
        { 
         car.wheel(); 
         break; 
        } 
       case 'b': 
        { 
         car.doors(); 
         break; 
        } 
       case 'c': 
        { 
         car.engine(); 
         break; 
        } 
       case 'd': 
        { 
         car.oil(); 
         break; 
        } 
       case 'e': 
        { 
         newmodel.newrims(); 
         break; 
        } 
       case 'f': 
        { 
         Environment.Exit(0); 
         break; 
        } 
       default: 
        { 
         Console.WriteLine("Please Enter the Correct Input"); 
         break; 
        } 
       } 
      } 
     } 
    } 
} 
+3

您正在尋找'base',而不是'car'。 – CodeCaster 2012-08-14 13:29:29

+3

我很努力去理解你的實際問題...... – 2012-08-14 13:29:40

+0

這也是你的功課嗎?請嘗試相應地添加標籤 – sundar 2012-08-14 13:31:22

回答

2

在你的情況下繼承的例子會是這樣的

namespace ConsoleApplication4 
{ 
    public class car 
    { 
     //note the absence of static keyword..it means it is an instance method 
     //note the virtual keyword..it means derived classes can override the behavior 
     public virtual void wheel() 
     { 
      Console.WriteLine("The wheels of car are rolling"); 
     } 

    } 

    public class newmodel : car 
    { 
     //note the override keyword....it means newmodel overrides the wheel function 
     public override void wheel() 
     { 
      //depending on your needs you may or maynot 
      // need to call the base class function first 
      base.wheel(); 
      Console.WriteLine("And The new rims are rocking"); 
     } 

    } 

    class Program 
    { 
     static void Main() 
     { 
      //Instead of static methods you create a car object on 
      //on which you invoke member functions 
      car currentcar = new car(); 
      while (true) 
      { 
       Console.WriteLine("Press A to Roll the Wheels baby"); 
       Console.WriteLine("Press N to switch to new model"); 
       char c = Convert.ToChar(Console.ReadLine()); 
       switch (c) 
       { 
        case 'a': 
         { 
          currentcar.wheel(); 
          break; 
         } 
        case 'n': 
         { 
          currentcar = new newmodel(); 
          break; 
         } 
        default: 
         { 
          Console.WriteLine("Please Enter the Correct Input"); 
          break; 
         } 

       } 
      } 
     } 
    } 
} 

您會注意到,按a將調用車輪功能,但取決於您的車是普通舊車還是新車型,它將向控制檯輸出不同的內容。

+0

我想他也想要'car.wheel()'函數的輸出。 – Default 2012-08-14 13:39:47

+0

@Default fixed :) – 2012-08-14 13:41:52

+0

這可能是OP正在尋找的東西。可能要強調關於base.wheel()的實際更改。在原始問題中'newmodel'沒有'wheel()'函數,而是'newrims()'函數。這會使它更容易,因爲它只需要一個'wheel();'就足夠了。 – Default 2012-08-14 13:45:40

1

刪除所有的靜態...從類中創建Car和/或NewModel實例並使用實例。

我沒有實際參考值,但是有些東西要考慮到:

  • ,如果你對大部分功能使用靜態,也許你做了一個設計缺陷。
  • 你已經做了一個基類繼承的類,這是很好
  • 而不是使用靜態函數,創建汽車對象(即currentCar =新車(),並創建一個newModel,並向實例)
  • 上使用的功能這些實例而不是類,那麼你可以刪除靜態關鍵字。當你使用一個單一的變量(即currentVehicle,你可以從汽車創建的:即currentVehicle = new car())時,你可以稍後將它變成一個新模型並使用新模型的函數,如currentVehicle = new newmodel )
  • 通常類是書面的首都,所以汽車和newModel,並向和類變量/實例,而不資本:即汽車=汽車(),newModel,並向= newModel,並向()
+0

感謝我是初學者你能給我一些很好的參考 – Geek 2012-08-14 13:40:51

+0

我在上面的答案中添加了很多評論。 – 2012-08-14 13:47:50

+0

感謝隊友,真的很好,再次感謝你:) – Geek 2012-08-14 13:57:55

0

我不確定你想要做什麼,因爲你正在使用switch語句來決定調用哪些方法,而且你並沒有按照它的意圖使用繼承或多態。但是,讓我向您展示一下如何以不同的方式構造這個結構的例子。

namespace ConsoleApplication4 
{ 
    public abstract class car 
    { 
    public virtual void wheel() 
    { 
     Console.WriteLine("The wheels are rolling"); 
    } 
    public virtual void doors() 
    { 
     Console.WriteLine("The Doors are automatic"); 
    } 
    public virtual void engine() 
    { 
     Console.WriteLine("The engine of car is runing"); 
    } 
    public virtual void oil() 
    { 
     Console.WriteLine("the oil is full in tank"); 
    } 
    } 

    public class standardmodel : car 
    { 
    } 

    public class newmodel : car 
    { 
    public override void wheel() 
    { 
     base.wheel(); 
     Console.WriteLine("And The new rims are rocking"); 
    } 

    } 

    class Program 
    { 
     static void Main() 
     { 
     while (true) 
     { 
      Console.WriteLine("Press A to Roll the Wheels baby"); 
      Console.WriteLine("Press B to Open/Close the Doors"); 
      Console.WriteLine("Press C to Start the Car Engine"); 
      Console.WriteLine("Press D to Check the Oil in tank"); 
      Console.WriteLine("Press E to Rims/wheels"); 
      Console.WriteLine("Press F to Exit the vehicle"); 
      char c = Convert.ToChar(Console.ReadLine()); 

      Car standard = new standardcar(), 
       newModel = new newmodel(); 

      switch (c) 
      { 
       case 'a': 
        { 
         standard.wheel(); 
         break; 
        } 
       case 'b': 
        { 
         standard.doors(); 
         break; 
        } 
       case 'c': 
        { 
         standard.engine(); 
         break; 
        } 
       case 'd': 
        { 
         standard.oil(); 
         break; 
        } 
       case 'e': 
        { 
         newModel.wheel(); 
         break; 
        } 
       case 'f': 
        { 
         Environment.Exit(0); 
         break; 
        } 
       default: 
        { 
         Console.WriteLine("Please Enter the Correct Input"); 
         break; 
        } 
       } 
      } 
     } 
    } 
} 

但是,再次,這不是一個很好的例子,因爲它不清楚你想要做什麼。你可以添加到上面的代碼,使其更像面向對象是使用工廠來確定哪種類型的car創建,然後你只是有一輛車。我希望這回答了你的問題。

+0

謝謝,真的幫了我很多: ) – Geek 2012-08-14 13:53:25

相關問題