日期:2014-05-17  浏览次数:21216 次

C#--第四周实验--任务2--定义一个描述坐标点的CPoint类,利用(默认参数值)构造函数传递参数。(控制台应用)
/* (程序头部注释开始)
 * 程序的版权和版本声明部分
 * Copyright (c) 2011, 烟台大学计算机学院学生 
 * All rights reserved.
 * 文件名称:定义一个描述坐标点的CPoint类,利用构造函数传递参数。
 * 作 者: 雷恒鑫 
 * 完成日期: 2012 年 09 月 22 日
 * 版 本 号: V1.0 
 * 对任务及求解方法的描述部分
 * 输入描述:
 * 问题描述:
 * 程序输出:
 * 程序头部的注释结束
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Four__week
{
    class Program
    {
        static void Main(string[] args)
        {
            CPoint c = new CPoint();
            Console.Write("默认参数值为:");
            c.display();
            c.setpoint(80, 150);
            Console.Write("修改后的参数值为:");
            c.display();
            Console.ReadKey(true);
        }
    }

    class CPoint
    {
        private int x;
        private int y;

        public CPoint(int x1,int y2)
        {
            x = x1;
            y = y2;
        }

        public CPoint()
        {
            x = 60;
            y = 75;

        }

        public void display()
        {
            Console.WriteLine("x = {0}   y = {1}",x,y);
        }
        public void setpoint(int x1, int y1)
        {
            x = x1;
            y = y1;
        }
    }
}


运行结果:

疑问:

为什么下面这种写法不行呢?

public CPoint(int x1 = 3,int y2 = 6)
        {
            x = x1;
            y = y2;
        }

系统在“x1=3”处提示不允许有默认参数说明符。