Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Challenge: Creating a Vehicle Hierarchy | Ereditarietà
Programmazione Orientata Agli Oggetti in Python
Sezione 2. Capitolo 3
single

single

bookChallenge: Creating a Vehicle Hierarchy

Scorri per mostrare il menu

Compito

Scorri per iniziare a programmare

Define a class hierarchy for different types of vehicles:

  • Create a base class named Vehicle with an __init__ method that takes brand and speed as arguments.
  • Add a method called get_info to Vehicle that returns the brand and speed.
  • Create a subclass Car that inherits from Vehicle and adds a doors attribute.
  • Create a subclass Bike that inherits from Vehicle and adds a type attribute (such as 'mountain' or 'road').
  • Override the get_info method in both Car and Bike to include their extra data (doors for cars, type for bikes).

Expected output format:

# Vehicle
Brand: <brand>, Speed: <speed>
# Car
Brand: <brand>, Speed: <speed>, Doors: <doors>
# Bike
Brand: <brand>, Speed: <speed>, Type: <type>

Soluzione

Switch to desktopCambia al desktop per esercitarti nel mondo realeContinua da dove ti trovi utilizzando una delle opzioni seguenti
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 3
single

single

Chieda ad AI

expand

Chieda ad AI

ChatGPT

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

some-alt