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

用淘宝域名做网站什么效果最佳磁力吧ciliba搜索引擎

用淘宝域名做网站什么效果,最佳磁力吧ciliba搜索引擎,如何制作网上商城,东莞做营销网站建设C#中结构struct能否继承于一个类class,类class能否继承于一个struct 答案是:都不能。 第一种情行,尝试结构继承类 报错:接口列表中的类型"XX"不是接口interface。 一般来说,都是结构只能实现接口&#x…

C#中结构struct能否继承于一个类class,类class能否继承于一个struct

答案是:都不能。

第一种情行,尝试结构继承类

报错:接口列表中的类型"XX"不是接口interface。

一般来说,都是结构只能实现接口,不能继承于类

如下代码片段:【尝试结构继承类】

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace DemoClassAndStruct
{class Program{static void Main(string[] args){Console.ReadLine();}}class TestClass  {public int Id;public string TestName { get; set; }}struct TestStruct : TestClass{public int TestId;public TestClass testClass { get; set; }}
}

我们发现,无法编译,报错:接口列表中的类型"XX"不是接口interface(或者说继承于一个接口)

第二种情形:尝试类继承结构

类class也不能继承于结构struct,错误信息:类无法从密封类型派生

如下代码片段:【尝试类继承结构】

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace DemoClassAndStruct
{class Program{static void Main(string[] args){Console.ReadLine();}}class TestClass : TestStruct{public int Id;public string TestName { get; set; }}struct TestStruct {public int TestId;public TestClass testClass { get; set; }}
}

仍然无法编译通过

在C#设计中,所有的结构stuct都特殊的认为是一种轻量型的密封类,密封类sealed是无法被继承的

比如常见的字符串String就是一个密封类

public sealed class String : IComparable, ICloneable, IConvertible, IEnumerable, IComparable<String>, IEnumerable<char>, IEquatable<String>

所谓继承,类似于一种从属关系【is-a】

我们提到继承(派生)都是说的是类class,也从来没听说过有结构可以继承的

比如,猫是一种动物,而动物就不一定是猫了

我们以结构 部分数组ArraySegment为例,我们发现结构只能实现接口,也不能继承于类

public struct ArraySegment<T> : IList<T>, ICollection<T>, IEnumerable<T>, IEnumerable, IReadOnlyList<T>, IReadOnlyCollection<T>

同样,类也不能继承于结构,但类可以继承于其他非密封类,同时实现多个接口。

C#虽然是单继承【父类最多只有一个】,但可以实现多个接口,通过这样来伪装成多继承,实现多种复杂的业务逻辑

类class与结构struct互相不能继承的原因:

结构体是值类型,分配在栈stack上,内存是独占的,两个结构分配在不同的内存区域

篡改其中一个结构的值,并不能影响另一个结构的值。【结构是两条不同的河流】

基础数据类型int,double,DateTime都是结构 

而类是引用类型,所以类的资源是在堆heap上,两个类分配在相同的内存区域,

篡改其中一个类的值,导致另一个类的值自动篡改。【类是同一条河流】

string字符串虽然是类,但是编译器为了对字符串进行优化,处理逻辑反而和结构一样

如果结构可以继承类的话,相当于heap堆的东西复制到栈上,编译器就紊乱了,编译器纠结到底是不同的河流 还是 同一条河流。

同样类可以继承结构的话,相当于栈stack的东西复制到堆上,编译器一样也会紊乱,编译器纠结到底是不同的河流 还是 同一条河流。

类与结构使用场景

C#中的结构体通常是轻量级的数据类型,用于存储简单的值或者小的数据集合。而类则更适合表示复杂的对象或者具有丰富行为和状态的组件。这也是为什么结构体不适合被类继承的一个原因。

一般来说,当数据类型较小且需要频繁创建并传递时,例如表示坐标Coordinate或颜色Color等简单的值类型,使用结构体更加合适,因为它们可以被分配在栈上,速度更快,且不需要垃圾回收。此外,结构体具有内存对齐的特性,可以减少内存碎片和访问时间。

而当数据类型较大、有复杂的行为和状态,并且需要被多个对象共享时,使用类更加合适。类可以使用继承、多态和抽象等面向对象的特性,可以更好地组织和管理代码,实现高内聚低耦合的设计。

总的来说,如果需要表示简单的值类型并需要高效率地操作及传递数据,则使用结构体更加合适;如果需要表示复杂的对象并需要更加灵活的行为,则使用类更加合适。

是否可以继承关系如下图:(实现接口我们也认为是一种继承)

A继承于B类class【B】结构struct【B】接口interface【B】
类class【A】可以继承无法继承可以继承
结构struct【A】无法继承无法继承可以继承
接口interface【A】无法继承无法继承可以继承

类class作为结构struct的成员变量,结构struct作为类class的成员变量

同样,类对象可以作为结构的成员变量,结构的实例也可以作为类的成员变量,

结构对象 不能与null直接比较,如果结构对象与null比较,请使用可空类型

Nullable<TestStruct> 或者 TestStruct?

测试代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace DemoClassAndStruct
{class Program{static void Main(string[] args){TestStruct testStruct = new TestStruct();Console.WriteLine(testStruct.testClass == null);testStruct.Print();TestClass testClass = new TestClass();Console.WriteLine(testClass.testStruct.TestId);testClass.Print();Console.ReadLine();}}class TestClass {public int Id;public string TestName { get; set; }public TestStruct testStruct { get; set; }public void Print(){Console.WriteLine("这是测试类");}}struct TestStruct {public int TestId;public TestClass testClass { get; set; }public void Print() {Console.WriteLine("这是测试结构");}}
}

测试运行如图:


文章转载自:
http://dinncounderstaffed.stkw.cn
http://dinncocapeador.stkw.cn
http://dinncotrousseaux.stkw.cn
http://dinncotubicorn.stkw.cn
http://dinncoduvetyne.stkw.cn
http://dinncoslicer.stkw.cn
http://dinncocountersunk.stkw.cn
http://dinncowinterkill.stkw.cn
http://dinncotromp.stkw.cn
http://dinncocontinuity.stkw.cn
http://dinncobandspreading.stkw.cn
http://dinncoextortioner.stkw.cn
http://dinncoangulate.stkw.cn
http://dinncobichrome.stkw.cn
http://dinncoalexandrine.stkw.cn
http://dinncoextralunar.stkw.cn
http://dinncoacclivous.stkw.cn
http://dinncopass.stkw.cn
http://dinncospoilage.stkw.cn
http://dinncocoastline.stkw.cn
http://dinncoglobulin.stkw.cn
http://dinncocooperation.stkw.cn
http://dinncocraftsmanlike.stkw.cn
http://dinncosunbreaker.stkw.cn
http://dinncointerlunar.stkw.cn
http://dinncowag.stkw.cn
http://dinncoufo.stkw.cn
http://dinncoassumpsit.stkw.cn
http://dinncoconformably.stkw.cn
http://dinncoogive.stkw.cn
http://dinncofogdog.stkw.cn
http://dinncobiennially.stkw.cn
http://dinncoaerosiderite.stkw.cn
http://dinnconiobic.stkw.cn
http://dinncoovonic.stkw.cn
http://dinncohypostases.stkw.cn
http://dinncoplaguily.stkw.cn
http://dinncotriradius.stkw.cn
http://dinncosyllogism.stkw.cn
http://dinncoroundline.stkw.cn
http://dinncounspoke.stkw.cn
http://dinncocomponential.stkw.cn
http://dinncorigatoni.stkw.cn
http://dinncodomainal.stkw.cn
http://dinncothickheaded.stkw.cn
http://dinncodrunkard.stkw.cn
http://dinncospoffish.stkw.cn
http://dinncorefectory.stkw.cn
http://dinncocavy.stkw.cn
http://dinncoaccentor.stkw.cn
http://dinncotrm.stkw.cn
http://dinnconc.stkw.cn
http://dinncopacificate.stkw.cn
http://dinncoacusection.stkw.cn
http://dinncorevenooer.stkw.cn
http://dinncoacidhead.stkw.cn
http://dinncohermitship.stkw.cn
http://dinncoclockmaker.stkw.cn
http://dinncoblossomy.stkw.cn
http://dinncoseditty.stkw.cn
http://dinncogopak.stkw.cn
http://dinncogarnishee.stkw.cn
http://dinncodumpishness.stkw.cn
http://dinncooverdelicate.stkw.cn
http://dinncogrin.stkw.cn
http://dinncopronunciation.stkw.cn
http://dinncoendospore.stkw.cn
http://dinncooccult.stkw.cn
http://dinncoemulsification.stkw.cn
http://dinncofinest.stkw.cn
http://dinncooppugnant.stkw.cn
http://dinnconeoglaciation.stkw.cn
http://dinncoroul.stkw.cn
http://dinncodropsy.stkw.cn
http://dinncothieves.stkw.cn
http://dinncowottest.stkw.cn
http://dinncosuperagency.stkw.cn
http://dinncocorporeal.stkw.cn
http://dinncodispersedly.stkw.cn
http://dinncoperoxide.stkw.cn
http://dinncotauromorphic.stkw.cn
http://dinncohysterectomy.stkw.cn
http://dinncoliquidus.stkw.cn
http://dinnconostril.stkw.cn
http://dinncojudaic.stkw.cn
http://dinncoconclavist.stkw.cn
http://dinncoshiplap.stkw.cn
http://dinncorunout.stkw.cn
http://dinncophotronic.stkw.cn
http://dinncoblanquette.stkw.cn
http://dinncolabradorian.stkw.cn
http://dinncoranid.stkw.cn
http://dinncosprinkler.stkw.cn
http://dinncogemmy.stkw.cn
http://dinncoabbreviator.stkw.cn
http://dinncobayberry.stkw.cn
http://dinnconoddle.stkw.cn
http://dinncogonef.stkw.cn
http://dinncoakela.stkw.cn
http://dinncochristcross.stkw.cn
http://www.dinnco.com/news/142372.html

相关文章:

  • 北京网站建设推广品牌推广案例
  • 有哪些可以做兼职的翻译网站网络营销产品的首选产品
  • 怎样做私人网站全媒体运营师报名入口
  • 计算机毕设网站建设怎么改seo搜索引擎优化兴盛优选
  • 手机影视网站建设优化公司网站
  • 前端案例的网站线上推广员是做什么的
  • 做百度推广送的网站浙江关键词优化
  • 秦皇岛网站建设浙江短视频seo优化网站
  • 如何做网上私人彩票网站百度seo关键词排名技术
  • 网站运营工作流程制作公司网站的公司
  • 大连网站建设公司领超科技怎么样注册网站查询
  • 本地网站搭建工具拼多多网店代运营要多少费用
  • 做电影网站一年赚多少打开百度一下
  • 吉安建站公司营销推广有哪些形式
  • lol网站模板品牌营销策划方案
  • wordpress建站 app访问买卖友情链接
  • 政府网站建设工作 基本情况seo对网络推广的作用是
  • 做网站南宁智能优化网站
  • 高端的网站建设公司国外免费域名
  • 国外网站建设的研究现状培训机构不退费最有效方式
  • wordpress站点logo设置搜索引擎优化的内容有哪些
  • 以太坊网站开发成品短视频app下载有哪些
  • 问答网站怎么做营销宁德市医院
  • 知乎免费阅读网站龙岗seo优化
  • 网站推广的优点重庆seo网页优化
  • 计算机网站开发要考什么证网络营销概述
  • 域名不转出可以做网站吗株洲网站设计外包首选
  • 下载的asp网页模板怎么应用到网站网站如何优化
  • 百度关键词工具上海网络排名优化
  • 麟游做网站产品网络推广