• 类与对象(矩形类)


    创建一个矩形类,包含长、宽属性和求面积方法。类中包含一个静态Count属性,当创建一个矩形时,Count++。
    使用静态构造函数为静态属性赋初值。
    在测试类中定义两个方法(static):
    (1)判断矩形是否为正方形
    (2)扩大矩形,调整矩形的长和宽
    在测试类中创建一个矩形,并判断矩形是否为正方形扩大矩形,显示矩形的长宽和面积。

    namespace ConsoleApp2
    {
        class Program
        {
            static void Main(string[] args)
            {
                Rectangle rect2 = new Rectangle() { Width = 4, Height = 4 };
                Rectangle.Count++;
                Console.WriteLine(rect2.Height + "," + rect2.Width);
                bool result = IsSquare(rect2);
                ScaleRect(rect2);
                Console.WriteLine(result);
                Console.WriteLine(rect2.Height + "," + rect2.Width);
                Console.WriteLine("矩形的面积是{0}", rect2.Area());
                Console.ReadLine();
            }
            static bool IsSquare(Rectangle rect)
            {
                if (rect.Width == rect.Height)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            static void ScaleRect(Rectangle rect)
            {
                rect.Width = 10;
                rect.Height = 12;
            }
            public class Rectangle
            {
                static public int Count { set; get; }
                public int Width { set; get; }
                public int Height { set; get; }
                public int Area()
                {
                    return Width * Height;
                }        
            }
            
        }
    }
  • 相关阅读:
    产生渐变色的view
    新建一个去除storyboard的项目
    绘制波形图
    文件系统扫描的工具类
    如何动态绘制时钟
    Xcode插件管理器Alcatraz的使用
    UITableView的headerView展开缩放动画
    POPSpring动画参数详解
    Java枚举类使用和总结
    Java判断不为空的工具类总结
  • 原文地址:https://www.cnblogs.com/programme-maker/p/11466651.html
Copyright © 2020-2023  润新知