C# 基础入门 互动版

在线工具推荐: Three.js AI纹理开发包 - YOLO合成数据生成器 - GLTF/GLB在线编辑 - 3D模型格式在线转换 - 可编程3D场景编辑器

this与base


  当前类的对象,可以通过this关键字调用相关属性或者方法。

class Employee
{ 
  public string Name;
  public stirng Sex;
  public Person(string Name, string Sex) 
  { 
      this.Name = Name; //this.Name是前面定义的,并不是参数Name
      this.Sex = Sex; 
  }
}

  base 关键字用于从派生类中访问基类的成员。

using System;
public class Person
{
    protected string ssn = "444-55-6666";
    protected string name = "John L. Malgraine";

    public virtual void GetInfo()
    {
        Console.WriteLine("Name: {0}", name);
        Console.WriteLine("SSN: {0}", ssn);
    }
}
class Employee : Person
{
    public string id = "ABC567EFG";
    public override void GetInfo()
    {
        base.GetInfo(); //调用基类的方法GetInfo()
        Console.WriteLine("Employee ID: {0}", id);
    }
}
class TestClass
{
    static void Main()
    {
        Employee E = new Employee();
        E.GetInfo();
    }
}