添加Lesson18
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
using System.Drawing;
|
||||
|
||||
namespace Lesson18_多态_抽象类和抽象方法练习
|
||||
{
|
||||
abstract class Animal
|
||||
{
|
||||
public abstract void Shout();
|
||||
}
|
||||
class Human : Animal
|
||||
{
|
||||
public override void Shout()
|
||||
{
|
||||
Console.WriteLine("人叫");
|
||||
}
|
||||
}
|
||||
class Dog : Animal
|
||||
{
|
||||
public override void Shout()
|
||||
{
|
||||
Console.WriteLine("狗叫");
|
||||
}
|
||||
}
|
||||
class Cat : Animal
|
||||
{
|
||||
public override void Shout()
|
||||
{
|
||||
Console.WriteLine("猫叫");
|
||||
}
|
||||
}
|
||||
abstract class Graph
|
||||
{
|
||||
public abstract void Perimeter();
|
||||
public abstract void Area();
|
||||
}
|
||||
class RectAngle : Graph
|
||||
{
|
||||
private int x, y;
|
||||
public RectAngle(int x,int y)
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
public override void Area()
|
||||
{
|
||||
Console.WriteLine("面积为:"+x*y);
|
||||
}
|
||||
|
||||
public override void Perimeter()
|
||||
{
|
||||
Console.WriteLine("周长为"+(x+y)*2);
|
||||
}
|
||||
}
|
||||
class Square : Graph
|
||||
{
|
||||
private int x;
|
||||
public Square(int x)
|
||||
{
|
||||
this.x = x;
|
||||
}
|
||||
public override void Area()
|
||||
{
|
||||
Console.WriteLine("面积为:" + x * x);
|
||||
}
|
||||
|
||||
public override void Perimeter()
|
||||
{
|
||||
Console.WriteLine("周长为" + x * 4);
|
||||
}
|
||||
}
|
||||
class Round : Graph
|
||||
{
|
||||
private 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);
|
||||
}
|
||||
}
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Animal human = new Human();
|
||||
human.Shout();
|
||||
Animal dog = new Dog();
|
||||
dog.Shout();
|
||||
Animal cat = new Cat();
|
||||
cat.Shout();
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user