当前位置: 首页 > news >正文

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,类型为 BoxBox Box2 = new Box();        // 声明 Box2,类型为 Boxdouble 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,类型为 BoxBox Box2 = new Box();        // 声明 Box2,类型为 Boxdouble 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

http://www.lryc.cn/news/149861.html

相关文章:

  • 解决Linux报错:Swap file “xxxxxx.swp“ already exists
  • 基于飞桨图学习框架的空间异配性感知图神经网络
  • Springboot整合JWT
  • 如何使用Python和正则表达式处理XML表单数据
  • LA@方阵相似@相似矩阵的性质
  • ZLMediaKit 各种推拉流
  • 行业追踪,2023-08-29
  • 【简单】228. 汇总区间
  • Mysql高级语句
  • Python中 re.compile 函数的使用
  • 【分布式搜索引擎es】
  • 单片机的ADC
  • 如何把pdf文件合并?分享最新pdf合并方法
  • 笙默考试管理系统-MyExamTest----codemirror(11)
  • Spring MVC 五 - Spring MVC的配置和DispatcherServlet初始化过程
  • Ramp 有点意思的题目
  • 算法通关村14关 | 堆在数组中找第k大的元素应用
  • Unity 顶点vertices,uv,与图片贴图,与mesh
  • Shell编程之函数
  • 10.物联网LWIP之TCP状态转变
  • Img标签的src地址自动拼接本地域名(localhost:8080)导致图片不显示问题
  • 数据结构入门 — 栈
  • Unity Android 之 在Unity 中引入 OkHttp的操作注意(OKHttp4.xx- kotlin 的包)简单记录
  • 内嵌功能强大、低功耗STM32WB55CEU7、STM32WB55CGU7 射频微控制器 - MCU, 48-UFQFN
  • 【测试】笔试03
  • JavaScript的while和for循环
  • mqtt安卓客户端
  • pdf怎么删除其中一页?
  • 10.Redis 渐进式遍历
  • 字符函数和字符串函数(2)