1.密封类是修饰为sealed的类,不能有字类,当时可以实例化
是否可以编写一个类继承自String类
答案:不能,因为String也是一个密封类
2.静态类:声明为static的类,不能实例化,只能定义static成员
3.可以在this来做扩展方法
接下来我们可以为string类扩展一个方法,我们写一个方法来让字符串p重复count次,最后返回打印输出
public static class ExtendMethod
{
public static string ShowNumber(this string p,int count)
{
string p1 = "";
for (int i = 0; i < count; i++)
{
p1 += p;
}
return p1;
}
public static void Main(string[] args)
{
string s = "abc";
Console.WriteLine(s.ShowNumber(3));
Console.ReadKey();
}
}
扩展方法注意:1.扩展方法所在类必须是static类
2.扩展方法的第一个参数类型是被扩展的类型,类型前面标注this
3.使用扩展方法的代码必须添加对扩展方法所在类的namespace的using
扫描二维码关注公众号,回复:
13608275 查看本文章
4.扩展方法最终其实还是被编译器处理成普通静态方法的调用
5.扩展方法由于本质上还是静态方法的调用,所以不能访问类的外部访问不了的
---本博客是学习以后记录知识,如有侵权,请联系删除!!!