Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Desafio: Encapsulamento | Princípios de POO
C# Além do Básico

bookDesafio: Encapsulamento

O código base possui duas classes chamadas Vehicle e Car. Alguns dos campos precisam ser ocultados enquanto outros devem ser expostos.

  • Ajustar a visibilidade dos campos type e modelYear em Vehicle para que não sejam acessíveis de fora da classe, incluindo classes derivadas;
  • Criar um novo método chamado getModelYear dentro da classe Vehicle para retornar o modelYear. Este método deve ser acessível de qualquer lugar. Este método permitirá que os usuários desta classe acessem o modelYear, mas não possam modificá-lo externamente;
  • As propriedades ownerName e fuel não devem ser acessíveis de nenhum lugar;
  • Criar um método chamado getFuel que retorna o valor de fuel;
  • Criar um método chamado addFuel que recebe um argumento do tipo float chamado fuel. Dentro do método, somar o valor de fuel à propriedade fuel (this.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. Os métodos getModelYear e getFuel são métodos de acesso (getters). Eles não recebem argumentos, são public e apenas retornam um valor.
  2. O método addFuel é um método de modificação (setter). Ele é public e recebe um argumento do tipo float chamado fuel. Para atualizar o valor de fuel, pode ser necessário utilizar a palavra-chave 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()}"); } }
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 5. Capítulo 7

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Suggested prompts:

Can you show me the base code for the `Vehicle` and `Car` classes?

Could you clarify which programming language this is for?

Do you need an explanation of how to implement field visibility in classes?

Awesome!

Completion rate improved to 2.04

bookDesafio: Encapsulamento

Deslize para mostrar o menu

O código base possui duas classes chamadas Vehicle e Car. Alguns dos campos precisam ser ocultados enquanto outros devem ser expostos.

  • Ajustar a visibilidade dos campos type e modelYear em Vehicle para que não sejam acessíveis de fora da classe, incluindo classes derivadas;
  • Criar um novo método chamado getModelYear dentro da classe Vehicle para retornar o modelYear. Este método deve ser acessível de qualquer lugar. Este método permitirá que os usuários desta classe acessem o modelYear, mas não possam modificá-lo externamente;
  • As propriedades ownerName e fuel não devem ser acessíveis de nenhum lugar;
  • Criar um método chamado getFuel que retorna o valor de fuel;
  • Criar um método chamado addFuel que recebe um argumento do tipo float chamado fuel. Dentro do método, somar o valor de fuel à propriedade fuel (this.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. Os métodos getModelYear e getFuel são métodos de acesso (getters). Eles não recebem argumentos, são public e apenas retornam um valor.
  2. O método addFuel é um método de modificação (setter). Ele é public e recebe um argumento do tipo float chamado fuel. Para atualizar o valor de fuel, pode ser necessário utilizar a palavra-chave 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()}"); } }
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 5. Capítulo 7
some-alt