实现多态的手段
1、虚方法
步骤:将父类的方法标记为虚方法,使用关键字virtual,这个方法可以被子类重新写一遍。(重写)
将子类的方法使用override —-(重写)
using System;
namespace _081_多态之虚方法
{
class Program
{
static void Main(string[] args)
{
//概念:让一个对象能够表现出多种的状态(类型)
//实现多态的三种方法:1、虚方法 2、抽象类 3、接口
Chinese cn1 = new Chinese("傻逼");
Chinese cn2 = new Chinese("笨蛋");
Japanese j1 = new Japanese("波多野结衣");
Japanese j2 = new Japanese("冲田杏梨");
Korea k1 = new Korea("金智秀");
Korea k2 = new Korea("金秀智");
American a1 = new American("Saber");
American a2 = new American("WeXiao");
//Person[] pers = new Person[8];
Person[] pers = { cn1, cn2, j1, j2, k1, k2, a1, a2 };
for (int i = 0; i < pers.Length; i++)
{
//if(pers[i] is Chinese)
//{
// ((Chinese)pers[i]).SayHello();
//}
//else if (pers[i] is Japanese)
//{
// ((Japanese)pers[i]).SayHello();
//}
//else if (pers[i] is Korea)
//{
// ((Korea)pers[i]).SayHello();
//}
//else //if (pers[i] is American)
//{
// ((American)pers[i]).SayHello();
//}
pers[i].SayHello();
}
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace _081_多态之虚方法
{
class Person
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
public Person(string name)
{
this._name = name;
}
public virtual void SayHello()
{
Console.WriteLine("我是人类,我的名字是{0}", Name);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace _081_多态之虚方法
{
class Korea:Person
{
public Korea(string name) : base(name)
{ }
public override void SayHello()
{
Console.WriteLine("我是韩国还是朝鲜,我的名字是{0}", Name);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace _081_多态之虚方法
{
class Japanese : Person
{
public Japanese(string name) : base(name)
{
}
public override void SayHello()
{
Console.WriteLine("我是小日本,我的名字是{0}", Name);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace _081_多态之虚方法
{
class English:Person
{
public English(string name) : base(name)
{ }
public override void SayHello()
{
Console.WriteLine("English");
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace _081_多态之虚方法
{
class Chinese : Person
{
public Chinese(string name) : base(name)
{
}
public override void SayHello()
{
Console.WriteLine("我是中国人,我的名字是{0}", Name);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace _081_多态之虚方法
{
class American : Person
{
public American(string name) : base(name)
{ }
public override void SayHello()
{
Console.WriteLine("我是米国人,我的名字是{0}", Name);
}
}
}
练习:
using System;
namespace _082_虚方法练习
{
class Program
{
static void Main(string[] args)
{
Duck duck=new Duck();
RealDuck realDuck = new RealDuck();
RubberDuck rubberDuck = new RubberDuck();
WoodenDuck woodenDuck = new WoodenDuck();
DeadDuck deadDuck = new DeadDuck();
Duck[] ducks = { duck, realDuck, rubberDuck, woodenDuck, deadDuck };
for (int i = 0; i < ducks.Length; i++)
{
ducks[i].Bark();
}
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace _082_虚方法练习
{
class Duck
{
public virtual void Bark()
{
Console.WriteLine("鸭子叫");
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace _082_虚方法练习
{
class RealDuck : Duck
{
public override void Bark()
{
Console.WriteLine("真的鸭子嘎嘎叫");
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace _082_虚方法练习
{
class RubberDuck : Duck
{
public override void Bark()
{
Console.WriteLine("橡胶鸭子唧唧叫");
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace _082_虚方法练习
{
class DeadDuck : Duck
{
public override void Bark()
{
Console.WriteLine("死的鸭子不会叫");
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace _082_虚方法练习
{
class WoodenDuck : Duck
{
public override void Bark()
{
Console.WriteLine("木头鸭子吱吱叫");
}
}
}
2、抽象类
当父类中的方法不知道如何去实现时,可以考虑将父类写成抽象类,将方法写成抽象方法。(抽象方法没有方法体)
特点:
1、抽象成员必须标记为abstract,并且不能有任何实现。
2、抽象成员必须在抽象类中。
3、抽象类不能被实例化。
4、子类继承抽象类后,必须把父类中的所有抽象成员都重写。
(除非子类也是一个抽象类,则可以不用重写)
5、抽象成员的访问修饰符不能是private。
6、在抽象类中可以包含实例成员,并且抽象类的实例成员可以不被子类实现。
7、抽象类是有构造函数的,虽然不能被实例化。
抽象类练习
using System;
namespace _084_抽象类练习
{
class Program
{
static void Main(string[] args)
{
//1|使用多态求矩形和圆的面积和周长
Console.WriteLine("输入圆的半径:");
double radius = Convert.ToDouble(Console.ReadLine());
Shape circle = new Circle(radius);
double circleArea = circle.Area();
double circlePerimeter = circle.Perimeter();
Console.WriteLine("圆的面积为:{0},圆的周长为:{1}", circleArea, circlePerimeter);
Console.WriteLine("请输入矩形的长和宽:");
double length = Convert.ToInt32(Console.ReadLine());
double width = Convert.ToInt32(Console.ReadLine());
Shape rectangle = new Rectangle(length, width);
double rectangleArea = rectangle.Area();
double rectanglePerimeter = rectangle.Perimeter();
Console.WriteLine("矩形的面积为:{0},矩形的周长为:{1}", rectangleArea, rectanglePerimeter);
//2、模拟移动硬盘、U盘、MP3插到电脑上读写数据
//通过Computer含参数方法实现
//1、
//MobileDisk md = new MobileDisk();
//UDisk ud = new UDisk();
MP3 mp3 = new MP3();
//Computer c = new Computer();
//c.CpuRead(md);
//c.CpuWrite(md);
mp3.PlayMusic();
//2、
//MobileStorage ms = new UDisk();
//Computer cpu = new Computer();
//cpu.CpuRead(ms);
//cpu.CpuWrite(ms);
MobileStorage ms = new UDisk();
Computer cpu = new Computer();
cpu.Ms = ms;
cpu.CpuRead();
cpu.CpuWrite();
Console.ReadKey();
}
}
}
练习一的类:
using System;
using System.Collections.Generic;
using System.Text;
namespace _084_抽象类练习
{
abstract class Shape
{
public abstract double Perimeter();
public abstract double Area();
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace _084_抽象类练习
{
class Circle : Shape
{
private double _radius;
public double Radius
{
get { return _radius; }
set { _radius = value; }
}
public Circle(double r)
{
this._radius = r;
}
public override double Area()
{
return Math.PI * this._radius * this._radius;
}
public override double Perimeter()
{
return Math.PI * this._radius * 2;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace _084_抽象类练习
{
class Rectangle : Shape
{
private double _length;
public double Length
{
get { return _length; }
set { _length = value; }
}
private double _width;
public double Width
{
get { return _width; }
set { _width = value; }
}
public Rectangle(double l,double w)
{
this.Length = l;
this.Width = w;
}
public override double Area()
{
return this.Length * this.Width;
}
public override double Perimeter()
{
return (this.Length + this.Width) * 2;
}
}
}
练习二的类:
using System;
using System.Collections.Generic;
using System.Text;
namespace _084_抽象类练习
{
abstract class MobileStorage
{
public abstract void Read();
public abstract void Write();
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace _084_抽象类练习
{
class MobileDisk : MobileStorage
{
public override void Read()
{
Console.WriteLine("移动硬盘在读取数据");
}
public override void Write()
{
Console.WriteLine("移动硬盘在写入数据");
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace _084_抽象类练习
{
class UDisk : MobileStorage
{
public override void Read()
{
Console.WriteLine("U盘在读取数据");
}
public override void Write()
{
Console.WriteLine("U盘在写入数据");
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace _084_抽象类练习
{
class MP3 : MobileStorage
{
public override void Read()
{
Console.WriteLine("MP3在读取数据");
}
public override void Write()
{
Console.WriteLine("MP3在写入数据");
}
public void PlayMusic()
{
Console.WriteLine("MP3在放音乐");
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace _084_抽象类练习
{
class Computer
{
private MobileStorage _ms;
public MobileStorage Ms
{
get { return _ms; }
set { _ms = value; }
}
public void CpuRead()
{
Ms.Read();
}
public void CpuWrite()
{
Ms.Write();
}
//使用方法来实现
//public void CpuRead(MobileStorage ms)
//{
// ms.Read();
//}
//public void CpuWrite(MobileStorage ms)
//{
// ms.Write();
//}
}
}