Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Sfida: Pratica con i Modificatori | Fondamenti Di OOP
C# Oltre le Basi

bookSfida: 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:

  1. Facendo sì che sia Student che Teacher ereditino dalla classe Person;
  2. Assicurandoti che il campo name sia accessibile nelle classi figlie ma inaccessibile da qualsiasi altra parte.
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. In questo esercizio è necessario prima utilizzare il concetto di classi derivate, poi i modificatori di accesso.
  2. Si utilizza il simbolo : per specificare la classe genitore nella definizione di una classe figlia (derivata).
  3. Ricorda quale modificatore di accesso è appropriato per il campo name per nasconderlo all'esterno della classe ma renderlo accessibile alle classi figlie.
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(); } }
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 4. Capitolo 4

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Suggested prompts:

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

bookSfida: 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:

  1. Facendo sì che sia Student che Teacher ereditino dalla classe Person;
  2. Assicurandoti che il campo name sia accessibile nelle classi figlie ma inaccessibile da qualsiasi altra parte.
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. In questo esercizio è necessario prima utilizzare il concetto di classi derivate, poi i modificatori di accesso.
  2. Si utilizza il simbolo : per specificare la classe genitore nella definizione di una classe figlia (derivata).
  3. Ricorda quale modificatore di accesso è appropriato per il campo name per nasconderlo all'esterno della classe ma renderlo accessibile alle classi figlie.
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(); } }
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 4. Capitolo 4
some-alt