我的重载示例有什么错误? [英] What is the error of my overloading example?

查看:62
本文介绍了我的重载示例有什么错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我的重载示例有什么错误?



 命名空间 MethodOverloading 
{
class 程序
{
public class TestOverloading
{
public void 添加(字符串 a,字符串 b)
{
Console.WriteLine( 添加两个字符串: + a + + b);
}
public void 添加( int a1, int a2)
{
Console.WriteLine( 添加两个整数: + a1 + a2);
}

}
静态 void Main( string [] args)
{
TestOverloading obj = new TestOverloading();
obj.Add( Ankit Agarwal);
obj.Add( 10 5 );
Console.ReadLine();
}
}
}





输出即将到来: -

添加两个字符串:Ankit Agarwal

添加两个整数:105



我想添加两个整数值。

为什么输出不正确?



请帮帮我。



先谢谢。< br $>


Ankit Agarwal

软件工程师

解决方案

将行更改为;

 Console.WriteLine(添加两个整数:+(a1 + a2)); 



否则第一次添加发生在Adding Two Integer:和 a1 之间,结果是另一个字符串。然后当你将 a2 添加到你再连接时。



希望这会有所帮助,

Fredrik


试试这个



  int  c = a1 + a2; 
Console.WriteLine( 添加两个整数: + c);


Hello,
What is the error of my overloading example?

namespace MethodOverloading
{
    class Program
    {
        public class TestOverloading
        {
            public void Add(string a, string b)
            {
                Console.WriteLine("Adding Two String :" + a + " " + b);
            }
            public void Add(int a1, int a2)
            {
                Console.WriteLine("Adding Two Integer :" + a1 + a2);
            }

        }
        static void Main(string[] args)
        {
            TestOverloading obj = new TestOverloading();
            obj.Add("Ankit", "Agarwal");
            obj.Add(10,5);
            Console.ReadLine();
        }
    }
}



Output is coming:-
Adding Two String :Ankit Agarwal
Adding Two Integer :105

I want to add two integer value.
why is not coming correct output?

Please help me.

Thanks in Advance.

Ankit Agarwal
Software Engineer

解决方案

Change the line to;

Console.WriteLine("Adding Two Integer :" + (a1 + a2));


Otherwise the first addition that takes place is between "Adding Two Integer :" and a1, the result of which is another string. Then when you add a2 to that you're concatenating again.

Hope this helps,
Fredrik


Try this

int c = a1 + a2;
Console.WriteLine("Adding Two Integer :" + c);


这篇关于我的重载示例有什么错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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