2012-08-14 55 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