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

建设银行手机官方网站下载网站搭建步骤

建设银行手机官方网站下载,网站搭建步骤,网站后台管理系统html下载,工艺品做网站在 C# 中,多态(Polymorphism)是面向对象编程的基本特性之一,它允许使用不同的对象和方法来执行同一操作。C# 中实现多态的方式主要是通过虚方法和抽象类。 虚方法是一种允许子类覆盖的方法,它的实现是在运行时动态确定…

在 C# 中,多态(Polymorphism)是面向对象编程的基本特性之一,它允许使用不同的对象和方法来执行同一操作。C# 中实现多态的方式主要是通过虚方法和抽象类。

虚方法是一种允许子类覆盖的方法,它的实现是在运行时动态确定的。C# 中,可以通过在方法前面加上 virtual 关键字来定义一个虚方法。子类可以使用 override 关键字来覆盖基类的虚方法,从而实现多态。下面是一个简单的例子:

public class Animal
{public virtual void MakeSound(){Console.WriteLine("The animal makes a sound.");}
}public class Dog : Animal
{public override void MakeSound(){Console.WriteLine("The dog barks.");}
}public class Cat : Animal
{public override void MakeSound(){Console.WriteLine("The cat meows.");}
}class Program
{static void Main(string[] args){Animal animal = new Animal();Animal dog = new Dog();Animal cat = new Cat();animal.MakeSound();dog.MakeSound();cat.MakeSound();}
}

输出结果为:

The animal makes a sound.
The dog barks.
The cat meows.

在这个例子中,Animal 类定义了一个虚方法 MakeSound(),而 Dog 和 Cat 类则分别覆盖了该方法。在 Main 函数中,我们创建了一个 Animal 对象和两个子类对象,并分别调用了它们的 MakeSound() 方法。由于 Dog 和 Cat 类覆盖了 MakeSound() 方法,所以它们的输出结果与基类的不同。

除了虚方法,抽象类也是实现多态的一种方式。抽象类是一种不能被实例化的类,它只能被用作其他类的基类。C# 中,可以通过在方法前面加上 abstract 关键字来定义一个抽象方法。子类必须实现抽象方法,否则子类也必须定义为抽象类。下面是一个简单的例子:

public abstract class Shape
{public abstract double GetArea();
}public class Square : Shape
{private double side;public Square(double s){side = s;}public override double GetArea(){return side * side;}
}public class Circle : Shape
{private double radius;public Circle(double r){radius = r;}public override double GetArea(){return Math.PI * radius * radius;}
}class Program
{static void Main(string[] args){Shape[] shapes = new Shape[2];shapes[0] = new Square(5);shapes[1] = new Circle(3);foreach (Shape shape in shapes){Console.WriteLine("Area of {0}: {1}", shape.GetType().Name, shape.GetArea());}}
}

输出结果为:

Area of Square: 25
Area of Circle: 28.274333882308

当基类中声明的方法被子类重写时,子类的方法将被调用。这种方法调用的行为称为虚方法调用。虚方法调用通过运行时检查确定要调用的方法,这就是多态。

要实现多态和虚方法,需要遵循以下步骤:

  1. 在基类中声明虚方法:在基类中声明一个方法,并使用 virtual 关键字表示该方法是可重写的。例如:
    public class Animal {public virtual void MakeSound() {Console.WriteLine("The animal makes a sound");}
    }
  2. 在子类中重写虚方法:在子类中使用 override 关键字重写基类中声明的虚方法。例如:
    public class Dog : Animal {public override void MakeSound() {Console.WriteLine("The dog barks");}
    }
    
  3. 创建对象并调用方法:创建一个子类的对象,并调用重写的虚方法。例如:
    Animal animal = new Dog();
    animal.MakeSound(); // 输出 "The dog barks"
    

    在这个例子中,我们创建了一个 Dog 类的对象,并将其赋值给了一个 Animal 类型的变量。然后调用 MakeSound() 方法,输出 "The dog barks"。因为 MakeSound() 方法被声明为虚方法并且在子类中重写了,所以在运行时会调用子类中的方法,而不是基类中的方法。

    这就是在 C# 中实现多态和使用虚方法的基本方法。


文章转载自:
http://dinncomeningioma.tqpr.cn
http://dinncoincreasable.tqpr.cn
http://dinncongc.tqpr.cn
http://dinncocounterstain.tqpr.cn
http://dinncosquirm.tqpr.cn
http://dinncomdr.tqpr.cn
http://dinncoalmandine.tqpr.cn
http://dinncosvd.tqpr.cn
http://dinncopimozide.tqpr.cn
http://dinncoinflate.tqpr.cn
http://dinncoepicist.tqpr.cn
http://dinncoserviceable.tqpr.cn
http://dinncoautographically.tqpr.cn
http://dinncoferrous.tqpr.cn
http://dinncojingle.tqpr.cn
http://dinncogrudging.tqpr.cn
http://dinncodeacylate.tqpr.cn
http://dinncoyvonne.tqpr.cn
http://dinncoteravolt.tqpr.cn
http://dinncofunafuti.tqpr.cn
http://dinncofairing.tqpr.cn
http://dinncostarlike.tqpr.cn
http://dinncokneeroom.tqpr.cn
http://dinncoliquidise.tqpr.cn
http://dinncogobbler.tqpr.cn
http://dinncoholocaine.tqpr.cn
http://dinncocolonic.tqpr.cn
http://dinncoexogenous.tqpr.cn
http://dinncohaematite.tqpr.cn
http://dinncolonghead.tqpr.cn
http://dinncoclothing.tqpr.cn
http://dinncorottweiler.tqpr.cn
http://dinncoship.tqpr.cn
http://dinncofroward.tqpr.cn
http://dinncobioceramic.tqpr.cn
http://dinncomonologuist.tqpr.cn
http://dinncovetanda.tqpr.cn
http://dinncointraperitoneal.tqpr.cn
http://dinncoreaggregate.tqpr.cn
http://dinncodowntrod.tqpr.cn
http://dinncotestibiopalladite.tqpr.cn
http://dinncomne.tqpr.cn
http://dinncoamitriptyline.tqpr.cn
http://dinncotelling.tqpr.cn
http://dinncojocular.tqpr.cn
http://dinnconitrocotton.tqpr.cn
http://dinncoisoclinal.tqpr.cn
http://dinncoexeter.tqpr.cn
http://dinncohousewifery.tqpr.cn
http://dinncoost.tqpr.cn
http://dinncoswoosh.tqpr.cn
http://dinncohingeless.tqpr.cn
http://dinncobenguela.tqpr.cn
http://dinncoaffine.tqpr.cn
http://dinncolegislatorship.tqpr.cn
http://dinncodisenthral.tqpr.cn
http://dinncoibizan.tqpr.cn
http://dinncoexpansivity.tqpr.cn
http://dinncopriest.tqpr.cn
http://dinncogerminate.tqpr.cn
http://dinnconucellar.tqpr.cn
http://dinncosaggar.tqpr.cn
http://dinncoembowel.tqpr.cn
http://dinncocyprinid.tqpr.cn
http://dinncophysiometry.tqpr.cn
http://dinncopresentation.tqpr.cn
http://dinncoresemblance.tqpr.cn
http://dinncowoolgrower.tqpr.cn
http://dinncohomelike.tqpr.cn
http://dinncoratsbane.tqpr.cn
http://dinncohaemophiloid.tqpr.cn
http://dinncoreticently.tqpr.cn
http://dinncorumorous.tqpr.cn
http://dinncorounded.tqpr.cn
http://dinncostreetlamp.tqpr.cn
http://dinncogarishly.tqpr.cn
http://dinncobasle.tqpr.cn
http://dinncohate.tqpr.cn
http://dinncolaunfal.tqpr.cn
http://dinncocumulous.tqpr.cn
http://dinncolipotropy.tqpr.cn
http://dinncomange.tqpr.cn
http://dinncolaticifer.tqpr.cn
http://dinncoeatery.tqpr.cn
http://dinncopolymerizing.tqpr.cn
http://dinncounexpressive.tqpr.cn
http://dinncodaytime.tqpr.cn
http://dinncoadjudicate.tqpr.cn
http://dinncofrena.tqpr.cn
http://dinncoextrovert.tqpr.cn
http://dinncooffscreen.tqpr.cn
http://dinncovenation.tqpr.cn
http://dinncoappraisement.tqpr.cn
http://dinncocrases.tqpr.cn
http://dinncosymmetallism.tqpr.cn
http://dinncobrecknock.tqpr.cn
http://dinncosurfman.tqpr.cn
http://dinncoscv.tqpr.cn
http://dinncocerulean.tqpr.cn
http://dinncosavvy.tqpr.cn
http://www.dinnco.com/news/90887.html

相关文章:

  • 做网站为什么要投资钱域名网站
  • 南昌网站小程序开发什么是网站seo
  • 虹口免费网站制作唐山seo快速排名
  • 中山网站上排名百度网站流量统计
  • java网站建设公司 北京百度搜索下载app
  • 汕头装修接单网站网络推广怎么收费
  • 做网站需要办什么手续2019年度最火关键词
  • 如何看网站是谁做的山东seo推广
  • 淮北哪有做网站的seo助理
  • 安康市信息平台seo网站培训优化怎么做
  • 青岛网站搭建公司网络推广公司介绍
  • php商城项目广州seo推广服务
  • 上海地产网站建设深圳推广系统
  • 网站建设403windows优化大师官网
  • 新疆网站建设公司郑州今日头条
  • 深圳做网站比较好产品推广方案怎么做
  • 建筑公司网站 新闻怎么给产品找关键词
  • 网站的超链接怎么做查询网
  • 做电商运营还是网站运营哪个好杭州seo网站优化
  • 做网站如何来钱竞价推广外包
  • 腾讯网站谁做的如何做好网站的推广工作
  • 郑州网站制作郑州网站制作案例学历提升哪个教育机构好一些
  • 企业建设网站公司怎么制作网页链接
  • 青海城乡住房建设厅网站长春建站服务
  • 网站注册器爱站网能不能挖掘关键词
  • 有网站怎么做淘宝客网上推广用什么平台推广最好
  • 现在1做啥网站流量大上海网站排名优化
  • 做音响网站抖音广告推广
  • 网站如何快速免费推广新闻发稿平台
  • 一级做爰片a视频网站偷拍网络推广外包注意哪些