《C#入门详解》刘老师 操作符详解12(类型转换&this、base)

 

操作符里的类型转换

一、隐式类型转换

  • 不丢失精度的转换
  • 子类向父类的转换
  • 装箱
using system;
namespace Conversion_Example
{
  class Program
  {
   static void Main(string[] args)
   {
       Teacher t = new Teacher();
       Human h = t ; //把T这个变量所存的地址赋给了H
       /*当你拿一个引用变量去访问它所引用着的实例的成员的时候,只能访问到变量的类型所具有的成员,而不是这个变量所引用的实例的类型。所以H这个变量只有eat和think,没有teach, h.teach根本不存在*/
    
    }
  }

   class Animal
   {
      public void Eat()
      {
      console.writeline("Eating.....");
     }
   }

  class Huamn:Animal
   {
      public void Think()
      {
      console.writeline("Who i am.....");
     }
   }

  class Teacher:Human
   {
      public void Teach()
      {
      console.writeline("i teach programming....");
     }
   }

}

二、显式类型转换

  • 有可能丢失精度的转换,即cast
  • 拆箱
  • 使用Convert类
  • ToString方法与各类数据类型的Parse/TryParse方法

例子1:两个变量不兼容,使用Convert转移

double x = system.Convert.ToDouble(Text1);

double y = system.Convert.ToDouble(Text2);

double result = x+y;

this.Text3 = Convert.ToString(result);//或者result.ToString();

例子2:两种类型相兼容

int n = (int) 10.0;

自定义类型转换操作符

  • 示例
using system;
namespace Conversion_Example
{
  class Program
  {
   static void Main(string[] args)
   {
     Stone stone = new Stone();
     stone.age    = 5000;
     Monkey wukongsun = (Monkey) stone;
     Console.Writeling(wukongsun.age);
    
    }
  }

   class Stone
   {
      public int age();

      public static explicit operator Monkey(Stone stone)
      {
       Monkey m = new Monkey();
       m.age       = stone.age/500;
       return m;
       }


      
   }

  class Monkey
   {
      public int age;
   }



}

三、自定义类型转换操作符

"is"、"as"、"?:"、lambda 、this、base

1、lambda

1、匿名方法
2、Inline方法
例子:
Func<int,int,int> func=new Func<int,int,int>(   (int a ,int b)     =>    {return a+b ;}          )
Notice:
*(int a ,int b)匿名方法里接受两个参数
*=>把上面的两个参数代入到   
*{return a+b ;} 函数体
* (int a ,int b)     =>    {return a+b ;}为lambda

 2、this&base

 第一个用处:

 1 class Student : Person
 2     {
 3         private string strClass;
 4  
 5         private string strAddress;
 6  
 7 
 8         public void Address(string cla, string adre)
 9         {
10             //这里的this关键字调用了子类的成员和父类的非似有成员
11             this.strClass = "三零八班";
12             this.strAddress = "北京";
13             this.strName = "李雷";
14  
15             //这里base关键字调用了是父类的非似有成员
16             base.strName = "李雷";
17  
18             Console.WriteLine("我是{0}年纪,来自{1}", cla, adre);
19         }
20         public void Sing()
21         {
22             this.strClass = "";
23             Console.WriteLine("我可以唱歌!");
24         }
25     }
所以在子类中this关键字和base关键字的访问范围的示意图如下:
  this:可以点出当前类可以用的所有成员,包括继承下来的成员。
  base:只能点出父类的成员。

this&base

 

 第二个用处:

public class student
{
   public student(int ID,string Name)
   {
     this.id =ID;
     this.name =Name;
   }
    
   public student(int ID,string Name ,sex Sex) :this (ID,Name)
   {
     this.sex =Sex;

   }

  
   Public int id;
   Public string  name;
   Public sex  sex;


}

  在调用下面的构造函数的时候,会先调用上面的构造函数。调用本类的其他构造函数。

 

原文地址:https://www.cnblogs.com/zfcsharp/p/13714753.html