Loading

C#、VB.ENT类的使用

类的使用

  1. 类的定义

    1:类是一种数据结构,类的实例叫做对象。类是对现实生活中一类具有共同特征的事物的抽象。

    2:类(Class)是面向对象程序设计(OOP,Object-Oriented Programming)实现信息封装的基础。

    3:面向对象程序设计(OOP)的特点:

    (1):OOP与"面向过程"(P0P,Procedure-Oriented Programming)的最大不同,POP中程序=算法+数据对象,OOP中程序=对象A+对象B+……,而对象=算法+数据结构

    (2):OOP的编程方式具有封装继承多态等特点。

  2. 类的使用

    1:声明一个类后,需实例化为具体的对象后才可操纵其非静态成员。一般来说一个对象的生命周期是从构造函数开始,由析构函数结束。

    2:类的创建

    C#:

class CarClass//汽车类

{

public string CarColor;//汽车颜色

public double Length;//汽车长度

public double Width;//汽车宽度

public double Heigth;//汽车高度

public double Volume()//汽车体积(构造函数Volume())

{

return Length * Width * Heigth;

}

public CarClass()//可带参数

{

Console.WriteLine("初始化汽车类为具体对象");

}

~CarClass()

{

Console.WriteLine("销毁该汽车类对象");

}

}

class SportCarClass : CarClass //跑车类继承汽车类

{

public string speedability;//快速性

}

 

 

VB.NET:

Class CarClass

Public CarColor As String '汽车颜色

Public Length As Double '汽车的长度

Public Width As Double '汽车的宽度

Public Heigth As Double '汽车的高度

Function Volume() '汽车的体积(构造函数Volume())

Return Length * Width * Heigth

End Function

'构造函数

Public Sub New()'可带参数

Console.WriteLine("初始化汽车类为具体对象")

End Sub

'析构函数

Protected Overrides Sub Finalize() '重写Finalize方法

Console.WriteLine("销毁该汽车类对象")

End Sub

End Class

Class SportCarClass

Inherits CarClass '跑车类继承汽车类

Public speedability As String '快速性//快速性

End Class

3:类的使用

C#:

static void TestMath_2()

{

CarClass MyCar = new CarClass();

MyCar.CarColor = "红色";

MyCar.Length = 123.4;

MyCar.Width = 56.7;

MyCar.Heigth = 89.0;

Console.WriteLine("我的汽车的颜色:" + MyCar.CarColor);

Console.WriteLine(" 长度:" + MyCar.Length);

Console.WriteLine(" 宽度:" + MyCar.Width);

Console.WriteLine(" 高度:" + MyCar.Heigth);

Console.WriteLine(" 体积:" + MyCar.Volume());//通过公式计算返回值

}

static void TestMath_3()

{

SportCarClass MyCar = new SportCarClass();

MyCar.CarColor = "红色";

MyCar.Length = 123.4;

MyCar.Width = 56.7;

MyCar.Heigth = 89.0;

Console.WriteLine("我的跑车的颜色:" + MyCar.CarColor);

Console.WriteLine(" 长度:" + MyCar.Length);

Console.WriteLine(" 宽度:" + MyCar.Width);

Console.WriteLine(" 高度:" + MyCar.Heigth);

Console.WriteLine(" 体积:" + MyCar.Volume());//通过公式计算返回值

Console.WriteLine(" 快速性:百公里10S加速");

}

VB.NET:

Sub TestMath_2()

Dim MyCar As CarClass = New CarClass()

MyCar.CarColor = "红色"

MyCar.Length = 123.4

MyCar.Width = 56.7

MyCar.Heigth = 89.0

Console.WriteLine("我的汽车的颜色:" & MyCar.CarColor)

Console.WriteLine(" 长度:" & MyCar.Length)

Console.WriteLine(" 宽度:" & MyCar.Width)

Console.WriteLine(" 高度:" & MyCar.Heigth)

Console.WriteLine(" 体积:" & MyCar.Volume()) '通过公式计算返回值

End Sub

Sub TestMath_3()

Dim MyCar As SportCarClass = New SportCarClass()

MyCar.CarColor = "红色"

MyCar.Length = 123.4

MyCar.Width = 56.7

MyCar.Heigth = 89.0

Console.WriteLine("我的跑车的颜色:" & MyCar.CarColor)

Console.WriteLine(" 长度:" & MyCar.Length)

Console.WriteLine(" 宽度:" & MyCar.Width)

Console.WriteLine(" 高度:" & MyCar.Heigth)

Console.WriteLine(" 体积:" & MyCar.Volume()) '通过公式计算返回值

Console.WriteLine(" 快速性:百公里10S加速")

End Sub

4:使用效果:

备注:当我们结束该程序时可看到控制台显示"销毁该汽车类对象"。

 

posted @ 2017-11-04 17:10  Dwaynerbing  阅读(254)  评论(0)    收藏  举报