4

正如你们从代码中看到的那样,我创建了三类汽车,新模型,主要。所有基本方法都在汽车类中,我创建了继承类来尝试(继承)。正如你所看到的,我正在做的只是输出 car 类的 wheel() 方法和 newmodel 继承类的 newrims() 方法来做一个完整的句子。需要建议使代码更准确。

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;
                    }
                }
            }
        }
    }
}
4

3 回答 3

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会调用轮子功能,但取决于您的车是普通旧车还是新车型,它会在控制台上打印不同的内容。

于 2012-08-14T13:37:52.160 回答
1

删除所有静态...从类中创建 Car 和/或 NewModel 实例并使用这些实例。

我没有实际的参考资料,但是需要考虑一些事项:

  • 如果您在大多数功能上使用静态,则可能是您犯了设计缺陷。
  • 您已经创建了一个很好的基类和继承类
  • 而不是使用静态函数,而是创建一个汽车对象(即 currentCar = new Car() 并创建一个 newModel 实例)
  • 在这些实例上使用函数而不是类,然后您可以删除静态关键字。
  • 当使用单个变量(即 currentVehicle,您可以从汽车创建:即 currentVehicle = new car())时,您可以稍后将其更改为新模型并使用新模型的函数,例如 currentVehicle = new newmodel()
  • 通常类是用大写的,所以 Car 和 NewModel,以及没有大写的类的变量/实例:即 car = Car(), newModel = NewModel()
于 2012-08-14T13:37:11.627 回答
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; 
                    } 
                } 
            } 
        } 
    } 
}

但是,总的来说,这并不是一个很好的例子,因为不清楚您要做什么。您可以在上面的代码中添加一些使其更像 OO 的东西是使用工厂来确定car要创建哪种类型,然后您就可以拥有一辆汽车。我希望这回答了你的问题。

于 2012-08-14T13:44:49.713 回答