嵌套类

来源:互联网 发布:头颅移植 知乎 编辑:程序博客网 时间:2024/05/17 03:50

在类的内部或结构的内部定义的类型称为嵌套类型,又称内部类型

namespace 例5_6.嵌套类
{
    class Program
    {
        static void Main(string[] args)
        {
            int x1 = Convert.ToInt32(Console .ReadLine());
            int x2 = Convert.ToInt32(Console.ReadLine());
            int y1 = Convert.ToInt32(Console.ReadLine());
            int y2 = Convert.ToInt32(Console.ReadLine());
            Rectangle my = new Rectangle(x1, y1, x2, y2);
            Console.WriteLine(my.Area());
            Console.ReadLine();
        }
    }
    class Rectangle
    {
        private Point topLeft;
        private Point bottomRight;
        public Rectangle(int lx, int ly, int rx, int ry)
        {
            topLeft = new Point(lx,ly);     //实例化
            bottomRight = new Point(rx, ry);
        }
        class Point    //内部类
        {
            private int x;
            private int y;
            public Point(int x, int y)
            {
                this.x = x;
                this.y = y;
            }
            public int X    //返回x的值
            {
                get { return x; }
            }
            public int Y    //返回y的值
            {
                get { return y; }
            }
            
        }
        public int Area()
        {
            return (bottomRight.X - topLeft.X) * (bottomRight.Y - topLeft.Y);
        }
    }
}

0 0
原创粉丝点击