C# method overriding 🙅
HTML-код
- Опубликовано: 19 окт 2024
- C# method overriding tutorial example explained
#C# #method #override
using System;
namespace MyFirstProgram
{
class Program
{
static void Main(string[] args) {
//method overriding = provides a new version of a method inherited from a parent class
// inherited method must be: abstract, virtual, or already overriden
// Used with ToString(), polymorphism
Dog dog = new Dog();
Cat cat = new Cat();
dog.Speak();
cat.Speak();
Console.ReadKey();
}
}
class Animal
{
public virtual void Speak()
{
Console.WriteLine("The animal goes *brrr*");
}
}
class Dog : Animal
{
public override void Speak()
{
Console.WriteLine("The dog goes *woof*");
}
}
class Cat : Animal
{
}
}
using System;
namespace MyFirstProgram
{
class Program
{
static void Main(string[] args) {
//method overriding = provides a new version of a method inherited from a parent class
// inherited method must be: abstract, virtual, or already overriden
// Used with ToString(), polymorphism
Dog dog = new Dog();
Cat cat = new Cat();
dog.Speak();
cat.Speak();
Console.ReadKey();
}
}
class Animal
{
public virtual void Speak()
{
Console.WriteLine("The animal goes *brrr*");
}
}
class Dog : Animal
{
public override void Speak()
{
Console.WriteLine("The dog goes *woof*");
}
}
class Cat : Animal
{
}
}
so short and effective!! thank you for not wasting my time!
Wow! I've spent more than 15 minutes listening to a video on my C# course about it, I almost fell asleep and absolutely didn't get how method overriding works, BUT 2 minutes of your video and I get it so clear. Thank you very much!
Thank you! This clarifies my confusion as to when and the difference that a method can be overriden if whether its from a regular class or abstract class.
bro,.. great videos, so simple and straight to the point. good review!
Very good explanations!
Thanks for sharing.
Wonderful tutorial, great thanks.
Thanks for the explanation
bro is always saving me for my assignments in the shortest amount of time
Super effective !! Thank you !
VRO thanks Short and Effective just perfect
3:30 reminded me of gin ibushi hahaa
but nice video 😁👍
very helpful video. thanks
very good vid, to the point and very good explanation.
Thank you
Simple and helpful
you are such a bro, bro.
This is that good that I saved around 100 lines of code
Keep the content coming.
Great vid bro, you helped me understand c# :D
Thanks for the video Bro.
great video
The thumbnails always get me x)
Thanks
Thanks bro!
yea maaaaaaaaan , super helpful
thanks
❣
❤
good vid bro
broooooooooo amazing
nice
What does the catdog go?
My compiler says the derived class has to use either override or new? What is new used for in this context?
What about new Keyword what happens when parent method is virtual and child is new
what does the fox say?
lesson check😇
woof
But what does the fox say?
so whats the point of overriding? Why not just create a new method that just prints a line?
Ill put my comment here so in case someone smarter than me comes along and gives an explanation, I can read it too
The point of overriding in this case is that with his structure all animals that inherit from the base class have a method Speak() which creates uniformity. when writing the base class, an animal that is not specific makes a **brr** noise.
A possibly better example is if you have a game, for instance,
with a base class: Sword that has the method Attack() that has a 20% chance to hit,
and an inherited class: MagicSword.
with this setup all Swords and MagicSwords can have the method Attack() called,
but what if when attacking, the magic sword always hits because *magic*.
well that is a good use for overrides as you can override the Sword implementation of Attack() and you would still be able to treat it as a sword as shown below.
If you instead had individual Attack() methods, then the base class cannot have a method named Attack(), so any generic swords would be unable to attack.
**EXAMPLE**
using System;
namespace Example
{
class Program
{
static void Main(string[] args)
{
List mySwords = new List();
mySwords.Add(new Sword());
mySwords.Add(new MagicSword());
foreach (var sword in mySwords)
{
sword.Attack();
}
Console.ReadKey();
}
}
class Sword
{
public virtual void Attack()
{
Random random = new Random();
float chanceToHit = random.NextSingle();
if (chanceToHit > 0.2f)
{
Console.WriteLine("Success!");
}
else
{
Console.WriteLine("You Missed!");
}
}
}
class MagicSword : Sword
{
public override void Attack()
{
Console.WriteLine("Success!");
}
}
}
a random comment down below
Why does it still work without putting override and virtual
my guess is that you create a new method (with the same name) within that object, which is still valid and will work but it's not a good idea
Random Comment 🤠👍
it is useless here u could type it directly without virtual , override right ?
Not useless. For example, you need a method for many objects, that derive from one class. But SOME of these objects are exceptions and each of them needs a different method
🗿🗿🗿🗿🗿 Done!
1
A random comment down below.
Random Comment Down Below!!!
thank you
Thanks Bro!
thanks