您的位置:首页 > 编程语言 > C#

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: