声明接口IPoint描述一个点的 坐标并实现该接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp12
{
    interface IPoint
    {
        int x { get; set; }//定义成员变量x,它含有读写抽象访问
        int y { get; set; }
    }
    class Point :IPoint
    {
        private int px;
        private int py;
        public Point(int x,int y)//构造函数
        {
            px = x;
            py = y;
        }
        public int x//接口属性实现
        {
            get { Console.WriteLine("\n调用接口读取数据px"); return px; }
            set { Console.WriteLine("\n调用接口写入数据px"); px = value; }

        }
        public int y//接口属性实现
        {
            get { Console.WriteLine("调用接口读取数据py"); return py; }
            set { Console.WriteLine("\n调用接口写入数据px"); py = value; }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Point p = new Point(4, 30);
            Console.Write("新创建的point点的坐标是:");
            p.x = 666;

            Console.WriteLine("x={0},y={1}",p.x,p.y);
            Console.ReadKey();

        }
    }
}

avatar