I tried to chew C # (polymorphism: polymorphism)

Output based on the basic Java of C # done in the past

Polymorphism: polymorphism

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.

Supplement 1

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()
        {
        }
    }
}

Supplement 2

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

I tried to chew C # (polymorphism: polymorphism)
I tried to chew C # (indexer)
I tried to chew C # (basic of encapsulation)
I tried to chew C # (reading and writing files)
I tried to summarize iOS 14 support
I tried to interact with Java
I tried to explain the method
I tried to summarize Java learning (1)
I tried to understand nil guard
I tried to explain Active Hash
I tried to summarize the methods used
I tried to introduce CircleCI 2.0 to Rails app
I tried migrating Processing to VS Code
I tried to summarize Java lambda expressions
I tried to get started with WebAssembly
I tried to solve AOJ's Binary Search
I tried to implement the Iterator pattern
I tried to summarize the Stream API
I tried to build AdoptOpenjdk 11 on CentOS 7
What is Docker? I tried to summarize
I tried to build Ruby 3.0.0 from source
I tried to use Selenium like JQuery
I tried to touch JavaScript Part.2 Object-oriented
I tried to implement ModanShogi with Kinx
I tried Spring.
I tried tomcat
I tried youtubeDataApi.
I tried refactoring ①
I tried FizzBuzz.
I tried JHipster 5.1
I tried to summarize about JVM / garbage collection
I tried to verify AdoptOpenJDK 11 (11.0.2) with Docker image
I tried to make Basic authentication with Java
I tried to implement polymorphic related in Nogizaka.
[Rails] I tried to raise the Rails version from 5.0 to 5.2
I tried to manage login information with JMX
I tried to organize the session in Rails
java I tried to break a simple block
I tried to link grafana and postgres [docker-compose]
I tried to develop a man-hour management tool
I tried to develop a DUO3.0 study website.
I also tried WebAssembly with Nim and C
I tried to implement deep learning in Java
[Must see !!!] I tried to summarize object orientation!
I tried to create a LINE clone app
I tried to build AdoptOpenJDK 8 (Addition: Amazon Corretto 8)
I tried to output multiplication table in Java
I tried to link JavaFX and Spring Framework.
I tried to set tomcat to run the Servlet.
I tried to develop an application in 2 languages
I tried to develop a website to record expenses.
I tried to implement a server using Netty
I tried to break a block with java (1)
I tried what I wanted to try with Stream softly.
I tried to move Hello World (+ α) written in C language from JavaScript [WebAssembly]
[I tried] Spring tutorial
I tried to implement file upload with Spring MVC
I tried to organize the cases used in programming
I tried to read and output CSV with Outsystems
I tried to implement TCP / IP + BIO with JAVA
I tried to implement Firebase push notification in Java