c#内部类的使用
2016-03-07 13:43
387 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 内部类Demo
{
class Program
{
static void Main(string[] args)
{
Student student = new Student("张三",20,"男");
student.ShowMsg();
}
public static void Test() {
Console.WriteLine("我是被内部类调用的外部静态方法");
}
public void Test2() {
Console.WriteLine("我是被内部内调用的外部非静态方法");
}
class Student {
private string name;
private int age;
private string sex;
public Student() { }
public Student(string name, int age, string sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
public void ShowMsg() {
Test();//访问外部内静态成员
new Program().Test2();//访问外部类非静态成员
Console.WriteLine("姓名:{0},年龄:{1},性别:{2}",name,age,sex);
}
}
}
}
(注意)c#内部类只能直接访问外部类的静态成员若要访问非静态成员则需要实例化外部类
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 内部类Demo
{
class Program
{
static void Main(string[] args)
{
Student student = new Student("张三",20,"男");
student.ShowMsg();
}
public static void Test() {
Console.WriteLine("我是被内部类调用的外部静态方法");
}
public void Test2() {
Console.WriteLine("我是被内部内调用的外部非静态方法");
}
class Student {
private string name;
private int age;
private string sex;
public Student() { }
public Student(string name, int age, string sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
public void ShowMsg() {
Test();//访问外部内静态成员
new Program().Test2();//访问外部类非静态成员
Console.WriteLine("姓名:{0},年龄:{1},性别:{2}",name,age,sex);
}
}
}
}
(注意)c#内部类只能直接访问外部类的静态成员若要访问非静态成员则需要实例化外部类
相关文章推荐
- C#检测是否联网
- C#读取Windows日志
- 【开源】.Net 分布式服务中心
- 安装服务时遇到 System.BadImageFormatException: 未能加载文件或程序集
- 从0自学C#08--绘制曲线chart控件
- 在C#的类库中添加web service引用
- C#第四节课
- C#多态的实现:虚方法、抽象类、接口
- C#文件路径的写法
- C# 收发和处理自定义的WINDOWS消息
- C#使用SOAP调用Web Service
- C# 4 中使用迭代器的等待任务
- C#控制定位Word光标移动到任意行或者最后一行,取得光标位置等操作
- about data type in c#
- c#操作word表格
- 背水一战 Windows 10 (1) - C# 6.0 新特性
- c#虚方法和抽象方法的区别
- c# GroupBox 只修改标题的字体和颜色
- C#基础
- C#语言基础—运算符