Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1705919
  • 博文数量: 171
  • 博客积分: 11553
  • 博客等级: 上将
  • 技术积分: 3986
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-25 20:28
文章分类

全部博文(171)

文章存档

2012年(2)

2011年(70)

2010年(9)

2009年(14)

2008年(76)

分类: C/C++

2008-03-30 01:06:49

using System;


public struct Point
{
    /*
     * 功能
     * struct不允许无参数的构造函数,如下:
     *
     * public Point(){}
     *
     * /
    /*
     * 功能
     * 构造一个Point对象
     * 参数
     * x in X坐标
     * y in Y坐标
     */

    public Point(int x,int y)
    {
        this.x = x;
       
        this.y = y;
    }
    /*
     * 演示struct的成员函数功能
     * 功能
     * 设置点的坐标
     * 参数
     * x in X坐标
     * y in Y坐标
     */

    public void SetPoint(int x,int y)
    {
        this.x = x;
        this.y = y;
    }
    /*
     * 演示struct的成员函数功能
     * 功能
     * 获取当前点
     * 参数
     * 无
     * 返回值
     * 当前这个点对象,注意这个是对象复制
     */

    public Point GetPoint()
    {
        Point pt;
        pt.x = x;
        pt.y = y;
        //这是对象复制操作

        return pt;
    }

    /*
     * 演示struct的属性功能
     * 创建一个X坐标的属性
     */

    public int X
    {
        get
        {
            return x;
        }
        set
        {
            x = value;
        }
    }
    /*
     * 演示struct的属性功能
     * 创建一个Y坐标的属性
     */

    public int Y
    {
        get
        {
            return y;
        }
        set
        {
            y = value;
        }
    }

    //演示struct的成员变量功能,struct也不允许使用初始化变量对成员进行赋值,如下

    //private int x = 0;

    //private int y = 0;

    private int x ;
    private int y ;

}

public class CallPoint
{
    public static void test()
    {
        //使用new来创建一个struct对象

        Point pt = new Point(5,5);
        Console.WriteLine("({0},{1})", pt.X, pt.Y);
        pt.X = 10;
        pt.Y = 20;
        Console.WriteLine("({0},{1})", pt.X, pt.Y);

        //测试成员函数

        pt.SetPoint(30, 30);

        //测试值传递

        Console.WriteLine("({0},{1})", pt.GetPoint().X, pt.GetPoint().Y);
    }
};


阅读(2829) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2009-10-19 14:09:57

G GHH 吧并不比发达挺好

chinaunix网友2009-10-19 14:09:34

123时代发给