类中方法也为私有时,如何访问的问题
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中的私有的方法。
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中的私有的方法。
相关文章推荐
- 如何扩大网站访问,五种方法让问题不再难
- 如何访问python类中的私有方法
- 关于如何取消访问https时的提示:“此网站的安全证书存在问题”的解决方法
- 如何通过反射访问私有变量和私有方法
- 如何利用反射去访问一个私有的方法
- [C#]如何访问及调用类中私有成员及方法
- iOS开发笔记之七十——如何访问对象的私有方法和变量
- 如何利用反射去访问一个私有的方法
- java中的反射机制——如何访问私有成员的私有方法
- 如何访问私有方法
- delphi 中如何访问另一个类中到私有方法?
- 如何使用java反射机制测试私有方法
- 如何强制访问c++私有成员
- c# 读取其他程序正打开的文件的时“正由另一进程使用,因此该进程无法访问该文件。"的问题解决方法
- blog流量分析以及关于如何提高CSDN blog访问数量的初步方法
- Java中的类成员访问控制符和继承重写父类的方法时的类成员访问控制符的相关问题
- 在使用私有属性公有方法访问的时候在get方法里使用判断
- 教你如何解决Windows xp局域网访问故障方法
- ruby中如何调用与局部变量同名的私有方法
- Java通过反射访问私有成员变量和私有方法