继承类输出的替代或好方法

时间:2012-08-14 13:26:35

标签: c#

正如你们可以从代码中看到我创建的三个类汽车,新模型,主要。所有基本方法都在car class中,我创建了继承类来尝试(继承)。你可以看到我正在做的只是用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 个答案:

答案 0 :(得分: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将调用滚轮功能,但根据您的汽车是普通旧车还是新车型,它会向控制台打印不同的东西。

答案 1 :(得分:1)

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

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

  • 如果你在大多数功能上使用静态,可能是你设计了一个缺陷。
  • 你已经创建了一个基类并且继承了很好的类
  • 而不是使用静态函数,创建一个汽车对象(即currentCar = new Car()并创建一个newModel实例)
  • 使用这些实例上的函数而不是类,然后您可以删除静态关键字。
  • 当使用一个单一变量(即currentVehicle,您可以从汽车创建:即currentVehicle = new car())时,您可以稍后将其更改为新模型并使用新模型的函数,如currentVehicle = new newmodel( )
  • 通常类是用大写写的,所以Car和NewModel,以及没有大写的类的变量/实例:ie car = Car(),newModel = NewModel()

答案 2 :(得分: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,然后您就可以拥有一辆汽车。我希望这能回答你的问题。