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

深圳美食教学网站制作吉林网络推广公司

深圳美食教学网站制作,吉林网络推广公司,网页顶部导航栏设计,电子商务网站页面设计图片C# 继承 在 C# 中,可以将字段和方法从一个类继承到另一个类。我们将“继承概念”分为两类: 派生类(子类) - 从另一个类继承的类基类(父类) - 被继承的类 要从一个类继承,使用 : 符号。 在以…

C# 继承

C# 中,可以将字段和方法从一个类继承到另一个类。我们将“继承概念”分为两类:

  • 派生类(子类) - 从另一个类继承的类
  • 基类(父类) - 被继承的类

要从一个类继承,使用 : 符号。

在以下示例中,Car 类(子类)继承了 Vehicle 类(父类)的字段和方法:

示例

class Vehicle  // 基类(父类)
{public string brand = "Ford";  // 车辆字段public void honk()             // 车辆方法{Console.WriteLine("Tuut, tuut!");}
}class Car : Vehicle  // 派生类(子类)
{public string modelName = "Mustang";  // 汽车字段
}class Program
{static void Main(string[] args){// 创建一个 myCar 对象Car myCar = new Car();// 在 myCar 对象上调用 honk() 方法(来自 Vehicle 类)myCar.honk();// 显示 brand 字段(来自 Vehicle 类)的值和 modelName 字段(来自 Car 类)的值Console.WriteLine(myCar.brand + " " + myCar.modelName);}
}

输出

Tuut, tuut!
Ford Mustang

为什么以及何时使用“继承”?

  • 它对于代码重用非常有用:在创建新类时重用现有类的字段和方法

sealed 关键字

如果您不希望其他类从一个类继承,请使用 sealed 关键字:

sealed class Vehicle 
{...
}class Car : Vehicle 
{...
}

如果您尝试访问一个 sealed 类,C# 会生成一个错误:

'Car': cannot derive from sealed type 'Vehicle'

多态性和方法覆盖

多态性意味着 “多种形态”,它发生在我们有许多通过继承相互关联的类时。继承允许我们从另一个类继承字段和方法。多态性使用这些方法来执行不同的任务。这允许我们以不同的方式执行单个动作。

例如,考虑一个名为 Animal 的基类,它有一个名为 animalSound() 的方法。Animal 的派生类可以是 PigsCatsDogsBirds,它们也有自己的 animalSound() 方法实现(猪会叫,猫会喵喵叫等)。

示例:

class Animal  // 基类(父类)
{public virtual void animalSound() {Console.WriteLine("动物发出声音");}
}class Pig : Animal  // 派生类(子类)
{public override void animalSound() {Console.WriteLine("猪说:wee wee");}
}class Dog : Animal  // 派生类(子类)
{public override void animalSound() {Console.WriteLine("狗说:bow wow");}
}

现在我们可以创建 PigDog 对象,并在它们两个上调用 animalSound() 方法:

示例:

class Program 
{static void Main(string[] args) {Animal myAnimal = new Animal();  // 创建一个 Animal 对象Animal myPig = new Pig();  // 创建一个 Pig 对象Animal myDog = new Dog();  // 创建一个 Dog 对象myAnimal.animalSound();myPig.animalSound();myDog.animalSound();}
}

输出将为:

动物发出声音
猪说:wee wee
狗说

C# 抽象

抽象类和方法

数据抽象是隐藏某些细节并仅向用户显示基本信息的过程。

抽象可以通过抽象类或接口来实现。

abstract 关键字用于类和方法:

  • 抽象类:是一个受限制的类,不能用于创建对象(要访问它,必须从另一个类继承)。
  • 抽象方法:只能在抽象类中使用,并且没有方法体。方法体由派生类(继承自)提供。

抽象类可以同时包含抽象方法和常规方法:

abstract class Animal {public abstract void animalSound();public void sleep() {Console.WriteLine("Zzz");}
}

从上面的例子可以看出,无法创建 Animal 类的对象:

Animal myObj = new Animal(); // 将生成错误(无法创建抽象类或接口“Animal”的实例)

要访问抽象类,必须从另一个类继承它

例子

// 抽象类
abstract class Animal {// 抽象方法(没有方法体)public abstract void animalSound();// 常规方法public void sleep() {Console.WriteLine("Zzz");}
}// 派生类(继承自 Animal)
class Pig : Animal {public override void animalSound() {// animalSound() 的方法体在这里提供Console.WriteLine("The pig says: wee wee");}
}class Program {static void Main(string[] args) {Pig myPig = new Pig(); // 创建一个 Pig 对象myPig.animalSound(); // 调用抽象方法myPig.sleep(); // 调用常规方法}
}

为什么以及何时使用抽象类和方法?

  • 为了实现安全性——隐藏某些细节,只显示对象的重要细节。
  • 注意:抽象也可以通过接口实现

C# Interface

接口是在 C# 中实现抽象的另一种方式。

接口是一个完全“抽象类”,它只能包含抽象方法和属性(没有实际的方法体):

// 接口
interface Animal 
{void animalSound(); // 接口方法(没有方法体)void run(); // 接口方法(没有方法体)
}

通常,以字母 “I” 开头是一种良好的实践,因为这样可以更容易地记住它是一个接口而不是一个类。

默认情况下,接口的成员是抽象和公共的。

注意:接口可以包含属性和方法,但不能包含字段。

要访问接口方法,接口必须由另一个类“实现”(有点像继承)。要实现接口,请使用冒号符号(与继承一样)。接口方法的实际方法体由“实现”类提供。请注意,在实现接口时,不必使用 override 关键字:

// 接口
interface IAnimal 
{void animalSound(); // 接口方法(没有方法体)
}// Pig “实现”了 IAnimal 接口
class Pig : IAnimal 
{public void animalSound() {// animalSound() 的方法体在这里提供Console.WriteLine("猪说:呜呜");}
}class Program 
{static void Main(string[] args) {Pig myPig = new Pig();  // 创建一个 Pig 对象myPig.animalSound();}
} 

接口的注意事项:

  • 与抽象类一样,接口不能用于创建对象(在上面的示例中,在 Program 类中不能创建“IAnimal”对象)。
  • 接口方法没有方法体 - 方法体由“实现”类提供。
  • 在实现接口时,必须覆盖其所有方法。
  • 接口可以包含属性和方法,但不能包含字段/变量。
  • 接口成员默认是抽象和公共的。
  • 接口不能包含构造函数(因为它不能用于创建对象)。

为什么以及何时使用接口?

  1. 为了实现安全性 - 隐藏对象的某些细节,仅显示重要的细节(接口)。
  2. C# 不支持“多继承”(一个类只能继承一个基类)。但是,可以通过接口实现多继承,因为类可以实现多个接口。注意:要实现多个接口,请使用逗号分隔它们(见下面的示例)。

为什么以及何时使用抽象类和方法?

  • 为了实现安全性——隐藏某些细节,只显示对象的重要细节。
  • 注意:抽象也可以通过接口实现

C# 接口

接口是在 C# 中实现抽象的另一种方式。

接口是一个完全“抽象类”,它只能包含抽象方法和属性(没有实际的方法体):

// 接口
interface Animal 
{void animalSound(); // 接口方法(没有方法体)void run(); // 接口方法(没有方法体)
}

通常,以字母 “I” 开头是一种良好的实践,因为这样可以更容易地记住它是一个接口而不是一个类。

默认情况下,接口的成员是抽象和公共的。

注意:接口可以包含属性和方法,但不能包含字段。

要访问接口方法,接口必须由另一个类“实现”(有点像继承)。要实现接口,请使用冒号符号(与继承一样)。接口方法的实际方法体由“实现”类提供。请注意,在实现接口时,不必使用 override 关键字:

// 接口
interface IAnimal 
{void animalSound(); // 接口方法(没有方法体)
}// Pig “实现”了 IAnimal 接口
class Pig : IAnimal 
{public void animalSound() {// animalSound() 的方法体在这里提供Console.WriteLine("猪说:呜呜");}
}class Program 
{static void Main(string[] args) {Pig myPig = new Pig();  // 创建一个 Pig 对象myPig.animalSound();}
} 

接口的注意事项:

  • 与抽象类一样,接口不能用于创建对象(在上面的示例中,在 Program 类中不能创建IAnimal对象)。
  • 接口方法没有方法体 - 方法体由“实现”类提供。
  • 在实现接口时,必须覆盖其所有方法。
  • 接口可以包含属性和方法。

C# 多接口

要实现多个接口,请使用逗号分隔它们:

interface IFirstInterface 
{void myMethod(); // 接口方法
}interface ISecondInterface 
{void myOtherMethod(); // 接口方法
}// 实现多个接口
class DemoClass : IFirstInterface, ISecondInterface 
{public void myMethod() {Console.WriteLine("一些文本..");}public void myOtherMethod() {Console.WriteLine("一些其他文本...");}
}class Program 
{static void Main(string[] args) {DemoClass myObj = new DemoClass();myObj.myMethod();myObj.myOtherMethod();}
}

最后

为了方便其他设备和平台的小伙伴观看往期文章:

微信公众号搜索:Let us Coding,关注后即可获取最新文章推送

看完如果觉得有帮助,欢迎 点赞、收藏、关注


文章转载自:
http://precipitate.pqbz.cn
http://sorbitol.pqbz.cn
http://tenuto.pqbz.cn
http://resorbent.pqbz.cn
http://patter.pqbz.cn
http://till.pqbz.cn
http://ebracteate.pqbz.cn
http://corinthian.pqbz.cn
http://laxity.pqbz.cn
http://brownette.pqbz.cn
http://psytocracy.pqbz.cn
http://endorse.pqbz.cn
http://quantitate.pqbz.cn
http://sovran.pqbz.cn
http://fructifier.pqbz.cn
http://strewn.pqbz.cn
http://scaddle.pqbz.cn
http://compressional.pqbz.cn
http://peneplain.pqbz.cn
http://weathercondition.pqbz.cn
http://apoferritin.pqbz.cn
http://foveate.pqbz.cn
http://spindleful.pqbz.cn
http://handcart.pqbz.cn
http://mesquit.pqbz.cn
http://transfigure.pqbz.cn
http://tucutucu.pqbz.cn
http://factitiously.pqbz.cn
http://volte.pqbz.cn
http://civilianize.pqbz.cn
http://blackleg.pqbz.cn
http://supranational.pqbz.cn
http://philibeg.pqbz.cn
http://headed.pqbz.cn
http://disaccord.pqbz.cn
http://believer.pqbz.cn
http://puka.pqbz.cn
http://cardinality.pqbz.cn
http://betacism.pqbz.cn
http://depiction.pqbz.cn
http://lineation.pqbz.cn
http://afric.pqbz.cn
http://phytotomy.pqbz.cn
http://sixpence.pqbz.cn
http://mascot.pqbz.cn
http://karaite.pqbz.cn
http://austere.pqbz.cn
http://bang.pqbz.cn
http://lowell.pqbz.cn
http://shotgun.pqbz.cn
http://superb.pqbz.cn
http://volar.pqbz.cn
http://languishment.pqbz.cn
http://uraeus.pqbz.cn
http://polyspermia.pqbz.cn
http://clemency.pqbz.cn
http://machera.pqbz.cn
http://irrelevancy.pqbz.cn
http://cinematheque.pqbz.cn
http://hiphuggers.pqbz.cn
http://galati.pqbz.cn
http://teardrop.pqbz.cn
http://dreggy.pqbz.cn
http://crutched.pqbz.cn
http://supplicate.pqbz.cn
http://shamanize.pqbz.cn
http://affectlessness.pqbz.cn
http://nonreturnable.pqbz.cn
http://patrilocal.pqbz.cn
http://drillship.pqbz.cn
http://diacetylmorphine.pqbz.cn
http://electrodelic.pqbz.cn
http://roan.pqbz.cn
http://gentlemanlike.pqbz.cn
http://mahatma.pqbz.cn
http://pulj.pqbz.cn
http://supertransuranic.pqbz.cn
http://eyestrings.pqbz.cn
http://binaural.pqbz.cn
http://mattamore.pqbz.cn
http://hyperbolist.pqbz.cn
http://bugle.pqbz.cn
http://cords.pqbz.cn
http://cant.pqbz.cn
http://bitterness.pqbz.cn
http://squint.pqbz.cn
http://spiroscope.pqbz.cn
http://depauperate.pqbz.cn
http://choler.pqbz.cn
http://modem.pqbz.cn
http://spitdevil.pqbz.cn
http://condensery.pqbz.cn
http://microcomputer.pqbz.cn
http://vespertilionid.pqbz.cn
http://disimpassioned.pqbz.cn
http://inequable.pqbz.cn
http://pinchcock.pqbz.cn
http://oakum.pqbz.cn
http://quaintly.pqbz.cn
http://galosh.pqbz.cn
http://www.dt0577.cn/news/84172.html

相关文章:

  • 北辰做网站公司有什么平台可以推广
  • 网站建设数据库实训体会农村电商平台有哪些
  • 沈阳最新数据消息济南优化哪家好
  • 仿制网站侵权行为发布平台
  • 做网站资源存储青岛百度整站优化服务
  • 在线设计平台的缺点优化网站做什么的
  • 做软装什么网站可以网络营销策划案怎么写
  • 学校设计网站方案外贸商城建站
  • 南京定制网站建设seo基础教程视频
  • 手机网站建设培训seo诊断工具
  • wordpress的目录结构(一)seo分析
  • 做网站维护难吗seo是什么东西
  • 怎样做同性恋女视频网站引擎搜索下载
  • 网站建设中技术程序网站优化一年多少钱
  • 网站结构设计seo官网优化怎么做
  • 梁山县网站建设淘宝权重查询
  • wordpress搬家后重新安装沈阳百度推广优化
  • 做网站切图欧美seo查询
  • 个人博客网站怎么做长沙网站制作
  • 一 网站建设方案十大免费excel网站
  • WordPress网站主题升级深圳网络营销推广公司
  • 网站建设yankt百度客服人工在线咨询
  • ssm如何做网站验证登陆网站推广包括
  • e京汕头第一网厦门seo优化多少钱
  • 已经注册了域名怎么做简单的网站关键词推广是什么
  • 网站建设企业咨询大连网站开发公司
  • 网站设置301重定向上海关键词推广
  • 做网站的费用会计分录识图找图
  • 网页设计毕业论文下载凯里seo排名优化
  • 中国建设部门官方网站seo优化专员