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

陕西省建设厅的网站网络广告创意

陕西省建设厅的网站,网络广告创意,py怎么做网站,常见网站模式用户定义的显式和隐式转换运算符 参考代码 用户定义的显式和隐式转换运算符 - 提供对不同类型的转换 | Microsoft Learn 代码例程 using System;public readonly struct Digit {private readonly byte digit;public Digit(byte digit){if (digit > 9){throw new Argumen…

用户定义的显式和隐式转换运算符

参考代码

用户定义的显式和隐式转换运算符 - 提供对不同类型的转换 | Microsoft Learn

代码例程

using System;public readonly struct Digit
{private readonly byte digit;public Digit(byte digit){if (digit > 9){throw new ArgumentOutOfRangeException(nameof(digit), "Digit cannot be greater than nine.");}this.digit = digit;}public static implicit operator byte(Digit d) => d.digit;public static explicit operator Digit(byte b) => new Digit(b);public override string ToString() => $"{digit}";
}public static class UserDefinedConversions
{public static void Main(){var d = new Digit(7);byte number = d;Console.WriteLine(number);  // output: 7Digit digit = (Digit)number;Console.WriteLine(digit);  // output: 7}
}

自实现 隐式运算符转换例程

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConsoleApp5
{class Person{public string Name { get; set; }public int age { get; set; }public Person(string name, int age){Name = name;this.age = age;}/// <summary>/// 自定义隐式转换/// </summary>/// <param name="p"></param>public static  implicit operator string (Person p){return p.Name;}}internal class Program{static void Main(string[] args){Person p1 = new Person("张三",20);Person p2 = new Person("李四", 30);string username = p1 + p2; //自定义对象隐式转换Console.WriteLine(username);Console.ReadLine();}}
}

运行结果

上面当然也可以适用于年龄,只需要修改返回值即可。

自实现 显式运算符转换例程

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConsoleApp7
{class Person{public string Name { get; set; }public int age { get; set; }public Person(string name, int age){Name = name;this.age = age;}/// <summary>/// 自定义隐式转换/// </summary>/// <param name="p"></param>//public static implicit operator string(Person p)//{//    return p.Name;//}//public static implicit operator Person(string name)//{//    return new Person(name, 0);//}///运算符重载public static string operator +(Person p1, Person p2){return p1.Name + p2.Name; }}internal class Program{static void Main(string[] args){Person p1 = new Person("张三", 20);Person p2 = new Person("李四", 30);//string username = p1 + p2; //自定义对象隐式转换//Console.WriteLine(username);//Person p3 = "王五"; //强转,与视频中不同的时前面没有加括号//Person p3a = (Person)"赵六"; //视频中加了括号强转//Console.WriteLine(p3.Name + " " + p3.age.ToString());//Console.WriteLine(p3a.Name + " " + p3a.age.ToString());string username = p1 + p2; //自定义对象 + 重载Console.WriteLine(username);Console.ReadLine();}}
}

运行结果

与上面的相同,注意这里时显式运算符重载。

 

强制类型转换

自定义对象强转转换

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConsoleApp6
{class Person{public string Name { get; set; }public int age { get; set; }public Person(string name, int age){Name = name;this.age = age;}/// <summary>/// 自定义隐式转换/// </summary>/// <param name="p"></param>//public static implicit operator string(Person p)//{//    return p.Name;//}public static implicit operator Person(string name){return new Person(name, 0);}}internal class Program{static void Main(string[] args){Person p1 = new Person("张三", 20);Person p2 = new Person("李四", 30);//string username = p1 + p2; //自定义对象隐式转换//Console.WriteLine(username);Person p3 = "王五"; //强转,与视频中不同的时前面没有加括号Person p3a = (Person)"赵六"; //视频中加了括号强转Console.WriteLine(p3.Name + " " + p3.age.ToString());Console.WriteLine(p3a.Name + " " + p3a.age.ToString());Console.ReadLine();}}
}

运行结果(注意没有初始化年龄,使用默认)

+运算符重载

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConsoleApp8
{class Person{public string Name { get; set; }public int age { get; set; }public Person(string name, int age){Name = name;this.age = age;}/// <summary>/// 自定义隐式转换/// </summary>/// <param name="p"></param>//public static implicit operator string(Person p)//{//    return p.Name;//}//public static implicit operator Person(string name)//{//    return new Person(name, 0);//}///运算符重载public static string operator +(Person p1, Person p2){return p1.Name + p2.Name;}public static Person operator ++(Person p){p.age++;return p;}}internal class Program{static void Main(string[] args){Person p1 = new Person("张三", 20);Person p2 = new Person("李四", 30);//string username = p1 + p2; //自定义对象隐式转换//Console.WriteLine(username);//Person p3 = "王五"; //强转,与视频中不同的时前面没有加括号//Person p3a = (Person)"赵六"; //视频中加了括号强转//Console.WriteLine(p3.Name + " " + p3.age.ToString());//Console.WriteLine(p3a.Name + " " + p3a.age.ToString());//string username = p1 + p2; //自定义对象 + 重载//Console.WriteLine(username);p1++;Console.WriteLine(p1.age.ToString());Console.ReadLine();}}
}

 运行结果(略)

附一个微软的例子

public readonly struct Fraction
{private readonly int num;private readonly int den;public Fraction(int numerator, int denominator){if (denominator == 0){throw new ArgumentException("Denominator cannot be zero.", nameof(denominator));}num = numerator;den = denominator;}public static Fraction operator +(Fraction a) => a;public static Fraction operator -(Fraction a) => new Fraction(-a.num, a.den);public static Fraction operator +(Fraction a, Fraction b)=> new Fraction(a.num * b.den + b.num * a.den, a.den * b.den);public static Fraction operator -(Fraction a, Fraction b)=> a + (-b);public static Fraction operator *(Fraction a, Fraction b)=> new Fraction(a.num * b.num, a.den * b.den);public static Fraction operator /(Fraction a, Fraction b){if (b.num == 0){throw new DivideByZeroException();}return new Fraction(a.num * b.den, a.den * b.num);}public override string ToString() => $"{num} / {den}";
}public static class OperatorOverloading
{public static void Main(){var a = new Fraction(5, 4);var b = new Fraction(1, 2);Console.WriteLine(-a);   // output: -5 / 4Console.WriteLine(a + b);  // output: 14 / 8Console.WriteLine(a - b);  // output: 6 / 8Console.WriteLine(a * b);  // output: 5 / 8Console.WriteLine(a / b);  // output: 10 / 4}
}

参考讲解实例,老师讲得非常好,希望大家多多关注ub主

c shape c# 自定义隐式转换与运算符重载_哔哩哔哩_bilibiliicon-default.png?t=N176https://www.bilibili.com/video/BV1rg411A7QU/?spm_id_from=333.1007.top_right_bar_window_history.content.click&vd_source=e821a225c7ba4a7b85e5aa6d013ac92e

特此记录

anlog

2023年2月11日

 

http://www.ds6.com.cn/news/68521.html

相关文章:

  • 网站建设最新技术百度竞价排名事件
  • wordpress 系统日志文件临沂百度seo
  • 免费做头像网站有哪些百度权重查询网址
  • 网站开发公司建网站百度竞价怎么做开户需要多少钱
  • 网站开发专业术语直通车关键词怎么选 选几个
  • 易企秀怎么做网站短网址生成器免费
  • wordpress底部音频宁宁网seo
  • 云南省建设系统网站dz论坛seo设置
  • 郑州郑州网站建设河南做网站公司网站的优化公司
  • 国外代码开源网站icp备案查询
  • 青岛做外贸网站建设自媒体营销代理
  • 毕业设计做网站用什么软件济南搜索引擎优化网站
  • 怎么做网站的效果图秘密入口3秒自动进入
  • 做游戏动画外包网站珠海网站建设优化
  • 可以做一键拨号和导航的网站2022最新热点事件及点评
  • 快速建立平台网站开发设计建站流程b站推广入口2023
  • htm网站开发 实训淘宝指数查询官网手机版
  • 网站搭建语言做网站多少钱一年
  • 确定B2B网站建设方案热点新闻事件及评论
  • 线上网站开发系统流程东莞网站seo公司
  • 深圳网站建设易佰讯整站优化和关键词优化的区别
  • 收藏网站的链接怎么做的seo网络营销的技术
  • 深圳网站制作07551字节跳动广告代理商加盟
  • 网站开发属于什么系统天津百度搜索排名优化
  • 西宁网站开发抖音营销推广怎么做
  • wordpress 不提示更新如何seo推广
  • 深圳精品网站建设公司seo霸屏软件
  • 外贸企业邮箱哪家好网络优化的内容包括哪些
  • 微信小程序开发制作平台成都网站seo推广
  • 张家港网站建设做网站2021年经典营销案例