154 lines
3.4 KiB
C#
154 lines
3.4 KiB
C#
using System.Threading.Channels;
|
|
|
|
namespace Lesson17_多态_vob练习
|
|
{
|
|
#region 鸭子题
|
|
class Duck
|
|
{
|
|
public virtual void Speak()
|
|
{
|
|
Console.WriteLine("嘎嘎叫");
|
|
}
|
|
}
|
|
class WoodDuck : Duck
|
|
{
|
|
public override void Speak()
|
|
{
|
|
Console.WriteLine("吱吱叫");
|
|
}
|
|
|
|
}
|
|
class RubberDuck : Duck
|
|
{
|
|
public override void Speak()
|
|
{
|
|
Console.WriteLine("叽叽叫");
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 打卡题
|
|
class Staff
|
|
{
|
|
public virtual void Clockin()
|
|
{
|
|
Console.WriteLine("员工9点打卡");
|
|
}
|
|
}
|
|
class Boss: Staff
|
|
{
|
|
public override void Clockin()
|
|
{
|
|
Console.WriteLine("老板11点打卡");
|
|
}
|
|
|
|
}
|
|
class Programer : Staff
|
|
{
|
|
public override void Clockin()
|
|
{
|
|
Console.WriteLine("程序员无需打卡");
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 计算题
|
|
class Graph
|
|
{
|
|
public virtual void Perimeter()
|
|
{
|
|
Console.WriteLine("未初始化图形无法计算");
|
|
}
|
|
public virtual void Area()
|
|
{
|
|
Console.WriteLine("未初始化图形无法计算");
|
|
}
|
|
}
|
|
class Rectangle : Graph
|
|
{
|
|
public int x, y;
|
|
public Rectangle(int x, int y)
|
|
{
|
|
this.x = x;
|
|
this.y = y;
|
|
}
|
|
public override void Perimeter()
|
|
{
|
|
Console.WriteLine("矩形周长为{0}", (x + y) * 2);
|
|
}
|
|
public override void Area()
|
|
{
|
|
Console.WriteLine("矩形面积为{0}", x * y);
|
|
}
|
|
}
|
|
class Square : Graph
|
|
{
|
|
public int x;
|
|
public Square(int x)
|
|
{
|
|
this.x = x;
|
|
}
|
|
|
|
public override void Perimeter()
|
|
{
|
|
Console.WriteLine("方的周长为{0}", x * 4);
|
|
}
|
|
public override void Area()
|
|
{
|
|
Console.WriteLine("方的面积为{0}", x * x);
|
|
}
|
|
}
|
|
class Round : Graph
|
|
{
|
|
public double PI = 3.1415926;
|
|
int r;
|
|
public Round(int r)
|
|
{
|
|
this.r = r;
|
|
}
|
|
public override void Perimeter()
|
|
{
|
|
double perimeter = 2 * PI * r;
|
|
Console.WriteLine("圆的周长{0}",perimeter);
|
|
}
|
|
public override void Area()
|
|
{
|
|
double areas = PI * r * r;
|
|
Console.WriteLine("圆的面积{0}",areas);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
internal class Program
|
|
{
|
|
static void Main(string[] args)
|
|
{
|
|
Console.WriteLine("Hello, World!");
|
|
Duck realDuck = new Duck();
|
|
realDuck.Speak();
|
|
Duck woodDuck = new WoodDuck();
|
|
woodDuck.Speak();
|
|
Duck rubberDuck = new RubberDuck();
|
|
rubberDuck.Speak();
|
|
|
|
Programer p = new Programer();
|
|
p.Clockin();
|
|
Boss b = new Boss();
|
|
b.Clockin();
|
|
Staff s = new Staff();
|
|
s.Clockin();
|
|
|
|
Graph rectangle = new Rectangle(2,3);
|
|
rectangle.Perimeter();
|
|
rectangle.Area();
|
|
Graph square = new Square(2);
|
|
square.Perimeter();
|
|
square.Area();
|
|
Graph round = new Round(3);
|
|
round.Perimeter();
|
|
round.Area();
|
|
|
|
}
|
|
}
|
|
} |