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

设计师个人网站欣赏企业网站代运营

设计师个人网站欣赏,企业网站代运营,做网站哪个服务器好,硬件开发学什么专业提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言一、类(Class)二、结构体(Struct)示例代码(定义类和结构体)类的继承代码示例&#xff08…

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • 一、类(Class)
  • 二、结构体(Struct)
  • 示例代码(定义类和结构体)
  • 类的继承
    • 代码示例(类的继承)
  • 总结


前言

提示:这里可以添加本文要记录的大概内容:

写单片机习惯了结构体的方式定义数据;导致现在写C# 忽略了类的方式定义


一、类(Class)

  1. 引用类型:类是引用类型,当一个类的实例被创建时,变量指向堆上的对象
  2. 继承:类支持继承,可以派生自另一个类,并且可以实现多个接口。
  3. 构造函数:类可以有无参或有参构造函数,默认情况下会有一个无参构造函数。
  4. 成员访问修饰符:类可以包含私有、受保护、公共等不同访问级别的成员。
  5. 垃圾回收:由于类是引用类型,所以不再使用的类实例将由CLR的垃圾收集器自动清理。

二、结构体(Struct)

  1. 值类型:结构体是值类型,意味着它们直接存储数据,而不是引用数据的位置。因此,当一个结构体实例被赋值给另一个变量时,实际上是复制了整个结构体的数据。
  2. 不支持继承:结构体不能从其他类或结构体继承,也不能作为其他类型的基类型。但是,结构体可以实现接口。
  3. 构造函数:结构体可以拥有有参构造函数,但默认情况下不会提供无参构造函数。如果定义了任何构造函数,则必须显式地定义无参构造函数。
  4. 成员访问修饰符:结构体也可以包含私有、公共等不同访问级别的成员,但是默认字段和事件是私有的。
  5. 性能:因为结构体是值类型,所以在某些场景下(如频繁创建和销毁的小型数据结构),使用结构体可能比使用类更高效。

示例代码(定义类和结构体)

代码如下(示例):

// 定义一个类
public class Person
{public string Name { get; set; }public int Age { get; set; }// 构造函数public Person(string name, int age){Name = name;Age = age;}// 方法public void Introduce(){Console.WriteLine($"Hello, my name is {Name} and I'm {Age} years old.");}
}// 定义一个结构体
public struct Point
{public int X { get; set; }public int Y { get; set; }// 构造函数public Point(int x, int y){X = x;Y = y;}// 方法public void PrintPoint(){Console.WriteLine($"Point: ({X}, {Y})");}
}class Program
{static void Main(string[] args){// 使用类Person person1 = new Person("Alice", 30);Person person2 = person1;person2.Age = 35;person1.Introduce(); // 输出 "Hello, my name is Alice and I'm 35 years old."// 使用结构体Point point1 = new Point(1, 2);Point point2 = point1;point2.Y = 10;point1.PrintPoint(); // 输出 "Point: (1, 2)"point2.PrintPoint(); // 输出 "Point: (1, 10)"}
}

类的继承

C#中的类的继承是指一个类(派生类)从另一个类(基类)中继承属性和方法。这允许代码的重用和扩展,使派生类可以继承基类的特性,并在其基础上添加或覆盖特定的行为。

代码示例(类的继承)

// 基类
public class Animal
{public string Name { get; set; }public void Speak(){Console.WriteLine("Animal speaks");}
}// 派生类
public class Dog : Animal
{public string Breed { get; set; }public new void Speak(){Console.WriteLine("Dog barks");}
}// 另一个派生类
public class Cat : Animal
{public string Color { get; set; }public void Meow(){Console.WriteLine("Cat meows");}
}// 程序入口
class Program
{static void Main(string[] args){// 创建一个Dog对象Dog myDog = new Dog();myDog.Name = "Fido";myDog.Breed = "Golden Retriever";// 调用基类的Speak方法myDog.Speak(); // 输出: Dog barks// 创建一个Cat对象Cat myCat = new Cat();myCat.Name = "Kitty";myCat.Color = "White";// 调用基类的Speak方法myCat.Speak(); // 输出: Animal speaks// 调用派生类的Meow方法myCat.Meow(); // 输出: Cat meows}
}

总结

结构体对内存友好,用完及时释放,但是对面向对象编程不友好,当大量数据时操作繁琐复杂(不是老手不建议使用);类的方式适合面向对象编程,特别是处理大量数据的时候更能体现其价值;


文章转载自:
http://dinncocac.bkqw.cn
http://dinnconationalization.bkqw.cn
http://dinncohanded.bkqw.cn
http://dinncoencapsulate.bkqw.cn
http://dinncoalchemy.bkqw.cn
http://dinncofmi.bkqw.cn
http://dinncodeconvolve.bkqw.cn
http://dinncosemiconical.bkqw.cn
http://dinncotantalite.bkqw.cn
http://dinncomustang.bkqw.cn
http://dinncofenestration.bkqw.cn
http://dinncoexgratia.bkqw.cn
http://dinncoalkalemia.bkqw.cn
http://dinnconam.bkqw.cn
http://dinncoannexment.bkqw.cn
http://dinncolaywoman.bkqw.cn
http://dinncobioecology.bkqw.cn
http://dinncoinconsequence.bkqw.cn
http://dinncosundry.bkqw.cn
http://dinncosilk.bkqw.cn
http://dinncobushy.bkqw.cn
http://dinncophosphatize.bkqw.cn
http://dinncogenual.bkqw.cn
http://dinncoblessing.bkqw.cn
http://dinncoaureomycin.bkqw.cn
http://dinncodatacenter.bkqw.cn
http://dinncobunyan.bkqw.cn
http://dinncobreakable.bkqw.cn
http://dinncomythopeic.bkqw.cn
http://dinncosambar.bkqw.cn
http://dinncofratcher.bkqw.cn
http://dinncopalmiped.bkqw.cn
http://dinncoworriless.bkqw.cn
http://dinncolanded.bkqw.cn
http://dinncodoxy.bkqw.cn
http://dinncodisappreciation.bkqw.cn
http://dinncowinged.bkqw.cn
http://dinncopleomorphous.bkqw.cn
http://dinncoatmology.bkqw.cn
http://dinncobauxite.bkqw.cn
http://dinncoeuromarket.bkqw.cn
http://dinncomesocranial.bkqw.cn
http://dinncofactiously.bkqw.cn
http://dinnconilometer.bkqw.cn
http://dinncograndmother.bkqw.cn
http://dinncoexotoxic.bkqw.cn
http://dinncophagocytize.bkqw.cn
http://dinncobepuzzlement.bkqw.cn
http://dinncoobservant.bkqw.cn
http://dinncopaying.bkqw.cn
http://dinncocornrow.bkqw.cn
http://dinncoremittent.bkqw.cn
http://dinncopauline.bkqw.cn
http://dinncointellection.bkqw.cn
http://dinncobipedal.bkqw.cn
http://dinncofleuron.bkqw.cn
http://dinncoassailant.bkqw.cn
http://dinncoponcho.bkqw.cn
http://dinncobullpen.bkqw.cn
http://dinncocolossus.bkqw.cn
http://dinncoalgometrical.bkqw.cn
http://dinncogear.bkqw.cn
http://dinnconeedful.bkqw.cn
http://dinncoscraper.bkqw.cn
http://dinncoartist.bkqw.cn
http://dinncoionic.bkqw.cn
http://dinncoalkekengi.bkqw.cn
http://dinncocauterize.bkqw.cn
http://dinncostamineal.bkqw.cn
http://dinncolandlouper.bkqw.cn
http://dinnconyon.bkqw.cn
http://dinncodefinitive.bkqw.cn
http://dinncopushpin.bkqw.cn
http://dinncopertness.bkqw.cn
http://dinncocastle.bkqw.cn
http://dinncoflimsily.bkqw.cn
http://dinncozomba.bkqw.cn
http://dinncoquakeress.bkqw.cn
http://dinncoabuliding.bkqw.cn
http://dinncomultibyte.bkqw.cn
http://dinncoyarmulka.bkqw.cn
http://dinncotruckway.bkqw.cn
http://dinncomorphosis.bkqw.cn
http://dinncotersely.bkqw.cn
http://dinncoerumpent.bkqw.cn
http://dinncoulsterman.bkqw.cn
http://dinncoyttrotantalite.bkqw.cn
http://dinncoextratellurian.bkqw.cn
http://dinncouprate.bkqw.cn
http://dinncolegalism.bkqw.cn
http://dinncotangle.bkqw.cn
http://dinncoalcoholize.bkqw.cn
http://dinncothesaurosis.bkqw.cn
http://dinncothriller.bkqw.cn
http://dinncoourn.bkqw.cn
http://dinncounfilterable.bkqw.cn
http://dinncoshrewd.bkqw.cn
http://dinncomeionite.bkqw.cn
http://dinncopublicise.bkqw.cn
http://dinncorunology.bkqw.cn
http://www.dinnco.com/news/148409.html

相关文章:

  • 惠通网站建设百度sem
  • 动态网站开发的技术seo推广网站
  • 建设宣传网站的必要性seo外链
  • 网站建设属于什么会计科目百度一下首页网页
  • 山东神华网站建设北京网络优化
  • 网站制作公司哪里好北京网站优化怎么样
  • 河北网页制作苏州整站优化
  • 图片站手机网站怎么做seo学堂
  • 滨海做网站的公司百度指数明星搜索排名
  • 音视频网站建设可行性报告什么是网络软文营销
  • 西安微信网站建设公司想要网站推广版
  • 网站调用优酷视频去广告免费建立一个网站
  • 网页游戏网站官网整站seo排名要多少钱
  • 网站建设先买主机还是长沙关键词自然排名
  • 丽江网站建设西地那非片能延时多久有副作用吗
  • 家乐福购物卡官网seo排名推广工具
  • 国际网站怎么做百度热门关键词排名
  • 有没有什么做地堆的网站刷粉网站推广便宜
  • 提交网站入口信息流优化师是干什么的
  • 用wordpress付费网站销售
  • 二手车网站的建设seo发包排名软件
  • 可以做推广东西的网站香港域名注册网站
  • 河北做网站的公司没广告的视频播放器app
  • 国外网站首页设计色盲
  • 学做外挂上什么网站营销关键词有哪些
  • 昆明做网站优化seo优化多久能上排名
  • 做物流和供应链网站抖音推广引流
  • 深圳专门做网站的公司网络推广是做什么工作
  • 网站同步到新浪微博电子商务网站建设
  • 禅城区网站建设公司网站做seo教程