Sfida: Pratica con i Modificatori
Ti viene fornita una classe chiamata Person
con un campo per memorizzare il name
. Inoltre, ci sono due classi chiamate Student
e Teacher
che derivano da Person
.
Ci sono alcuni errori nel programma a causa di codice incompleto. Il tuo compito è completare il codice:
- Facendo sì che sia
Student
cheTeacher
ereditino dalla classePerson
; - Assicurandoti che il campo
name
sia accessibile nelle classi figlie ma inaccessibile da qualsiasi altra parte.
index.cs
12345678910111213141516171819202122232425262728293031323334353637383940414243444546using 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(); } }
- In questo esercizio è necessario prima utilizzare il concetto di classi derivate, poi i modificatori di accesso.
- Si utilizza il simbolo
:
per specificare la classe genitore nella definizione di una classe figlia (derivata). - Ricorda quale modificatore di accesso è appropriato per il campo
name
per nasconderlo all'esterno della classe ma renderlo accessibile alle classi figlie.
index.cs
123456789101112131415161718192021222324252627282930313233343536373839404142434445using 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(); } }
Grazie per i tuoi commenti!
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
What is the correct access modifier to use for the `name` field?
Can you show an example of how to define the inheritance for `Student` and `Teacher`?
Can you explain why the `name` field should not be public?
Awesome!
Completion rate improved to 2.04
Sfida: Pratica con i Modificatori
Scorri per mostrare il menu
Ti viene fornita una classe chiamata Person
con un campo per memorizzare il name
. Inoltre, ci sono due classi chiamate Student
e Teacher
che derivano da Person
.
Ci sono alcuni errori nel programma a causa di codice incompleto. Il tuo compito è completare il codice:
- Facendo sì che sia
Student
cheTeacher
ereditino dalla classePerson
; - Assicurandoti che il campo
name
sia accessibile nelle classi figlie ma inaccessibile da qualsiasi altra parte.
index.cs
12345678910111213141516171819202122232425262728293031323334353637383940414243444546using 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(); } }
- In questo esercizio è necessario prima utilizzare il concetto di classi derivate, poi i modificatori di accesso.
- Si utilizza il simbolo
:
per specificare la classe genitore nella definizione di una classe figlia (derivata). - Ricorda quale modificatore di accesso è appropriato per il campo
name
per nasconderlo all'esterno della classe ma renderlo accessibile alle classi figlie.
index.cs
123456789101112131415161718192021222324252627282930313233343536373839404142434445using 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(); } }
Grazie per i tuoi commenti!