1. 程式人生 > >C#--第四周實驗--任務2--定義一個描述座標點的CPoint類,利用(預設引數值)建構函式傳遞引數。(控制檯應用)

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”處提示不允許有預設引數說明符。