【发布时间】:2015-07-17 00:12:43
【问题描述】:
我很好奇如何将 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);
}
}
}
【问题讨论】:
-
你可以删掉你的大部分代码,因为这里是多余的。不要把你所有的代码都放在这里。只保留 addOperands 和 Add_Click.remove other
-
你能解释一下你为什么想要它私有吗?
-
访问私有方法的唯一方法是使用公共构造函数/属性/方法访问。
-
私人意味着你不能从另一个班级访问它。
-
从公共方法中创建私有方法的方式是做出设计决策