当前位置: 首页 > news >正文

池州市网站建设_网站建设公司_后端开发_seo优化

网页制作与网站建设完全学习手册pdf,系统之家网站怎么做,企业所得税优惠政策最新2023一般纳税人,wordpress 知更鸟 网格一、base的用法 Base的用法使用场景主要可以概括为两种#xff1a; 1 、访问基类方法 2、 调用基类构造函数 使用要求#xff1a;仅允许用于访问基类的构造函数、实例方法或实例属性访问器。从静态方法中使用 base 关键字是错误的。所访问的基类是类声明中指定的基类。 例如 1 、访问基类方法 2、 调用基类构造函数 使用要求仅允许用于访问基类的构造函数、实例方法或实例属性访问器。从静态方法中使用 base 关键字是错误的。所访问的基类是类声明中指定的基类。 例如如果指定 class ClassB : ClassA则从 ClassB 访问 ClassA 的成员而不考虑 ClassA 的基类。 例子1、访问基类方法 public class animal{public virtual void sound(){Console.WriteLine(动物的叫声wowowow);}}public class dog:animal{public override void sound(){base.sound();Console.WriteLine(dog:wowowowo);}}static void Main(string[] args){dog dog new dog();dog.sound();Console.ReadKey();} 基类 Person 和派生类 Employee 都有一个名为 Getinfo 的方法。 通过使用 base 关键字可以从派生类中调用基类的 Getinfo 方法。 运行结果为 例子2、调用基类构造函数 public class animal{public animal(){Console.WriteLine(发现未知动物);}public animal(int a){Console.WriteLine(发现{0}只未知动物,a);}public virtual void sound(){Console.WriteLine(动物的叫声wowowow);}}public class dog:animal{public dog() : base(){Console.WriteLine(未知动物为小狗);}public dog(int a) : base(a){Console.WriteLine(小狗的数量为{0},a);}public override void sound(){base.sound();Console.WriteLine(dog:wowowowo);}}class Program{static void Main(string[] args){dog dog new dog(2);dog.sound();Console.ReadKey();}} 运行结果为 从例子中我们也可以看的出对于继承类的构造函数访问顺序是父类构造函数再访问子类的构造函数。base 用于用户父类构造函数this 用于调用自己的构造函数。 二、this的用法 this的用法主要总结为5种 限定类似名称隐藏的成员将对象作为参数传递给方法声明索引器串联构造函数扩展方法 1、限定类似名称隐藏的成员用 this 区别类成员和参数 public class Employee {private string alias;private string name;public Employee(string name, string alias){// Use this to qualify the members of the class// instead of the constructor parameters.this.name name;this.alias alias;} }2、将对象作为参数传递给方法 public class animal{public void leg_count(dog dog){Console.WriteLine(狗腿的数量为dog.leg);}public void leg_count(duck duck){Console.WriteLine(鸡腿的数量为 duck.leg);}}public class dog{public int leg 4;public animal animal;public void count(){animal new animal();animal.leg_count(this);}}public class duck{public int leg 2;public animal animal;public void count(){animal new animal();animal.leg_count(this);}}static void Main(string[] args){dog dog new dog();duck duck new duck();dog.count();duck.count();Console.ReadKey();} 运行结果为 3、声明索引器 索引器类似于属性。 很多时候创建索引器与创建属性所使用的编程语言特性是一样的。 索引器使属性可以被索引使用一个或多个参数引用的属性。 这些参数为某些值集合提供索引。 使用 this 关键字作为属性名声明索引器并在方括号内声明参数。 namespace ConsoleApp1 {public class IndexExample{private string[] nameList new string[10];public string this[int index]{get { return nameList[index]; }set { nameList[index] value; }}public int this[string name]{get{for(int i 0; i nameList.Length; i){if(nameList[i] name) return i;}return -1;}}}public class Program{public static void Main(string[] args){IndexExample indexExample new IndexExample();indexExample[0] Tom;indexExample[1] Lydia;Console.WriteLine(indexExample[0]: indexExample[0]);Console.WriteLine(indexExample[Lydia]: indexExample[Lydia]);}} }运行结果为 4、串联构造函数 namespace ConsoleApp1 {public class Test{public Test(){Console.WriteLine(no parameter);}public Test(string str) : this(){Console.WriteLine(one parameter: str);}public Test(string str1, string str2): this(str1){Console.WriteLine(two parameters: str1 ; str2);}}public class ProgramTest{static void Main(string[] args){Console.WriteLine(Test t1 new Test(););Test t1 new Test();Console.WriteLine(Test t2 new Test(str1););Test t2 new Test(str1);Console.WriteLine(Test t3 new Test(str2, str3););Test t3 new Test(str2, str3);}} }运行结果为 Test t1 new Test(); no parameter Test t2 new Test(str1); no parameter one parameter: str1 Test t3 new Test(str2, str3); no parameter one parameter: str2 two parameters: str2 ; str35、扩展方法 定义包含扩展方法的类必须为静态类将扩展方法实现为静态方法并且使其可见性至少与所在类的可见性相同。此方法的第一个参数指定方法所操作的类型此参数前面必须加上 this 修饰符。在调用代码中添加 using 指令用于指定包含扩展方法类的 using。和调用类型的实例方法那样调用这些方法。 官方示例 using System.Linq; using System.Text; using System;namespace CustomExtensions {// Extension methods must be defined in a static class.public static class StringExtension{// This is the extension method.// The first parameter takes the this modifier// and specifies the type for which the method is defined.public static int WordCount(this string str){return str.Split(new char[] { , .,?}, StringSplitOptions.RemoveEmptyEntries).Length;}} } namespace Extension_Methods_Simple {// Import the extension method namespace.using CustomExtensions;class Program{static void Main(string[] args){string s The quick brown fox jumped over the lazy dog.;// Call the method as if it were an// instance method on the type. Note that the first// parameter is not specified by the calling code.int i s.WordCount();System.Console.WriteLine(Word count of s is {0}, i);}} }自己自定义类的代码 第一步、新建一个扩展类 第二步、对扩展类定义 扩展类要引用被扩展类的命名空间。 第三步、在使用界面内引入扩展类的命名空间 代码 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using classextension;namespace 符号测试 {class Program{static void Main(string[] args){Test test new Test();test.method();test.methodextension();Console.ReadKey();}}public class Test{public void method(){Console.WriteLine(这是原始类内的方法);}}}扩展类/ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using 符号测试;namespace classextension {public static class TestExtension{public static void methodextension(this Test test){Console.WriteLine(这是扩展类的方法);}} }运行结果为 参考文献 C# - base 关键字用法_c#中base的用法-CSDN博客 C# - this 的用法_c# 静态函数 子类this参数_wumingxiaoyao的博客-CSDN博客
http://www.lebaoying.cn/news/19360.html

相关文章:

  • 芸志建站怎么建立网站网站的加盟代理
  • 做app网站的软件有哪些内容杨凌开发建设局网站
  • s网站优化郑州做网站 熊掌号
  • 有谁知道知乎网站是谁做的wordpress主题重置
  • 网站建设的基本术语深圳工信部网站备案
  • 怎样自己做代刷网站想做外贸做哪些网站好
  • 如何用电脑做网站服务器吗大型网站怎么做优化
  • 网站优化北京联系电话?北京网站设计联系方式
  • 手机开网站杭州盘石做网站专业吗
  • 舆情监测软件有哪些企业网站设计有名 乐云seo
  • 关于网站建设的策划案如何增加网站访问量
  • 旅游网站建设 策划书手机房屋3d设计软件
  • 安卓手机做网站服务器吗大庆城市投资建设网站
  • 网站免备案空间diy在线定制网站系统
  • 最大的网站建设公司自己设计一个网站
  • 删除网站栏目广州o2o网站建设
  • it网站开发培训中心长沙微信交流群
  • 帝国cms 网站地图插件php外贸网站模板
  • 心理咨询网站建设论文wordpress还是dede
  • 珠海专业网站制作公电商网站建设渠道
  • 在哪个网站上做简历网站重新搭建程序要多少钱
  • 购买网站空间后怎么做智卡会员管理系统
  • 徐州网站建站襄阳做网站排行榜
  • 网站开发流程图解释含义设计工作室怎么找客户
  • 网站设计模板html北京海淀建设工程律师哪个好
  • 策划公司排行榜外贸网站搜索 引擎优化方法
  • dw怎么做网站相册园艺wordpress模板
  • 如何设计酒店网站建设中国贸易信息网
  • 济南做网站多少钱茶社网站开发与设计的开题报告
  • 电商网站英文电信宽带做网站服务器