C#实现多态主要有3种方法,虚方法,抽象类,接口
1虚方法
在父类的方法前面加关键字virtual, 子类重写该方法时在方法名前面加上override关键字,例如下面的Person类的SayHello方法
点击查看代码
class Person
{
public Person(string name)
{
this.Name = name;
}
string _name;
public string Name { get => _name; set => _name = value; }
//父类方法加virtual,子类用override重写该方法,就实现了多态
public virtual void SayHello()
{
Console.WriteLine("我是父类的方法");
}
}
点击查看代码
class Student : Person
{
public Student(string name) : base(name) { }
public override void SayHello()
{
Console.WriteLine("我叫{0}, 我是学生", this.Name);
}
}
class Teacher:Person
{
public Teacher(string name) : base(name) { }
public override void SayHello()
{
Console.WriteLine("我叫{0}, 我是老师", this.Name);
}
}
点击查看代码
Student st = new Student("张三");
Teacher th = new Teacher("李四");
Person[] p = { st, th }; //子类对象赋给父类
for(int i = 0; i < p.Length; i++)
{
p[i].SayHello();
}
Console.ReadKey();
点击查看代码
using System;
namespace 多态之虚方法
{
class Program
{
static void Main(string[] args)
{
Student st = new Student("李雷");
Teacher th = new Teacher("井边君");
Person[] p = { st, th }; //子类对象赋给父类
for(int i = 0; i < p.Length; i++)
{
p[i].SayHello();
}
Console.ReadKey();
}
}
class Person
{
public Person(string name)
{
this.Name = name;
}
string _name;
public string Name { get => _name; set => _name = value; }
//父类方法加virtual,子类用override重写该方法,就实现了多态
public virtual void SayHello()
{
Console.WriteLine("我是父类的方法");
}
}
class Student : Person
{
public Student(string name) : base(name) { }
public override void SayHello()
{
Console.WriteLine("我叫{0}, 我是学生", this.Name);
}
}
class Teacher:Person
{
public Teacher(string name) : base(name) { }
public override void SayHello()
{
Console.WriteLine("我叫{0}, 我是老师", this.Name);
}
}
}
2 抽象类
在类前面加关键字abstract,方法前面加abstract,抽象方法不能有函数体。
抽象类的特点:
(1)可以有字段
(2)可以有非抽象方法
抽象类实现多态的代码如下
点击查看代码
using System;
using System.Collections.Generic;
namespace 多态之抽象类
{
abstract class Person
{
//抽象方法不能有函数体
public abstract void SayHello();
}
class Student : Person
{
public override void SayHello()
{
Console.WriteLine("我是子类Student重写的抽象方法"); ;
}
}
class Teacher : Person
{
public override void SayHello()
{
Console.WriteLine("我是子类Teacher重写的抽象方法"); ;
}
}
class Program
{
static void Main(string[] args)
{
List<Person> clist = new List<Person>();
Student st = new Student();
Teacher th = new Teacher();
clist.Add(st);
clist.Add(th);
foreach(Person p in clist)
{
p.SayHello();
}
Console.ReadKey();
}
}
}
3 接口实现多态
代码如下
点击查看代码
using System;
using System.Collections.Generic;
namespace 多态之接口
{
public interface IWeapon
{
void Fire();
}
class Gun : IWeapon
{
public void Fire()
{
Console.WriteLine("我是枪");
}
}
class Sword : IWeapon
{
public void Fire()
{
Console.WriteLine("我是剑");
}
}
class Tank : IWeapon
{
public void Fire()
{
Console.WriteLine("我是坦克");
}
}
class Program
{
static void Main(string[] args)
{
List<IWeapon> list = new List<IWeapon>();
Gun gun = new Gun();
Sword sw = new Sword();
Tank ta = new Tank();
list.Add(gun);
list.Add(sw);
list.Add(ta);
foreach (IWeapon p in list)
{
p.Fire();
}
Console.ReadKey();
}
}
}
接口的特点
(1)接口里的方法不能有修饰符,默认是public
(2)接口的方法不能有函数体
(3)接口中不能包含示例字段,不能有构造函数
(4)接口里的方法被继承的类重写时,不需要用override关键字,接口不能被实例化
(5)接口之间可以继承,并且可以多继承;接口不能继承于类,但是类可以继承于接口
(6)一个类可以同时继承一个类,并实现多个接口
标签:Console,name,C#,void,多态,class,Person,三种,public From: https://www.cnblogs.com/GaoUpUp/p/17171606.html