解决c#代码中的错误 [英] Resolve the bug in the c# code

查看:77
本文介绍了解决c#代码中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



以下是有错误的代码段。我想通过将类型参数传递给Hit()方法或任何东西来执行代码。



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;

命名空间 CatDog
{

class 程序
{
静态 void Main( string [] args)
{
Dog dog = new Dog();
Cat cat = new Cat();
Human human = new Human();
human.Hit(cat);
human.Hit(dog);
Console.ReadLine();
}
}

class
{
public void 运行()
{
Console.WriteLine( 狗跑);
}
}
class Cat
{
public void 运行()
{
Console.WriteLine( Cat运行);
}
}

class 人类
{
public void 点击(_____ x)
{

x.Run();
}
}
}

解决方案

我在OriginalGriff使用摘要中包含了这个变体OP信息的类:它演示了利用这样一个事实:你可以委托给抽象类一些所有继承者将使用/需要的功能实现。



< b>我鼓励任何读这篇文章的人投票支持OriginalGriff或Karthik的优秀答案,而不是这种变化。



  public   abstract   class 动物
{
public abstract void AnimalHit();

// 我们可以在这里实现一个方法
< span class =code-comment> // 继承人可以直接使用
public void AnimalRun( string AnimalRunString)
{
Console.WriteLine(AnimalRunString) );
}
}

public class 狗:动物
{
公共 覆盖 void AnimalHit()
{
AnimalRun( dog runs);
}
}

public class Cat:动物
{
公共 覆盖 void AnimalHit()
{
AnimalRun( cat runs);
}
}

public class Human
{
public void Hit(Animal theAnimal)
{
theAnimal.AnimalHit();
}
}

你会注意到这里不需要使用base.AnimalRun(),但使用'base将起作用。


有几种方法可以做到这一点。第一种是传递对象

  public   void 点击(对象 x)
{
}

但这有很大的问题 - 你必须明确检查输入参数是什么类型并适当处理它:

  public   void 点击(对象 x)
{
Cat c = x as Cat;
if (c!= null
{
c。跑();
}
狗d = x as Dog;
if (d!= null
{
d。跑();
}
}

这不是一个好方法,而且很难扩展,而且容易出错。



更好的方法是创建基类:

  public   abstract   class 动物
{
public < span class =code-keyword> abstract void Run();
}

(我们将其声明为 abstract ,以便您无法创建它的实例)

然后从中得出每个动物类:

公共类Dog:Animal 
{
public override void Run()
{
Console.WriteLine(Dog Runs);
}
}
公共类Cat:动物
{
公共覆盖void Run()
{
Console.WriteLine(Cat Run );
}
}

覆盖关键字告诉系统这将使用Cat或Dog特定版本替换基类版本)

然后你可以声明你的Hit方法来获取Animal,系统将选择使用哪种Run方法:

 < span class =code-keyword> public   class  Human 
{
public void Hit(Animal x)
{
x.Run();
}
}


HI



你应该指定一个在参数中键入 ..

如果你指定为Dog,你只能访问Dog Class Run Method,而不是Cat类和副反之亦然,

要修复此问题,您必须使用Common方法创建一个接口 Run()

并在 DOG 和<$ c $中实施界面 c> Cat Classes。

然后你尝试用任何实现的动物来调用Human class Run方法 IAnimal 界面..



如果是面试问题,答案如上。







试试这样..



< pre lang =c#> 使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;

命名空间 CatDog
{

class 程序
{
静态 void Main( string [] args)
{
Dog dog = new Dog();
Cat cat = new Cat();
Human human = new Human();
human.Hit(cat);
human.Hit(dog);
Console.ReadLine();
}
}

interface IAnimal { void Run();}
class Dog:IAnimal
{
public void 运行()
{
Console.WriteLine( Dog Runs);
}
}
class Cat:IAnimal
{
public void 运行()
{
Console.WriteLine( Cat运行);
}
}

class 人类
{
public void Hit(IAnimal x)
{

x.Run();
}
}
}


Hi All,

Below is a code snippet with errors. I want to execute the code, by passing a typed parameter to Hit() method or any thing.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CatDog
{

    class Program
    {
        static void Main(string[] args)
        {
            Dog dog = new Dog();
            Cat cat = new Cat();
            Human human = new Human();
            human.Hit(cat);
            human.Hit(dog);
            Console.ReadLine();
        }
    }

    class Dog
    {
        public void Run()
        {
            Console.WriteLine("Dog Runs");
        }
    }
    class Cat
    {
        public void Run()
        {
            Console.WriteLine("Cat Runs");
        }
    }

    class Human
    {
        public void Hit(_____ x)
        {
            
            x.Run();
        }
    }
}

解决方案

I am including this variation on OriginalGriff's use of an abstract Class for the OP's information: it demonstrates taking advantage of the fact that you can delegate to an abstract Class some implementation of features that all inheritors will use/need.

I encourage anyone reading this to vote for either OriginalGriff's or Karthik's excellent answers, not for this variation.

public abstract class Animal
{
    public abstract void AnimalHit();

    // we can implement a method here that
    // inheritors can use directly
    public void AnimalRun(string AnimalRunString)
    {
        Console.WriteLine(AnimalRunString);
    }
}

public class Dog: Animal
{
    public override void AnimalHit()
    {
        AnimalRun("dog runs");
    }
}

public class Cat: Animal
{
    public override void AnimalHit()
    {
        AnimalRun("cat runs");
    }
}

public class Human
{
    public void Hit(Animal theAnimal)
    {
        theAnimal.AnimalHit();
    }
}

You'll note that using base.AnimalRun("") is not required here, but using 'base would work.


There are a couple of ways you could do this. The first is to pass an object:

public void Hit(object x)
   {
   }

But this has big problems - you have to check explicitly what type teh incoming parameter is and deal with it appropriately:

public void Hit(object x)
   {
   Cat c = x as Cat;
   if (c != null)
      {
      c.Run();
      }
   Dog d = x as Dog;
   if (d != null)
      {
      d.Run();
      }
   }

That isn't a nice approach, and it's difficult to expand, and prone to errors.

The better approach would be to create a Base Class:

public abstract class Animal
    {
    public abstract void Run();
    }

(We declare it as abstract so that you can't create an instance of it)
And then derive each animal class from that:

public class Dog : Animal
    {
    public override void Run()
        {
        Console.WriteLine("Dog Runs");
        }
    }
public class Cat : Animal
    {
    public override void Run()
        {
        Console.WriteLine("Cat Runs");
        }
    }

(The override keyword tells the system this replaces the base class version with a Cat or Dog specific version)
You can then declare your Hit method to take an Animal, and the system will sort out which Run method to use:

public class Human
    {
    public void Hit(Animal x)
        {
        x.Run();
        }
    }


HI

You should specify a Type in the parameter..
if u specify as Dog , u can be able to access only Dog Class Run Method, not the Cat class and vice versa,
To fix this issue u have to create an interface with the Common method as Run()
and implement the interface in the DOG and Cat Classes.
then you try to call the Human class Run method with any Animals which is implemented by IAnimal Interface..

If it is an Interview question, the answer is the above.



Try like this..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CatDog
{

    class Program
    {
        static void Main(string[] args)
        {
            Dog dog = new Dog();
            Cat cat = new Cat();
            Human human = new Human();
            human.Hit(cat);
            human.Hit(dog);
            Console.ReadLine();
        }
    }

    interface IAnimal { void Run();}
    class Dog : IAnimal
    {
        public   void Run()
        {
            Console.WriteLine("Dog Runs");
        }
    }
    class Cat : IAnimal
    {
        public   void Run()
        {
            Console.WriteLine("Cat Runs");
        }
    }

    class Human
    {
        public void Hit(IAnimal x)
        {

            x.Run();
        }
    }
}


这篇关于解决c#代码中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆