简体   繁体   English

C#中的简单多态

[英]Simple polymorphism in C#

This program is about polymorphism in C#.这个程序是关于 C# 中的多态性的。 But call method does not working in visual studio.但是调用方法在 Visual Studio 中不起作用。 The program give error like "The name 'Call' does not exist in the current context".程序给出错误,如“当前上下文中不存在名称'Call'”。

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);您必须知道来自哪个类的静态方法 - 即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 .进程有Call ,没有polymorphism So, call Call by referring process .因此,通过引用process调用Call

  2. and 3. You cannot access a method outside class if you haven't defined an access level.和 3. 如果您没有定义访问级别,您将无法访问类外的方法。 So, I added public as we need to access Call outside the process class.因此,我添加了public因为我们需要在process类之外访问Call

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.这意味着当你不在process ,你不能在不指定类的情况下调用Call
  2. The Call methods have no explicit visibility, that means they are private by default and as such not callable outside process . Call方法没有明确的可见性,这意味着它们默认是private的,因此不能在process外部调用。
  3. There is nothing polymorphic in your program (not even cat and dog ).您的程序中没有任何多态性(甚至catdog也不行)。 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 .最重要的是,您的方法是static There is no polymorphism with static methods.静态方法没有多态性。

Your issued 1 and 2 are resolved like this:您发出的 1 和 2 是这样解决的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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