Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ チャレンジ:エンカプセレーション | OOP原則
C#オブジェクト指向構造

bookチャレンジ:エンカプセレーション

メニューを表示するにはスワイプしてください

ベースコードには Vehicle クラスと Car クラスの2つのクラスが含まれています。一部のフィールドは隠す必要があり、一部は公開する必要があります。

  • type クラスの modelYear および Vehicle フィールドの可視性を調整し、派生クラスを含めてクラス外部からアクセスできないようにする;
  • getModelYear クラス内に Vehicle という新しいメソッドを作成し、modelYear を返すようにする。このメソッドはどこからでもアクセス可能とする。このメソッドにより、クラスの利用者は modelYear にアクセスできるが、外部から変更はできない;
  • ownerName および fuel プロパティはどこからもアクセスできないようにする;
  • getFuel メソッドを作成し、fuel の値を返すようにする;
  • addFuel メソッドを作成し、float 型の引数 fuel を受け取り、メソッド内で fuel の値をプロパティ fuelthis.fuel)に加算する。
index.cs

index.cs

copy
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
using System; class Vehicle { // Edit code below this line public string type; public int modelYear; // Edit code above this line // Create a method below this line // Create a method above this line public Vehicle(string type, int modelYear) { this.type = type; this.modelYear = modelYear; } } class Car : Vehicle { // Edit code below this line public string brandName; public string numberPlate; public string ownerName; public float fuel; // Edit code above this line // Create a method below this line // Create a method above this line public Car(int modelYear, string brandName, string numberPlate, string ownerName, float fuel) : base("Car", modelYear) { this.brandName = brandName; this.numberPlate = numberPlate; this.ownerName = ownerName; this.fuel = fuel; } } class Program { static void Main() { // Create an instance of Car Car myCar = new Car(2022, "Toyota", "ABC123", "John Doe", 50.0f); // Accessing properties and methods from Car class Console.WriteLine($"Brand: {myCar.brandName}"); Console.WriteLine($"Number Plate: {myCar.numberPlate}"); // Accessing getModelYear method from Vehicle class Console.WriteLine($"Model Year: {myCar.getModelYear()}"); // Accessing getFuel method from Car class Console.WriteLine($"Fuel: {myCar.getFuel()}"); // Adding fuel using addFuel method myCar.addFuel(10.0f); Console.WriteLine($"After adding fuel, new Fuel: {myCar.getFuel()}"); } }
  1. getModelYear および getFuel メソッドはゲッターメソッドです。引数を取らず、public であり、値のみを返します。
  2. addFuel メソッドはセッターメソッドです。public であり、float という1つの fuel 型引数を取ります。燃料値を更新するには this キーワードを使用する必要があります(this.fuel += fuel)。
index.cs

index.cs

copy
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
using System; class Vehicle { // Edit code below this line private string type; private int modelYear; // Edit code above this line // Create a method below this line public int getModelYear() { return modelYear; } // Create a method above this line public Vehicle(string type, int modelYear) { this.type = type; this.modelYear = modelYear; } } class Car : Vehicle { // Edit code below this line public string brandName; public string numberPlate; private string ownerName; private float fuel; // Edit code above this line // Create a method below this line public float getFuel() { return fuel; } public void addFuel(float fuel) { this.fuel += fuel; } // Create a method above this line public Car(int modelYear, string brandName, string numberPlate, string ownerName, float fuel) : base("Car", modelYear) { this.brandName = brandName; this.numberPlate = numberPlate; this.ownerName = ownerName; this.fuel = fuel; } } class Program { static void Main() { // Create an instance of Car Car myCar = new Car(2022, "Toyota", "ABC123", "John Doe", 50.0f); // Accessing properties and methods from Car class Console.WriteLine($"Brand: {myCar.brandName}"); Console.WriteLine($"Number Plate: {myCar.numberPlate}"); // Accessing getModelYear method from Vehicle class Console.WriteLine($"Model Year: {myCar.getModelYear()}"); // Accessing getFuel method from Car class Console.WriteLine($"Fuel: {myCar.getFuel()}"); // Adding fuel using addFuel method myCar.addFuel(10.0f); Console.WriteLine($"After adding fuel, new Fuel: {myCar.getFuel()}"); } }
すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 5.  7

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 5.  7
some-alt