简体   繁体   中英

Simple polymorphism in C#

This program is about polymorphism in C#. But call method does not working in visual studio. The program give error like "The name 'Call' does not exist in the current context".

class dog
{
}
class cat
{
}
class process
{
    static void Call(dog d)
    {
        Console.WriteLine("dog is called");
    }
    static void Call(cat c)
    {
        Console.WriteLine("cat is called");
    }
}
class polymorphism
{
    public static void Main()
    {
        dog dog = new dog();
        cat cat = new cat();
        Call(dog);
    }

}

You have to tell from which class is the static method coming from - that is process.Call(dog); . That should make it work

class process
{
    public static void Call(dog d) //2
    {
        Console.WriteLine("dog is called");
    }
    public static void Call(cat c) //3
    {
        Console.WriteLine("cat is called");
    }
}
class polymorphism
{
    public static void Main()
    {
        dog dog = new dog();
        cat cat = new cat();
        process.Call(dog); //1
    }
}

What changes I've done?

  1. process has Call , not polymorphism . So, call Call by referring process .

  2. and 3. You cannot access a method outside class if you haven't defined an access level. So, I added public as we need to access Call outside the process class.

Hope it helped.

There are some issues with your program:

  1. You are not working with functions, but with methods. That means when younare not within process , you can not invoke Call without specifying the class.
  2. The Call methods have no explicit visibility, that means they are private by default and as such not callable outside process .
  3. There is nothing polymorphic in your program (not even cat and dog ). What you are doing is to verify that overloading works. Overloading is no object oriented concept. It's syntactic sugar. On top of everything, your methods are static . There is no polymorphism with static methods.

Your issued 1 and 2 are resolved like this:

class process
{
    public static void Call(dog d)
    {
        Console.WriteLine("dog is called");
    }
    public static void Call(cat c)
    {
        Console.WriteLine("cat is called");
    }
}
class polymorphism
{
    public static void Main()
    {
        dog dog = new dog();
        cat cat = new cat();
        process.Call(dog);
    }
 }
using System;

namespace ConsoleApp
{
    class Program
    {
        static void Main()
        {
            var dog = new Dog();
            var cat = new Cat();
            var whale = new Whale();

            var c = new Caller();

            c.CallFeature(dog);
            c.CallFeature(cat);
            c.CallFeature(whale);

            Console.ReadKey();
        }
    }

    class Cat : Animal
    {
        public override string Feature()
        {
            return "sharp claws";
        }
    }

    class Dog : Animal
    {
        public override string Feature()
        {
            return "big teeth";
        }
    }

    class Whale : Animal
    {

    }

    class Animal
    {
        public virtual string Feature()
        {
            return "unknown features";
        }
    }

    class Caller
    {
        public void CallFeature(Animal a)
        {
            Console.WriteLine("a {0} has {1}", a.GetType().Name, a.Feature());
        }
    }
}

 //polymorphism class dog { } class cat { } class process { static void Call(dog d) { Console.WriteLine("dog is called"); } static void Call(cat c) { Console.WriteLine("cat is called"); } public static void Main() { dog dog = new dog(); cat cat = new cat(); Call(dog); Console.ReadLine(); } }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM