如何将方法设为私有并从另一个类访问它?

时间:2015-07-17 00:12:44

标签: c# get scope set accessor

我很好奇如何将addOperands方法设为私有而不是公开,因为它现在被编码。我已经阅读了一些get和set访问器的例子,我仍然不理解这个概念。我如何在CalcEngine类中将addOperands方法设为私有,并且仍能从另一个类中使用此方法?

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

namespace Project3_WindowsCalculator
{
    class CalcEngine
{
    private int operationResult;

    public int addOperands(int operand1, int operand2) 
    {
        operationResult = operand1 + operand2;
        return operationResult;
    }

    public int subtractOperands(int operand1, int operand2)
    {
        operationResult = operand1 - operand2;
        return operationResult;
    }

    public int multiplyOperands(int operand1, int operand2)
    {
        operationResult = operand1 * operand2;
        return operationResult;
    }

    public int divideOperands(int operand1, int operand2)
    {
        operationResult = operand1 / operand2;
        return operationResult;
    }

}

}

以下是使用它的类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Project3_WindowsCalculator
{
public partial class MainWindow : Window

{
    CalcEngine c = new CalcEngine();
    bool addSelected;
    bool subtractSelected;
    bool multiplySelected;
    bool divideSelected;
    bool operationSelected;
    int operationResult;
    int operand1;
    int operand2;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void C_Click(object sender, RoutedEventArgs e)
    {
        txtDisplay.Text = "0";
        operationResult = 0;
        addSelected = false;
        subtractSelected = false;
        multiplySelected = false;
        divideSelected = false;
        operationSelected = false;
        operand1 = 0;
        operand2 = 0;
    }

    private void Multiply_Click(object sender, RoutedEventArgs e)
    {
        if (operand1 == 0 && operand2 == 0 && operationSelected == false) //operationSelected makes sure * cannot be pressed more than once at a time
        { //if + is clicked and nothing has been stored into operand1 yet
            Int32.TryParse(txtDisplay.Text, out operand1); //store the displayed text into the operand1 variable
        }
        else if (operand1 != 0 && operand2 == 0  && operationSelected == false)
        { //if + is clicked and both operand variables are stored with something
            Int32.TryParse(txtDisplay.Text, out operand2); //store what's on the display into the operand 2 variable
            if (addSelected)
            {
                operand1 = c.addOperands(operand1, operand2);
                addSelected = false;
            }
            else if (subtractSelected)
            {
                operand1 = c.subtractOperands(operand1, operand2);
                subtractSelected = false;
            }
            else if (multiplySelected)
            {
                operand1 = c.multiplyOperands(operand1, operand2);
                multiplySelected = false;
            }
            else if (divideSelected)
            {
                operand1 = c.divideOperands(operand1, operand2);
                divideSelected = false;
            }
            txtDisplay.Text = operand1.ToString();
            operand2 = 0; //empty the operand2 variable
        }
        operationSelected = true; multiplySelected = true;
    }

    private void Divide_Click(object sender, RoutedEventArgs e)
    {
        if (operand1 == 0 && operand2 == 0 && operationSelected == false)
        { //if + is clicked and nothing has been stored into operand1 yet
            Int32.TryParse(txtDisplay.Text, out operand1); //store the displayed text into the operand1 variable
        }
        else if (operand1 != 0 && operand2 == 0  && operationSelected == false)
        { //if + is clicked and both operand variables are stored with something
            Int32.TryParse(txtDisplay.Text, out operand2); //store what's on the display into the operand 2 variable
            if (addSelected)
            {
                operand1 = c.addOperands(operand1, operand2);
                addSelected = false;
            }
            else if (subtractSelected)
            {
                operand1 = c.subtractOperands(operand1, operand2);
                subtractSelected = false;
            }
            else if (multiplySelected)
            {
                operand1 = c.multiplyOperands(operand1, operand2);
                multiplySelected = false;
            }
            else if (divideSelected)
            {
                operand1 = c.divideOperands(operand1, operand2);
                divideSelected = false;
            }
            txtDisplay.Text = operand1.ToString();
            operand2 = 0; //empty the operand2 variable
        }
        operationSelected = true; divideSelected = true;

    }

    private void Subtract_Click(object sender, RoutedEventArgs e)
    {
        if (operand1 == 0 && operand2 == 0 && operationSelected == false)
        { //if + is clicked and nothing has been stored into operand1 yet
            Int32.TryParse(txtDisplay.Text, out operand1); //store the displayed text into the operand1 variable
        }
        else if (operand1 != 0 && operand2 == 0  && operationSelected == false)
        { //if + is clicked and both operand variables are stored with something
            Int32.TryParse(txtDisplay.Text, out operand2); //store what's on the display into the operand 2 variable
            if (addSelected)
            {
                operand1 = c.addOperands(operand1, operand2);
                addSelected = false;
            }
            else if (subtractSelected)
            {
                operand1 = c.subtractOperands(operand1, operand2);
                subtractSelected = false;
            }
            else if (multiplySelected)
            {
                operand1 = c.multiplyOperands(operand1, operand2);
                multiplySelected = false;
            }
            else if (divideSelected)
            {
                operand1 = c.divideOperands(operand1, operand2);
                divideSelected = false;
            }
            txtDisplay.Text = operand1.ToString();
            operand2 = 0; //empty the operand2 variable
        }
        operationSelected = true; subtractSelected = true;
    }

    private void Add_Click(object sender, RoutedEventArgs e)
    {
        if (operand1 == 0 && operand2 == 0 && operationSelected == false){ //if + is clicked and nothing has been stored into operand1 yet
            Int32.TryParse(txtDisplay.Text, out operand1); //store the displayed text into the operand1 variable
        }
        else if (operand1 != 0 && operand2 == 0  && operationSelected == false)
        { //if + is clicked and both operand variables are stored with something
            Int32.TryParse(txtDisplay.Text, out operand2); //store what's on the display into the operand 2 variable
            if (addSelected)
            {
                operand1 = c.addOperands(operand1, operand2);
                addSelected = false;
            }
            else if (subtractSelected)
            {
                operand1 = c.subtractOperands(operand1, operand2);
                subtractSelected = false;
            }
            else if (multiplySelected)
            {
                operand1 = c.multiplyOperands(operand1, operand2);
                multiplySelected = false;
            }
            else if (divideSelected)
            {
                operand1 = c.divideOperands(operand1, operand2);
                divideSelected = false;
            }
            txtDisplay.Text = operand1.ToString();
            operand2 = 0; //empty the operand2 variable
        }
        operationSelected = true; addSelected = true;
    }

    private void Equals_Click(object sender, RoutedEventArgs e)
    {
        Int32.TryParse(txtDisplay.Text, out operand2);

        if(addSelected){
            operationResult = c.addOperands(operand1, operand2);
            //operand1 = operationResult;
        }

        if(subtractSelected){
            operationResult = c.subtractOperands(operand1, operand2);
        }

        if(multiplySelected){
            operationResult = c.multiplyOperands(operand1, operand2);
        }

        if(divideSelected){
            operationResult = c.divideOperands(operand1, operand2);
        }

        txtDisplay.Text = operationResult.ToString();
        Int32.TryParse(txtDisplay.Text, out operand1);

        addSelected = false;
        subtractSelected = false;
        multiplySelected = false;
        divideSelected = false;
        operationSelected = false;
    }

    private void Seven_Click(object sender, RoutedEventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text == "0" || operationSelected ? "7" : txtDisplay.Text + "7";
        operationSelected = false;
    }

    private void Eight_Click(object sender, RoutedEventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text == "0" || operationSelected ? "8" : txtDisplay.Text + "8";
        operationSelected = false;
    }

    private void Nine_Click(object sender, RoutedEventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text == "0" || operationSelected ? "9" : txtDisplay.Text + "9";
        operationSelected = false;
    }

    private void Four_Click(object sender, RoutedEventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text == "0" || operationSelected ? "4" : txtDisplay.Text + "4";
        operationSelected = false;
    }

    private void Five_Click(object sender, RoutedEventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text == "0" || operationSelected ? "5" : txtDisplay.Text + "5";
        operationSelected = false;
    }

    private void Six_Click(object sender, RoutedEventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text == "0" || operationSelected ? "6" : txtDisplay.Text + "6";
        operationSelected = false;
    }

    private void One_Click(object sender, RoutedEventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text == "0" || operationSelected ? "1" : txtDisplay.Text + "1";
        operationSelected = false;
    }

    private void Two_Click(object sender, RoutedEventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text == "0" || operationSelected ? "2" : txtDisplay.Text + "2";
        operationSelected = false;
    }

    private void Three_Click(object sender, RoutedEventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text == "0" || operationSelected ? "3" : txtDisplay.Text + "3";
        operationSelected = false;
    }

    private void btn_0_Click(object sender, RoutedEventArgs e){}

    private void Off_Click(object sender, RoutedEventArgs e)
    {
        C_Click(sender, e);
        txtDisplay.Foreground = new SolidColorBrush(Colors.White);
    }

    private void On__Click(object sender, RoutedEventArgs e){}

    private void On_Click(object sender, RoutedEventArgs e)
    {
        C_Click(sender, e);
        txtDisplay.Foreground = new SolidColorBrush(Colors.Black);
    }

}
}

3 个答案:

答案 0 :(得分:1)

查看有关访问修饰符MSDN article。如果想要保护您的类免受外部程序集的影响,只需将其设为internal,但如果您想要不同的东西,我会提出一些建议。 (我不确定你需要什么,所以我现在发布了我的所有建议)

方法1:仅限Getter的属性

public class MyClass
{
    // Read-only access from outside, no 'set'
    public object RestrictedMember { get; private set; }
}

方法2:受保护的修饰符

使用此修饰符,只有派生类可以访问该成员。

public class BaseClass
{
    protected object RestrictedMember;
}

public DerivedClass : BaseClass
{
    public DerivedClass() : base()
    {
        // You can access base class's protected member
        RestrictedMember = new object();
    }
}

方法3:代表

对于方法,您可以声明自己的委托。

public delegate string MyDelegate(int a, int b);
public class MyClass
{
    // Also you should make it readonly or getter-only property if you don't want outside changes
    public MyDelegate restrictedMethod = _restrictedMethod;

    private string _restrictedMethod(int a, int b)
    {
        return (a + b).ToString();
    }
}

public class Test
{
    public void It(MyClass mc)
    {
        // You can call method and get result
        string sumAsStr = mc.restrictedMethod(3, 5);
    }
}

我没有测试过它们。可以修复一些小错误/语法错误。

答案 1 :(得分:0)

如果两个类都在同一个程序集中,则可以将函数设置为internal而不是private。内部任何内容都可以在同一个程序集中访问,但对程序集外的任何类都不可见。

你可能看起来的getter和setter访问器可能指的是在类外部使类的属性不可变。

考虑一个类Foo

公共课Foo {    公共字符串Bar {get;私人集; }

.. }

在这种情况下,Bar属性可以在Foo之外访问,但不能在Foo之外修改。

答案 2 :(得分:0)

我刚刚编写了另一个可能有用的问题的答案:

https://stackoverflow.com/a/31468619/2566963