4

コードからわかるように、car、new model、main の 3 つのクラスを作成しました。すべての基本的なメソッドは車のクラスにあります。継承されたクラスを作成して試してください(継承)。ご覧のとおり、私がしていることは、自動車クラスの 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インスタンスを作成し、インスタンスを使用します。

実際の参照はありませんが、考慮すべき点がいくつかあります。

  • ほとんどの関数でstaticを使用している場合は、おそらく設計上の欠陥があります。
  • あなたはすでに基本クラスを作成し、良いクラスを継承しました
  • 静的関数を使用する代わりに、carオブジェクトを作成します(つまり、currentCar = new Car()を作成し、newModelインスタンスも作成します)
  • クラスの代わりにそれらのインスタンスの関数を使用すると、静的キーワードを削除できます。
  • 1つの変数を使用する場合(つまり、車から作成できる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; 
                    } 
                } 
            } 
        } 
    } 
}

しかし、繰り返しになりますが、これは全体としてあまり良い例ではありません。なぜなら、何をしようとしているのかが明確ではないからです。上記のコードに追加してオブジェクト指向のようにすることができるのは、ファクトリを使用しcarて作成するタイプを決定することです。そうすれば、車ができます。これがあなたの質問に答えることを願っています。

于 2012-08-14T13:44:49.713 に答える