| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2625 人关注过本帖
标题:如何定义类?
只看楼主 加入收藏
hailingmm
Rank: 2
等 级:论坛游民
帖 子:96
专家分:15
注 册:2009-10-15
结帖率:91.67%
收藏
已结贴  问题点数:20 回复次数:9 
如何定义类?
class cp
    {
        public double sum(double[] t)
        {
            double result = 0;
            foreach (double i in t)
            {
                result = result + i;
            }
            return result;
        }
cp mycp = new cp();
        PointF[] p = new PointF[8];
        double a, b, c, u, q;

        private double sumx(PointF[] tp)//求X的和
        {
            double[] temp = new double[tp.Length];
            for (int i = 0; i < tp.Length; i++)
            {
                temp[i] = tp[i].X;
            }
            return mycp.sum(temp);
        }
        private double sumy(PointF[] tp)//Y的和
        {
            double[] temp = new double[tp.Length];
            for (int i = 0; i < tp.Length; i++)
            {
                temp[i] = tp[i].Y;
            }
            return mycp.sum(temp);
        }
        private double sumx2(PointF[] tp)//求X2的和
        {
            double[] temp = new double[tp.Length];
            for (int i = 0; i < tp.Length; i++)
            {
                temp[i] = Math.Pow(tp[i].X, 2);  
            }
            return mycp.sum(temp);
        }
        private double sumx3(PointF[] tp)//求X3和
        {
            double[] temp = new double[tp.Length];
            for (int i = 0; i < tp.Length; i++)
            {
                temp[i] = Math.Pow(tp[i].X, 3);
            }
            return mycp.sum(temp);
        }
        private double sumx4(PointF[] tp)//求X4的和
        {
            double[] temp = new double[tp.Length];
            for (int i = 0; i < tp.Length; i++)
            {
                temp[i] = Math.Pow(tp[i].X, 4);
            }
            return mycp.sum(temp);
        }
        private double sumxy(PointF[] tp)//求XY的和
        {
            double[] temp = new double[tp.Length];
            for (int i = 0; i < tp.Length; i++)
            {
                temp[i] = tp[i].X * tp[i].Y;
            }
            return mycp.sum(temp);
        }
        private double sumx2y(PointF[] tp)//求X2Y的和
        {
            double[] temp = new double[tp.Length];
            for (int i = 0; i < tp.Length; i++)
            {
                temp[i] = Math.Pow(tp[i].X, 2) * tp[i].Y;
            }
            return mycp.sum(temp);
        }
        private void calculate()
        {
            int n = tidu.Length;
            double d, e, f, g, h;
            d = n * sumx2(p) - sumx(p) * sumx(p);
            e = n * sumx2y(p) - sumx2(p) * sumy(p);
            f = n * sumxy(p) - sumx(p) * sumy(p);
            g = n * sumx3(p) - sumx2(p) * sumx(p);
            h = n * sumx4(p) - sumx2(p) * sumx2(p);
            a = (d * e - f * g) / (h * d - g * g);
            b = (f - g * a) / d;
            c = (sumy(p) - a * sumx2(p) - b * sumx(p)) / n;
            u = -(b / (2 * a));
            q = Math.Sqrt(-1 / (2 * a));
            this.label4.Text = "a=" + a.ToString() + "\r\nb=" + b.ToString() + "\r\nc=" + c.ToString() + "\r\nu=" + u.ToString() + "\r\nq=" + q.ToString();
        }

怎么把上面的用类定义出来,哪位大侠帮帮忙!
搜索更多相关主题的帖子: 定义 
2010-05-20 11:31
bygg
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:乖乖的心中
等 级:版主
威 望:241
帖 子:13555
专家分:3076
注 册:2006-10-23
收藏
得分:0 
class cp
你这个不是类吗?你还想怎么定义?

建议先看看资料什么的。

飘过~~
2010-05-20 11:33
hailingmm
Rank: 2
等 级:论坛游民
帖 子:96
专家分:15
注 册:2009-10-15
收藏
得分:0 
回复 楼主 hailingmm
我想把下面的定义成类,看了一些资料,但是不会啊!
2010-05-20 11:35
hhy420459674
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:203
专家分:179
注 册:2010-4-9
收藏
得分:0 
那你就重新定义一个类  然后把下面的代码放进该类就行了啊??
2010-05-20 11:45
hailingmm
Rank: 2
等 级:论坛游民
帖 子:96
专家分:15
注 册:2009-10-15
收藏
得分:0 
回复 3楼 hailingmm
我试了怎么有问题啊?
2010-05-20 13:59
hailingmm
Rank: 2
等 级:论坛游民
帖 子:96
专家分:15
注 册:2009-10-15
收藏
得分:0 
回复 3楼 hailingmm
重新定义一个类放进去老是出现这样的错误:找不到类型或命名空间名称PointF,是否缺少using指令或程序集应用。
2010-05-20 14:03
hailingmm
Rank: 2
等 级:论坛游民
帖 子:96
专家分:15
注 册:2009-10-15
收藏
得分:0 
回复 3楼 hailingmm
定义了之后怎么调用类啊?
2010-05-20 14:08
hhy420459674
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:203
专家分:179
注 册:2010-4-9
收藏
得分:0 
PointF[] p = new PointF[8];
这是什么?抱你那个错误时因为PointF不是String[]一样  不可以自己写就用了

你是想把它PointF[] 定义成个类么?
2010-05-20 18:30
adu_to
Rank: 2
等 级:论坛游民
帖 子:12
专家分:75
注 册:2010-4-21
收藏
得分:20 
1.在开始加上引用空间:using System.Drawing
2.程序最好这样写:
程序代码:
    class cp
    {
        public double sum(double[] t)
        {
            double result = 0;
            foreach (double i in t)
            {
                result = result + i;
            }
            return result;
        }

        public double sumx(PointF[] tp)//求X的和
        {
            double[] temp = new double[tp.Length];
            for (int i = 0; i < tp.Length; i++)
            {
                temp[i] = tp[i].X;
            }
            return mycp.sum(temp);
        }
        public double sumy(PointF[] tp)//Y的和
        {
            double[] temp = new double[tp.Length];
            for (int i = 0; i < tp.Length; i++)
            {
                temp[i] = tp[i].Y;
            }
            return mycp.sum(temp);
        }
        public double sumx2(PointF[] tp)//求X2的和
        {
            double[] temp = new double[tp.Length];
            for (int i = 0; i < tp.Length; i++)
            {
                temp[i] = Math.Pow(tp[i].X, 2);
            }
            return mycp.sum(temp);
        }
        public double sumx3(PointF[] tp)//求X3和
        {
            double[] temp = new double[tp.Length];
            for (int i = 0; i < tp.Length; i++)
            {
                temp[i] = Math.Pow(tp[i].X, 3);
            }
            return mycp.sum(temp);
        }
        public double sumx4(PointF[] tp)//求X4的和
        {
            double[] temp = new double[tp.Length];
            for (int i = 0; i < tp.Length; i++)
            {
                temp[i] = Math.Pow(tp[i].X, 4);
            }
            return mycp.sum(temp);
        }
        public double sumxy(PointF[] tp)//求XY的和
        {
            double[] temp = new double[tp.Length];
            for (int i = 0; i < tp.Length; i++)
            {
                temp[i] = tp[i].X * tp[i].Y;
            }
            return mycp.sum(temp);
        }
        public double sumx2y(PointF[] tp)//求X2Y的和
        {
            double[] temp = new double[tp.Length];
            for (int i = 0; i < tp.Length; i++)
            {
                temp[i] = Math.Pow(tp[i].X, 2) * tp[i].Y;
            }
            return mycp.sum(temp);
        }
    }
这个是定义计算的类,用的时候这样用:
程序代码:
            double d = 0;
            cp mycp=new cp();
            d = mycp.sumx2(1) - mycp.sumx(1) * mycp.sumx(1);
            ……
显得比较有条理。用的时候是在另一个所定义的类中的一个方法内。

2010-05-20 23:18
hailingmm
Rank: 2
等 级:论坛游民
帖 子:96
专家分:15
注 册:2009-10-15
收藏
得分:0 
回复 9楼 adu_to
明白了,谢谢!
2010-05-21 08:40
快速回复:如何定义类?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020452 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved