0
Help with polymorphism
I need help with this exercise. In a turn-based strategy game, each unit can attack. The standard unit attacks with a sword. But there are two more types of units - musketeers and mages, who attack in their own way. The program you are given declares a Unit class that has an Attack () method. This generates "Using sword!". Derive the Musketeer and Magician classes from the Unit class and override their Attack () method to generate the corresponding messages while attacking: Musketeer => "Using musket!" Magician => "Using magic!" Don't forget about the virtual keyword, which allows you to override the method in derived classes. This is my code: https://code.sololearn.com/cSFPE5IV5QIn/?ref=app
8 Respostas
+ 2
Alex
you just need to add the complete string that you are looking for.So do like this
"Using sword!" for all of it. Your code is working !!
+ 2
class Unit
{
public virtual void Attack()
{
Console.WriteLine("Using sword!");
}
}
/*derive the class from Unit class
and override Attack() method*/
class Musketeer : Unit
{
public override void Attack()
{
Console.WriteLine("Using musket!");
}
}
/*derive the class from Unit class
and override Attack() method*/
class Magician : Unit
{
public override void Attack()
{
Console.WriteLine("Using magic!");
}
}
}
+ 1
I have no idea about this exercise but what output you are looking for ?
+ 1
the test expect "Using musket!" and "Using magic!", but you forgot "Using " in derived class attack output:
Console.WriteLine("Using musket!");
Console.WriteLine("Using magic!");
0
In the description of the exercise is the result that should have
0
{ :: ( [ " JRamoneMH " ] ) ;; }; Thank you very much for helping me see what I was missing
0
visph Thanks, I was missing only that detail. And thanks also for helping me
0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
static void Main(string[] args)
{
Unit unit1 = new Unit();
Unit musketeer = new Musketeer();
Unit magician = new Magician();
unit1.Attack();
musketeer.Attack();
magician.Attack();
}
}
class Unit
{
public virtual void Attack()
{
Console.WriteLine("Using sword!");
}
}
/*derive the class from Unit class
and override Attack() method*/
class Musketeer : Unit
{
public override void Attack()
{
Console.WriteLine("Using musket!");
}
}
/*derive the class from Unit class
and override Attack() method*/
class Magician : Unit
{
public override void Attack()
{
Console.WriteLine("Using magic!");
}
}
}