当前位置:汇报书 > 总结专题 > 工作总结专题 > 面向对象编程总结

面向对象编程总结

发布时间:2026-10-26 10:59:18

面向对象编程总结 篇1

一、this指针是什么:

这里有一些面向对象编程的概念需要说明:类(Class)的概念和对象(Object)的概念

类是对事物概括,也是C#编码时所有代码归属的基本单位;而对象是对类的实例化,也就是C#里new方法的返回值。写代码是不能直接用操作类,而只能先实例化类,然后我们用这个类被实例化后的对象。

通俗一些的说明是,“类”好比是“人”的概念,而我们把类实例化后,就成为单个人的“对象”,例如:“张三”。

this就是你的代码所归属的`“类”的实例化后的“对象”,也叫当前对象。

二、this指针的几种用法:

1、限定被相似的名称隐藏的成员

主要代码如下:

public class ThisName { public string name = 张三; public int num = 55; public ThisName() { } public void GetThisName(string name, int num) { name = this.name;//调取全局变量name num = this.num;//调取全局变量num HttpContext.Current.Response.Write(参数name的值为:+name+;参数num的值为:+num);//输出结果为参数name的值为张三;参数num值为:55 }}

2、将对象作为参数传递到别的方法中

主要代码如下:

public class ThisFF{ public ThisFF() { } public string shuju() { return This指针当做方法传递; } public void f1(ThisFF ff) { HttpContext.Current.Response.Write(ff.shuju()); } public void f() { f1(this);//this在这里可以理解为 ThisFF ff=news ThisFF();当前类的实例 }} ThisFF ff=news ThisFF();//方法实例 ff.f();//调用方法 输出结果为: This指针当做方法传递

3、声明索引器

主要代码如下:

private int _Sy; public int this[int sy] { get { return _Sy;} set { sy=_Sy;} }

三、总结:

1.this关键字引用被访问成员所在的当前实例。静态成员函数没有this指针。this关键字可以用来从构造函数,实例方法和实例化访问器中访问成员。

不能在静态方法。静态属性访问器或者域声明的变量初始化程序中使用this关键字,这将会产生错误。

2.在类的构造函数中出现的this作为一个值类型表示对正在构造的对象本身的引用。

3.在类的方法中出现this作为一个值类型表示对调用该方法的对象的引用。

4.在结构的构造函数中出现的this作为一个变量类型表示对正在构造的结构的引用。

5.在结构的方法中出现的this作为一个变量类型表示对调用该方法的结构。

面向对象编程总结 篇2

public virtual void IntroduceMyself(){...}//父类虚函数

public new void IntroduceMyself(){...}//子类覆盖父类虚函数

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace MethodOverrideByNew{ public enum Genders { Female=0, Male=1 } public class Person { protected string _name; protected int _age; protected Genders _gender; ///

/// 父类构造函数 ///

public Person() { this._name = "DefaultName"; this._age = 23; this._gender = Genders.Male; } ///

/// 定义虚函数IntroduceMyself() ///

public virtual void IntroduceMyself() { System.Console.WriteLine("Person.IntroduceMyself()"); } ///

/// 定义虚函数PrintName() ///

public virtual void PrintName() { System.Console.WriteLine("Person.PrintName()"); } } public class ChinesePerson :Person{ ///

/// 子类构造函数,指明从父类无参构造函数调用起 ///

public ChinesePerson() :base(){ this._name = "DefaultChineseName"; } ///

/// 覆盖父类方法IntroduceMyself,使用new关键字修饰虚函数 ///

public new void IntroduceMyself() { System.Console.WriteLine("ChinesePerson.IntroduceMyself()"); } ///

/// 重载父类方法PrintName,使用override关键字修饰虚函数 ///

public override void PrintName(){ System.Console.WriteLine("ChinesePerson.PrintName()"); } } class Program { static void Main(string[] args) { //定义两个对象,一个父类对象,一个子类对象 Person aPerson = new ChinesePerson(); ChinesePerson cnPerson = new ChinesePerson(); //调用覆盖的方法,父类对象不能调用子类覆盖过的方法,只能调用自身的虚函数方法 aPerson.IntroduceMyself(); cnPerson.IntroduceMyself(); //调用重载方法,父类对象和子类对象都可以调用子类重载过后的方法 aPerson.PrintName(); cnPerson.PrintName(); System.Console.ReadLine(); } }}

结果:

Person.IntroduceMyself()

ChinesePerson.IntroduceMyself()

ChinesePerson.PrintName()

ChinesePerson.PrintName()

以上这篇C# 函数覆盖总结学习(推荐)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。