Output based on the basic Java of C # done in the past
One of the convenient functions of a class is that you can put an instance of a derived class in a variable of the base class.
I heard that there is a development group that only attaches finals and that MicroSoft made it.
namespace ShootingGame
{
class Enemy
{
public int hp;
public virtual void Move() // public abstract void Move()
{
}
}
}
Since the abstract that is also in Java is (unimplemented class), new cannot be done. It is important to "override and write the contents at the inherited destination"!
I feel that it is possible to access from anywhere because many merits can be inherited.
Based on
Enemy.cs
namespace ShootingGame
{
class UFO:Enemy
{
public override void Move()
{
Console.WriteLine("Move in a zigzag");
}
}
}
Meteor.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShootingGame
{
class Meteor:Enemy //Create a Meteor class by inheriting the Enemy class
{
//Override the Move method of the Enemy class
public override void Move()
{
Console.WriteLine("Move straight");
}
}
}
Star.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShootingGame
{
class Star:Enemy
{
public override void Move()
{
Console.WriteLine("Move while rotating");
}
}
}
UFO.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShootingGame
{
class UFO:Enemy
{
public override void Move()
{
Console.WriteLine("Move in a zigzag");
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShootingGame
{
class Program
{
static void Main(string[] args)
{
UFO ufo;
Star[] stars = new Star[2];
Meteor[] meteors = new Meteor[2];
//Create an instance
ufo = new UFO();
stars[0] = new Star();
stars[1] = new Star();
meteors[0] = new Meteor();
meteors[1] = new Meteor();
//Move the enemy
ufo.Move();
for (int i = 0; i < stars.Length; i++)
{
stars[i].Move();
}
for (int i = 0; i < meteors.Length; i++)
{
meteors[i].Move();
}
}
}
}
Recognize useful functions such as
Recommended Posts