/* #########
############
#############
## ###########
### ###### #####
### ####### ####
### ########## ####
#### ########### ####
#### ########### #####
##### ### ######## #####
##### ### ######## ######
###### ### ########### ######
###### #### ############## ######
####### ##################### ######
####### ###################### ######
####### ###### ################# ######
####### ###### ###### ######### ######
####### ## ###### ###### ######
####### ###### ##### #####
###### ##### ##### ####
##### #### ##### ###
##### ### ### #
### ### ###
## ### ###
__________#_______####_______####______________
身是菩提树,心如明镜台,时时勤拂拭,勿使惹尘埃。
我们的未来没有BUG
* ==============================================================================
* Filename: AbstractScript
* Created: $time$
* Author: WYC
* Purpose: 封装、继承、多态
* ==============================================================================
*/
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AbstractScript : MonoBehaviour {
void Start () {
Circle circle = new Circle();
circle.R = 20;
Rectangle rectangle = new Rectangle();
rectangle.Width = 20;
rectangle.Height = 30;
List<Shape> shapeList = new List<Shape>();
shapeList.Add(circle);
shapeList.Add(rectangle);
foreach (var shape in shapeList)
{
shape.PrintArea(shape.CalculateArea());
shape.PrintPerimeter(shape.CalculatePerimeter());
}
}
}
public abstract class Shape {
public string ShapeName { get; private set; }
public Shape(string shapeName) {
ShapeName = shapeName;
}
public virtual void PrintPerimeter(double perimeter) {
Debug.Log(ShapeName + "perimeter:" + perimeter);
}
public virtual void PrintArea(double area) {
Debug.Log(ShapeName + "Area:" + area);
}
public abstract double CalculatePerimeter();
public abstract double CalculateArea();
}
public class Circle : Shape
{
public double R { get; set; }
public Circle() : base("圆") {
this.R = 0;
}
public override double CalculatePerimeter()
{
return 2 * Math.PI * R;
}
public override double CalculateArea()
{
return Math.PI * R * R;
}
}
public class Rectangle : Shape
{
public double Width { get; set; }
public double Height { get; set; }
public Rectangle() : base("长方形")
{
this.Width = 0;
this.Height = 0;
}
public override double CalculatePerimeter()
{
return (Width + Height) * 2;
}
public override double CalculateArea()
{
return Width * Height;
}
}