C# class示例 0012
2016-04-22 05:18
513 查看
Defining a Class
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BoxApplication { class Box { public double length; public double width; public double height; } class Boxtester { static void Main(string[] args) { Box Box1 = new Box(); Box Box2 = new Box(); double volume = 0.0; Box1.height = 5.0; Box1.width = 4.0; Box1.length = 3.0; Box2.height = 3.0; Box2.width = 5.0; Box2.length= 4.0; // volumne of Box1 volume = Box1.height * Box1.width * Box1.length; Console.WriteLine("volume of Box1 {0}", volume); // volumne of Box2 volume = Box2.height * Box2.width * Box2.length; Console.WriteLine("volume of Box2 {0}", volume); Console.ReadKey(); } } }
output:
volume of Box1 60
volume of Box2 60
Member Functions and Encapsulation
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BoxApplication { class Box { public double length; public double width; public double height; public void setLength(double len) { length = len; } public void setWidth(double wid) { width = wid; } public void setHeight(double hei) { height = hei; } public double getVolume() { return height * length * width; } } class Boxtester { static void Main(string[] args) { Box Box1 = new Box(); Box Box2 = new Box(); double volume; Box1.setLength(6.0); Box1.setHeight(4.0); Box1.setWidth(3.0); // Box2 specification Box2.setLength(12.0); Box2.setHeight(14.0); Box2.setWidth(13.0); // volumne of Box1 volume = Box1.getVolume(); Console.WriteLine("volume of Box1 {0}", volume); // volumne of Box2 volume = Box2.getVolume(); Console.WriteLine("volume of Box2 {0}", volume); Console.ReadKey(); } } }
output:
volume of Box1 72
volume of Box2 2184
C# Constructor
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LineApplication { class Line { private double length; // Length of Line public Line() { Console.WriteLine("Object is being crated"); } public void setLength(double len) { length = len; } public double getLength() { return length; } static void Main(string[] args) { Line l = new Line(); // set line length l.setLength(6.0); Console.WriteLine("Length of line : {0}", l.getLength()); Console.ReadKey(); } } }
output:
Object is being crated
Length of line : 6
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LineApplication { class Line { private double length; // Length of Line public Line( double len) // Parameterized constructor { Console.WriteLine("Object is being crated",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("Length of line : {0}", line.getLength()); // set line length line.setLength(6.0); Console.WriteLine("Length of line : {0}", line.getLength()); Console.ReadKey(); } } }
output:
Object is being crated
Length of line : 10
Length of line : 6
C# Destructors
Destructor can be very useful for releasing memory resources before exiting the program. Destructors cannot be inherited or overloaded.
using System; namespace LineApplication { class Line { private double length; // Length of Line public Line( ) // constructor { Console.WriteLine("Object is being crated"); } ~ Line() { // destructor Console.WriteLine("Object is being deleted"); } public void setLength(double len) { length = len; } public double getLength() { return length; } static void Main(string[] args) { Line line = new Line(); // set line length line.setLength(6.0); Console.WriteLine("Length of line : {0}", line.getLength()); Console.ReadKey(); } } }
output:
Object is being crated
Length of line : 6
why destructor not excuted?
Static Member of a C# Class
using System; namespace StaticVarApplication { class staticVar { public static int num; public void count() { num++; } public int getNum() { return num; } class StaticTester { static void Main(string[] args) { staticVar s1 = new staticVar(); staticVar s2 = new staticVar(); s1.count(); s1.count(); s1.count(); s2.count(); s2.count(); s2.count(); Console.WriteLine("Varible num for s1:{0}", s1.getNum()); Console.WriteLine("Varible num for s2:{0}", s2.getNum()); Console.ReadKey(); } } } }
output:
Varible num for s1:6
Varible num for s2:6
when I write wrong the
Console.WriteLine(“Varible num for s2:{ 0}”, s2.getNum());
It’s occured An unhandled exception of type ‘System.FormatException’ occurred in mscorlib.dll
static Method
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(); s2.count(); Console.WriteLine("Varible num for s1:{0}", staticVar.getNum()); Console.WriteLine("Varible num for s2:{0}", staticVar.getNum()); Console.ReadKey(); } } } }
output:
Varible num for s1:2
Varible num for s2:2
相关文章推荐
- XML转换
- CSharpGL(17)重构CSharpGL
- 《CLR via C#:框架设计》读书笔记 - 委托
- 《CLR via C#:框架设计》读书笔记 - 计算限制的异步操作
- 《CLR via C#:框架设计》读书笔记 - 线程基础
- 《CLR via C#:框架设计》读书笔记 - 运行时序列化
- 《CLR via C#:框架设计》读书笔记 - 程序集加载和反射
- 《CLR via C#:框架设计》读书笔记 - GC(垃圾回收)
- 利用C#自带组件强壮程序日志
- C# 调用网易“易盾” Web API
- C#中ToString() 的一些常用用法
- 【C#进阶系列】23 程序集加载和反射
- 图解C# 调用Win32 API 示例程序
- C#中String和string有什么区别
- C#基础之--线程、任务和同步:一、异步委托
- C#入门经典(第6版)
- C# structures 0011
- C#—异常应用(正则表达式)
- 计算阶乘-当输入的数据是带小数时引发异常
- C#—异常类