您的位置:首页 > 其它

类中方法也为私有时,如何访问的问题

2010-10-16 12:29 197 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Text
{

class Program
{
/// <summary>
/// 程序入口点,属性的重要功能就是提供对字段的访问
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
student.MyMethod();
}
#region
class student
{
private double Mymath = 0;
private double Mychinese = 0;
/// <summary>
/// 设置,可读写
/// </summary>
private double _Mymath
{
get
{
return Mymath;
}
set
{
Mymath = value;
}
}
/// <summary>
///
/// </summary>
private double _Mychinese
{
get
{
return Mychinese;
}
set
{
Mychinese = value;
}
}
private double Add()
{
Console.WriteLine(" 调用Add");
return (Mychinese + Mymath);
}
/// <summary>
/// 此处定义一个静态方法,无需实例化即可访问
/// </summary>
public static void MyMethod()
{
student st = new student();
st.Add();
}
}
#endregion
}
}

我们在类中定义了一个静态方法,method,在类外,我们不需要实例化student便可访问,method,

因此可以通过这样的方法来访问类student中的私有的方法。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: