Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ チャレンジ:修飾子の練習 | OOPの基本
C#オブジェクト指向構造

bookチャレンジ:修飾子の練習

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

Personという名前のクラスがあり、nameを格納するフィールドがあります。さらに、StudentTeacherという2つのクラスがPersonから派生しています。

プログラムにはいくつかの不完全なコードがあり、エラーが発生しています。次の内容でコードを完成させてください:

  1. StudentTeacherの両方がPersonクラスを継承するようにすること。
  2. nameフィールドが子クラスからアクセス可能であり、その他の場所からはアクセスできないようにすること。
index.cs

index.cs

copy
12345678910111213141516171819202122232425262728293031323334353637383940414243444546
using System; public class Person { // Field to store the name string name; } public class Student { public Student(string name) { this.name = name; } public void Study() { Console.WriteLine($"{name} is studying."); } } public class Teacher { public Teacher(string name) { this.name = name; } public void Teach() { Console.WriteLine($"{name} is teaching."); } } public class Program { public static void Main(string[] args) { Teacher t = new Teacher("Hannah"); Student s = new Student("Mark"); t.Teach(); s.Study(); } }
  1. この課題では、まず派生クラスの概念を使用し、その後アクセス修飾子を使用します。
  2. 子(派生)クラスの定義で親クラスを指定するには、:記号を使用します。
  3. nameフィールドをクラス外部から隠し、子クラスからアクセス可能にするために適切なアクセス修飾子を思い出してください。
index.cs

index.cs

copy
123456789101112131415161718192021222324252627282930313233343536373839404142434445
using System; public class Person { // Field to store the name protected string name; } public class Student : Person { public Student(string name) { this.name = name; } public void Study() { Console.WriteLine($"{name} is studying."); } } public class Teacher : Person { public Teacher(string name) { this.name = name; } public void Teach() { Console.WriteLine($"{name} is teaching."); } } public class Program { public static void Main(string[] args) { Teacher t = new Teacher("Hannah"); Student s = new Student("Mark"); t.Teach(); s.Study(); } }
すべて明確でしたか?

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

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

セクション 4.  4

AIに質問する

expand

AIに質問する

ChatGPT

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

セクション 4.  4
some-alt