Ejercicio
Clase Vehículos
Objectivo
Con Visual Studio, cree un proyecto y las clases correspondientes (con varios archivos) para este diagrama de clases. Cada clase debe incluir los atributos y métodos que se muestran en el diagrama, así como los métodos Get y Set para los métodos Vehicle y "Has" ("HasDualSlidingDoors") para MiniVan.
Debe crear también un programa de prueba, que creará un objeto perteneciente a cada clase y le dirá "Drive".
Ejemplo Ejercicio C#
Mostrar Código C#
// Import the System namespace for basic functionality
using System;
// Define the main class containing all vehicle-related classes
public class VehiclesDemo
{
// Define the base class Vehicle
public class Vehicle
{
// Declare a private field for the vehicle's speed
private int speed;
// Declare a private field for the vehicle's color
private string color;
// Define a public method to set the vehicle's speed
public void SetSpeed(int speed)
{
// Assign the speed parameter to the speed field
this.speed = speed;
}
// Define a public method to get the vehicle's speed
public int GetSpeed()
{
// Return the speed of the vehicle
return speed;
}
// Define a public method to set the vehicle's color
public void SetColor(string color)
{
// Assign the color parameter to the color field
this.color = color;
}
// Define a public method to get the vehicle's color
public string GetColor()
{
// Return the color of the vehicle
return color;
}
// Define a virtual method for driving
public virtual void Drive()
{
// Print a message indicating that the vehicle is driving
Console.WriteLine("The vehicle is driving.");
}
}
// Define the Car class, inheriting from Vehicle
public class Car : Vehicle
{
// Declare a private field for the number of doors in the car
private int numberOfDoors;
// Define a public method to set the number of doors
public void SetNumberOfDoors(int doors)
{
// Assign the doors parameter to the numberOfDoors field
numberOfDoors = doors;
}
// Define a public method to get the number of doors
public int GetNumberOfDoors()
{
// Return the number of doors
return numberOfDoors;
}
// Override the Drive method to specify that a car is driving
public override void Drive()
{
// Print a message indicating that the car is driving
Console.WriteLine("The car is driving.");
}
}
// Define the Truck class, inheriting from Vehicle
public class Truck : Vehicle
{
// Declare a private field for the load capacity of the truck
private int loadCapacity;
// Define a public method to set the load capacity
public void SetLoadCapacity(int capacity)
{
// Assign the capacity parameter to the loadCapacity field
loadCapacity = capacity;
}
// Define a public method to get the load capacity
public int GetLoadCapacity()
{
// Return the load capacity
return loadCapacity;
}
// Override the Drive method to specify that a truck is driving
public override void Drive()
{
// Print a message indicating that the truck is driving
Console.WriteLine("The truck is driving.");
}
}
// Define the MiniVan class, inheriting from Vehicle
public class MiniVan : Vehicle
{
// Declare a private field to indicate if the minivan has dual sliding doors
private bool hasDualSlidingDoors;
// Define a public method to set if the minivan has dual sliding doors
public void SetDualSlidingDoors(bool hasDoors)
{
// Assign the hasDoors parameter to the hasDualSlidingDoors field
hasDualSlidingDoors = hasDoors;
}
// Define a public method to check if the minivan has dual sliding doors
public bool HasDualSlidingDoors()
{
// Return true if the minivan has dual sliding doors
return hasDualSlidingDoors;
}
// Override the Drive method to specify that a minivan is driving
public override void Drive()
{
// Print a message indicating that the minivan is driving
Console.WriteLine("The minivan is driving.");
}
}
// Define the main entry point of the program
public static void Main()
{
// Create a Vehicle object, set attributes, and call the Drive method
Vehicle vehicle = new Vehicle();
vehicle.SetSpeed(50);
vehicle.SetColor("Red");
vehicle.Drive();
// Create a Car object, set attributes, and call the Drive method
Car car = new Car();
car.SetSpeed(80);
car.SetColor("Blue");
car.SetNumberOfDoors(4);
car.Drive();
// Create a Truck object, set attributes, and call the Drive method
Truck truck = new Truck();
truck.SetSpeed(60);
truck.SetColor("Black");
truck.SetLoadCapacity(1000);
truck.Drive();
// Create a MiniVan object, set attributes, and call the Drive method
MiniVan minivan = new MiniVan();
minivan.SetSpeed(70);
minivan.SetColor("White");
minivan.SetDualSlidingDoors(true);
minivan.Drive();
}
}
Salida
The vehicle is driving.
The car is driving.
The truck is driving.
The minivan is driving.
Código de Ejemplo Copiado!
Comparte este Ejercicio C# Sharp