|
楼主 |
发表于 2012-11-15 16:48
|
显示全部楼层
C#类库学习
代码连接地址:http://msdn.microsoft.com/zh-cn/library/k6sa6h87(v=vs.100).aspx
用示例代码封装一个圆面积和圆柱面积函数的类库,
类库代码:- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace myFunction
- {
-
- abstract class Shape
- {
- public const double pi = Math.PI;
- protected double x, y;
- public Shape(double x, double y)
- {
- this.x = x;
- this.y = y;
- }
- public abstract double Area();
- }
- public class ShapeFunction
- {
- class Circle : Shape
- {
- public Circle(double radius)
- : base(radius, 0)
- {
- }
- public override double Area()
- {
- return pi * x * x;
- }
- }
- class Cylinder : Circle
- {
- public Cylinder(double radius, double height)
- : base(radius)
- {
- y = height;
- }
- public override double Area()
- {
- return (2 * base.Area()) + (2 * pi * x * y);
- }
- }
- public double circle(double radius)
- {
- Circle ring = new Circle(radius);
- return ring.Area();
- }
- public double cylinder(double radius, double height)
- {
- Cylinder tube = new Cylinder(radius, height);
- return tube.Area();
-
- }
-
- }
-
- }
复制代码 调用类库代码:- public void DemoFunction()
- {
- double radius = 2.5;
- double height = 3.0;
-
- ShapeFunction ring = new ShapeFunction();
- //Cylinder tube = new Cylinder(radius, height);
-
- Excel.Worksheet sh;
- sh = this.Application.ActiveSheet;
- sh.Cells[10,1].value = "圆面积";
- sh.Cells[10,2].value = string.Format("{0:F2}", ring.circle(radius));
- sh.Cells[11,1].value = "圆柱面积";
- sh.Cells[11,2].value = string.Format("{0:F2}", ring.cylinder(radius,height));
-
- }
复制代码 |
评分
-
1
查看全部评分
-
|