标签:重写 使用 for img 关键字 多态 eal str log
多态的概率:override与重载(overload)的区别
重载是方法的名称相同。参数或参数类型不同,进行多次重载以适应不同的需要。重载(overload)是面向过程的概念。
Override 是进行基类中函数的重写。Override是面向对象的概念
重写 和重载 不一样 要区分开
namespace _23.虚方法的练习01
{
class Program
{
static void Main(string[] args)
{
//用虚方法来实现 真的鸭子嘎嘎叫 木头鸭子吱吱叫 橡皮鸭子唧唧叫
RealDuck rd1 = new RealDuck();
MuDuck md1 = new MuDuck();
XPDuck xd1 = new XPDuck();
RealDuck[] rd = new RealDuck[3] { rd1,md1,xd1} ;
for (int i = 0; i < rd.Length; i++)
{
rd[i].Bark();
}
Console.ReadKey();
}
}
public class RealDuck
{
public virtual void Bark()
{
Console.WriteLine("真的鸭子嘎嘎叫");
}
}
public class MuDuck:RealDuck
{
public override void Bark()
{
Console.WriteLine("木头鸭子吱吱叫");
}
}
public class XPDuck:RealDuck
{
public override void Bark()
{
Console.WriteLine("橡皮鸭子唧唧叫");
}
}
}
namespace _24.虚方法的练习02
{
class Program
{
static void Main(string[] args)
{
//经理十一点打卡 员工九点打卡 程序员不打卡
Employee emp = new Employee();
Manager mg = new Manager();
Programmer pm = new Programmer();
Employee[] emps = {emp,mg,pm};
for (int i = 0; i < emps.Length; i++)
{
emps[i].DaKa();
}
Console.ReadKey();
}
}
/// <summary>
/// 员工类
/// </summary>
public class Employee
{
public virtual void DaKa()
{
Console.WriteLine("员工,九点打卡");
}
}
public class Manager : Employee
{
public override void DaKa()
{
Console.WriteLine("经理十一点打卡");
}
}
public class Programmer : Employee
{
public override void DaKa()
{
Console.WriteLine("程序员不打卡");
}
}
}
namespace _25.抽象类的练习01
{
class Program
{
static void Main(string[] args)
{
//狗狗会叫 猫咪也会叫
Dog dg = new Dog();
Cat ct = new Cat();
Animal[] am = { dg,ct};
for (int i = 0; i < am.Length; i++)
{
am[i].Shout();
}
Console.ReadKey();
}
}
public abstract class Animal
{
public abstract void Shout();
}
public class Dog : Animal
{
public override void Shout()
{
Console.WriteLine("小狗汪汪叫");
}
}
public class Cat : Animal
{
public override void Shout()
{
Console.WriteLine("小猫喵喵叫");
}
}
}
namespace _26.使用多态来解决实际问题01
{
class Program
{
static void Main(string[] args)
{
//使用多态求矩形的面积和周长以及圆形的面积和周长
//思考过程:
//矩形不能作为圆形的父类 因为计算方法不一样
//圆形也不能作为矩形的父类 因为计算方法不一样
//所以 虚方法不能解决问题
//我们 采用抽象类来实现
Shape s1 = new Rectangle(5, 3);
Console.WriteLine("矩形的面积为:{0},矩形的周长为:{1}.",s1.area(),s1.Perimeter());
Shape s2 = new Circle(3);
Console.WriteLine("圆的面积为:{0:#.##},圆形的周长为:{1:#.##}.", s2.Perimeter(), s2.Perimeter());
Console.ReadKey();
}
}
public abstract class Shape
{
public abstract double area(); //没有实现的求面积的方法
public abstract double Perimeter(); //没有实现的求周长的方法
}
public class Rectangle : Shape
{
private double _chang;
private double _kuan;
public double Chang
{
get
{
return _chang;
}
set
{
_chang = value;
}
}
public double Kuan
{
get
{
return _kuan;
}
set
{
_kuan = value;
}
}
public Rectangle(double chang, double kuan)
{
this.Chang = chang;
this.Kuan=kuan;
}
public override double area()
{
return Chang * Kuan;
}
public override double Perimeter()
{
return (Chang + Kuan) * 2;
}
}
public class Circle : Shape
{
private double r;
public double R
{
get
{
return r;
}
set
{
r = value;
}
}
public Circle(double r)
{
this.R = r;
}
public override double area()
{
return Math.PI * Math.Pow(R, 2);
}
public override double Perimeter()
{
return 2*Math.PI*R;
}
}
}
标签:重写 使用 for img 关键字 多态 eal str log
原文地址:http://www.cnblogs.com/HelloZyjS/p/6042500.html