C# 练习题

26.   Enum(枚举)

/*
    枚举是一组命名整型常量。枚举类型是使用 enum 关键字声明的。
    C# 枚举是值类型。换句话说,枚举包含自己的值,且不能继承或传递继承。
*/

using System;

public class EnumTest
{
    enum Day { Sun, Mon, Tue, Wed, Thu, Fri, Sat };

    static void Main()
    {
        int x = (int)Day.Sun;
        int y = (int)Day.Fri;
        Console.WriteLine("Sun = {0}", x);
        Console.WriteLine("Fri = {0}", y);
    }
}


27.class(类定义)

/*
	类的定义是以关键字class开始,后跟类的名称。
	类的主体,包含在一对花括号内。
*/

using System;
namespace BoxApplication
{
    class Box
    {
        public double length;   // 长度
        public double breadth;  // 宽度
        public double height;   // 高度
    }
    class Boxtester
    {
        static void Main(string[] args)
        {
            Box Box1 = new Box();        // 声明 Box1,类型为 Box
            Box Box2 = new Box();        // 声明 Box2,类型为 Box
            double volume = 0.0;         // 体积

            // Box1 详述
            Box1.height = 5.0;
            Box1.length = 6.0;
            Box1.breadth = 7.0;

            // Box2 详述
            Box2.height = 10.0;
            Box2.length = 12.0;
            Box2.breadth = 13.0;

            // Box1 的体积
            volume = Box1.height * Box1.length * Box1.breadth;
            Console.WriteLine("Box1 的体积: {0}", volume);

            // Box2 的体积
            volume = Box2.height * Box2.length * Box2.breadth;
            Console.WriteLine("Box2 的体积: {0}", volume);
            Console.ReadKey();
        }
    }
}

28.成员函数和封装

/*
					成员函数和封装	
	1.类的成员函数是一个在类定义中有它的定义或原型的函数,就像其他变量一样。
	2.作为类的一个成员,它能在类的任何对象上操作,且能访问该对象的类的所有成员。
	3.成员变量是对象的属性(从设计角度),且它们保持私有来实现封装。
	4.这些变量只能使用公共成员函数来访问。
*/

using System;
namespace BoxApplication
{
    class Box
    {
        public double length;   // 长度
        public double breadth;  // 宽度
        public double heigth;   // 高度
		
		//封装长度函数
		public void setLength(double len)
        {
            length = len;
        }
		//封装宽度函数
        public void setBreadth(double bre)
        {
            breadth = bre;
        }
		//封装高度函数
        public void setHeigth(double hei)
        {
            heigth = hei;
        }
		
		//封装获取体积函数
        public double getVolume()
        {
            return length * breadth * heigth;
        }
    }
    class Boxtester
    {
        static void Main(string[] args)
        {
            Box Box1 = new Box();        // 声明 Box1,类型为 Box
            Box Box2 = new Box();        // 声明 Box2,类型为 Box
            double volume ;             // 定义体积,双精度值

            // Box1 详述
            Box1.heigth  = 5.0;
            Box1.length  = 6.0;
            Box1.breadth = 7.0;

            // Box2 详述
            Box2.heigth  = 10.0;
            Box2.length  = 12.0;
            Box2.breadth = 13.0;

            // Box1 的体积
            volume = Box1.getVolume();   //调用体积函数
            Console.WriteLine("Box1 的体积: {0}", volume);

            // Box2 的体积
            volume = Box2.getVolume();	//调用体积函数
            Console.WriteLine("Box2 的体积: {0}", volume);
    
            Console.ReadKey();
        }
    }
}

29.C#构造函数

/*
                    C#的构造函数
    类的 构造函数 是类的一个特殊的成员函数,当创建类的新对象时执行。

    构造函数的名称与类的名称完全相同,它没有任何返回类型。
    
 */

using System;
namespace LineApplication
{
    class Line
    {
        private double length;
        public Line()
        {
            Console.WriteLine("对象已创建");
        }
        public void setLength(double len)
        {
            length = len;
        }
        public double getLength() 
        {
            return length;
        }

        //主函数中执行
        static void Main(string[] args)
        {
            Line line = new Line();
            //设置线的长度
            line.setLength(6.0);
            Console.WriteLine("线条的长度为:{0}",line.getLength());

            Console.ReadKey();
        }
    }
}

30. 参数化构造函数

/*
 *  参数化构造函数
    类的构造函数,是类的一个特殊的成员函数,当创建类的新对象时执行。
    构造函数的名称与类的名称完全相同,它没有任何返回类型。  

    默认的构造函数没有任何参数。
    但是如果你需要一个带有参数的构造函数可以有参数,
    这种构造函数叫做参数化构造函数。
    这种技术可以帮助你在创建对象的同时给对象赋初始值,
    具体请看下面实例:
 
 */
using System;
namespace LineApplication
{
    class Line
    {
        private double length;   // 线条的长度
        public Line(double len)  // 参数化构造函数
        {
            Console.WriteLine("对象已创建,length = {0}", len);
            length = len;
        }

        public void setLength(double len)
        {
            length = len;
        }
        public double getLength()
        {
            return length;
        }

        static void Main(string[] args)
        {
            Line line = new Line(10.0);
            Console.WriteLine("线条的长度: {0}", line.getLength());
            // 设置线条长度
            line.setLength(6.0);
            Console.WriteLine("线条的长度: {0}", line.getLength());
            Console.ReadKey();
        }
    }
}

31.C# 析构函数 

/*
 *                  析构函数
 *  类的 析构函数 是类的一个特殊的成员函数,当类的对象超出范围时执行。

    析构函数的名称是在类的名称前加上一个波浪形(~)作为前缀,它不返回值,也不带任何参数。
    
    析构函数用于在结束程序(比如关闭文件、释放内存等)之前释放资源。析构函数不能继承或重载。
 * 
 */
using System;
namespace LineApplication
{
    class Line
    {
        private double length;  //定义线条长度

        public Line()   //构造函数
        {
            Console.WriteLine("对象已经创建");
        }
        ~Line()     //析构函数
        {
            Console.WriteLine("对象已经删除");
        }

        public void setLength(double len)
        {
            length = len;
        }
        public double getLength() 
        {
            return length;
        }

        static void Main(string[] args)
        {
            Line line = new Line(); //new一个新对象
            // 设置线条长度
            line.setLength(6.0);
            Console.WriteLine("线条的长度: {0}", line.getLength());
           
        }
    }
}

32.静态函数static

/*
     你也可以把一个成员函数声明为 static。
	 这样的函数只能访问静态变量。
	 静态函数在对象被创建之前就已经存在。
 */

using System;
namespace StaticVarApplication
{
    class StaticVar
    {
        public static int num;
        public void count() 
        {
            num++;
        }
        public static int getNum() 
        { 
            return num; 
        }
    }
    class StaticTester
    {
        static void Main(string[] args)
        { 
            StaticVar s1 = new StaticVar();
            StaticVar s2 = new StaticVar();
            s1.count();
           // s1.count();

            s2.count();
           // s2.count();
            Console.WriteLine("s1的变量是:{0}",s1.getNum());
            Console.WriteLine("s2的变量是:{0}",s2.getNum());

            Console.ReadKey();
        }
    }
}

33. C# 继承 

/*
	继承是面向对象程序设计中最重要的概念之一。
	继承允许我们根据一个类来定义另一个类,这使得创建和维护应用程序变得更容易。
	同时也有利于重用代码和节省开发时间。

	当创建一个类时,程序员不需要完全重新编写新的数据成员和成员函数,
	只需要设计一个新的类,继承了已有的类的成员即可。
	这个已有的类被称为的基类,这个新的类被称为派生类。

	继承的思想实现了 属于(IS-A) 关系。
	例如,哺乳动物 属于(IS-A) 动物,狗 属于(IS-A) 哺乳动物,
	因此狗 属于(IS-A) 动物。
*/

using System;
namespace InheritanceApplication
{
   class Shape 
   {
      public void setWidth(int w)
      {
         width = w;
      }
      public void setHeight(int h)
      {
         height = h;
      }
      protected int width;
      protected int height;
   }

   // 派生类
   class Rectangle: Shape
   {
      public int getArea()
      { 
         return (width * height); 
      }
   }
   
   class RectangleTester
   {
      static void Main(string[] args)
      {
         Rectangle Rect = new Rectangle();

         Rect.setWidth(5);
         Rect.setHeight(7);

         // 打印对象的面积
         Console.WriteLine("总面积: {0}",  Rect.getArea());
         Console.ReadKey();
      }
   }
}

 @学习来源于www.runoob.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值