C#中的部分类没有意义 [英] Partial Class in C# not wokring

查看:90
本文介绍了C#中的部分类没有意义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第二个文件:Class1.cs



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



public partial class TestClass
{

public TestClass( int x, int y)
{

_a = x;
_b = y;
}


public int A
{


get
{
return _a;
}


set {_b = value ; }
}

public int B { get { return _b; } set {_ b = value ; }




}





第一档:程序.cs

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



partial class TestClass
{
private int _a,_b;
public TestClass()
{
Console.WriteLine( 测试类默认构造函数);

}

public void 添加()
{
Console.WriteLine( 加法方法);
Console.WriteLine(_a + _b);

}


public void Sub()
{
Console.WriteLine( 减法方法) ;
Console.WriteLine(_a - _b);

}

}

class 计划
{
static void Main( string [] args)
{

TestClass t = new TestClass();

t.A = 10 ;

t.B = 20 ;

t.Add();
t.Sub();


}
}





我想了解部分类概念我写了简单的程序来添加和减去它的所有工作,但我总是得到输出有20和-20 ..我不知道为什么我得到相同的输出我尝试更改数字仍然相同

解决方案

这只是一个错字。在 A 属性中,您分配给 _b 而不是 _a


Second file: Class1.cs

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



public partial class TestClass
{

    public TestClass(int x, int y)
    {

        _a = x;
        _b = y;
    }


    public int A
    {


        get
        {
            return _a;
        }


        set { _b = value; }
    }

    public int B { get { return _b; } set { _b = value; } }




}



First file: Program.cs

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



partial class TestClass
{
    private int _a, _b;
    public TestClass()
    {
        Console.WriteLine("Test Class Default Constructor");

    }

    public void Add()
    {
        Console.WriteLine("Addition Method");
        Console.WriteLine(_a+_b);

    }


    public void Sub()
    {
        Console.WriteLine("Subtraction Method");
        Console.WriteLine(_a - _b);

    }

}

    class Program
    {
        static void Main(string[] args)
        {

            TestClass t = new TestClass();

            t.A=10;

            t.B = 20;

            t.Add();
            t.Sub();


        }
    }



I am trying to understand the partial class concepts i have written simple program to add and subtract it all works but i always get the output has 20 and -20 ..I don't know why do i get the same output i have tried changing the numbers still the same

解决方案

It's just a typo. In the A property you assign to _b instead of _a.


这篇关于C#中的部分类没有意义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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