C#基础概念二十五问

添加人:gamefriends二级(659分)   添加时间:2007-09-13    阅读次数:15113  收藏此教程
4.abstract 是什么意思?

答:

abstract 修饰符可以用于类、方法、属性、事件和索引指示器(indexer),表示其为抽象成员

abstract 不可以和 static 、virtual 一起使用

声明为 abstract 成员可以不包括实现代码,但只要类中还有未实现的抽象成员(即抽象类),那么它的对象就不能被实例化,通常用于强制继承类必须实现某一成员

示例:

 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4 
 5namespace Example04
 6{
 7    基类,抽象类
30 
31    继承类
65 
66    class Program
67    {
68        static void OnFunction(object sender, EventArgs e)
69        {
70            for (int i = 0; i < ((DeriveClass)sender).Attribute.Length; i++)
71            {
72                Console.WriteLine(((DeriveClass)sender)[i]);
73            }

74        }

75        static void Main(string[] args)
76        {
77            DeriveClass tmpObj = new DeriveClass();
78 
79            tmpObj.Attribute = "1234567";
80            Console.WriteLine(tmpObj.Attribute);
81 
82            //将静态函数OnFunction与tmpObj对象的Event事件进行关联
83            tmpObj.Event += new EventHandler(OnFunction);
84 
85            tmpObj.Function("7654321");
86 
87            Console.ReadLine();
88        }

89    }

90}

结果:
1234567
7
6
5
4
3
2
1


5.internal 修饰符起什么作用?

答:

internal 修饰符可以用于类型或成员,使用该修饰符声明的类型或成员只能在同一程集内访问

接口的成员不能使用 internal 修饰符

值得注意的是,如果为 internal 成员加上了 protected 修饰符,这时的访问级别为 internal 或 protected。只是看字面意思容易弄错,许多人认为 internal protected 应该是“只有同一个程序集中的子类可以访问”,但其实它表示“同一个程序集中的所有类,以及所有程序集中的子类都可以访问”

示例

Example05Lib 项目的 Class1

 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4 
 5namespace Example05Lib
 6{
 7    public class Class1
 8    {
 9        internal String strInternal = null;
10        public String strPublic;
11        internal protected String strInternalProtected = null;
12    }

13}

结果
Example05Lib 项目的 Class2 类可以访问到 Class1 的 strInternal 成员,当然也可以访问到 strInternalProtected 成员,因为他们在同一个程序集里


Example05 项目里的 Class3 类无法访问到 Class1 的 strInternal 成员,因为它们不在同一个程序集里。但却可以访问到 strInternalProtected 成员,因为 Class3 是 Class1 的继承类


Example05 项目的 Program 类既无法访问到 Class1 的 strInternal 成员,也无法访问到 strInternalProtected 成员,因为它们既不在同一个程序集里也不存在继承关系

 

9页 第2上一页123456789下一页
相关的教程: CSharp
收藏此教程

当前平均分: 0.0(0 次打分)

-5-4-3-2-1012345
评论主题
您的大名
您的评论
验证码 点击换一个验证码
知识库搜索: