可以的,把项目的类型设成类库,将所有的函数用Public修饰附封装在类里面,生成dll文件。这样别人就可以在别人项目属性的引用页里面添加对你的dll文件的引用,然后导入命名空间,直接使用了。
化隆网站制作公司哪家好,找创新互联公司!从网页设计、网站建设、微信开发、APP开发、自适应网站建设等网站项目制作,到程序开发,运营维护。创新互联公司自2013年创立以来到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联公司。
比如说,在一个项目里:
NameSpace Controller
Public Class ControlMachine
Public Sub Boot()
End Sub
Public Sub Shutdown()
End Sub
End Class
End NameSpace
进入另一个项目的项目属性,进入引用页,添加到那个dll文件的引用。在代码中:
Imports Controller '加在代码文档的最顶端
使用:
Dim controller As New ControlMachine
controller.Boot()
controller.Shutdown()
希望你能了解,不懂再追问
在VB.NET的类里,实现一个接口的语句是:
implements接口名称
例如,下面定义一个车(总称)的接口,这里的车是各种车的总称:
Public Interface ICar
Property color() As String
Property speed() As Long
Sub PrintInfo()
End Interface
然后在不同类型的“车”类里实现它:
Public Class A_Car
Implements ICar
Dim m_color As String, m_speed As Long
Public Property color() As String Implements ICar.color
Get
Return m_color
End Get
Set(ByVal Value As String)
m_color = Value
End Set
End Property
Protected Overrides Sub Finalize()
MsgBox(I was deconstructed!)
End Sub
Public Sub New()
m_color = Red
m_speed = 210
MsgBox(I was constructed!)
End Sub
Public Property speed() As Long Implements ICar.speed
Get
Return m_speed
End Get
Set(ByVal Value As Long)
m_speed = speed
End Set
End Property
Public Sub PrintInfo() Implements ICar.PrintInfo
MsgBox(Color: m_color vbNewLine Speed: m_speed, MsgBoxStyle.Information)
End Sub
End Class
在 Form 的 Load 事件中编写:
Dim myCar As New A_Car
myCar.PrintInfo()
运行之后就创建了一个 A_Car 类的实例 myCar,然后出现两个对话框,分别说明实例已经创建和汽车的信息。当窗体卸载时,这个类的实例 myCar 将自动销毁,这时将出现一个“I was deconstructed!”的对话框。
声明一个接口时,需要考虑以下几点:
1.接口主体只限于对方法,索引器以及属性的声明;
2.接口成员是隐式公开的,如果对其显式指定访问级别,就会出现编译器错误;
3.接口中不能包含字段,构造函数和常量等;
4.在接口中不能实现任何方法,属性或者索引器;
5.在指定方法时,只需要给出返回类型,名称和参数列表,然后分号结束。
面向对象的接口
在C++中,一个类被允许继承多个类。但是在Java以后的语言不被允许。
这样,如果想继承多个类时便非常困难。所以开发方想出了新办法:接口。
一个接口内,允许包含变量、常量等一个类所包含的基本内容。但是,接口中的函数不允许设定代码,也就意味着不能把程序入口放到接口里。由上可以理解到,接口是专门被继承的。接口存在的意义也是被继承。和C++里的抽象类里的纯虚函数是相同的。不能被实例化。
定义接口的关键字是interface,例如:
publicinterfaceMyInterface{
public void add(int x,int y);
public void volume(int x,int y,int z);
}
继承接口的关键字是implements,相当于继承类的extends。
需要注意的是,当继承一个接口时,接口里的所有函数必须全部被覆盖。例如:
class Demo implements MyInterface{
public void add(int x,int y){
System.out.println( +(x+y));
}
public void volume(int x,int y,int z){
System.out.println( +(x*y*z));
}
public static void main(String args[]){
Demo d=new Demo();
d.add(10,20);
d.volume(10,10,10);
}
}
输出结果:
30
1000
当想继承多个类时,开发程序不允许,报错。这样就要用到接口。因为接口允许多重继承(,),而类不允许。所以就要用到接口。
Imports System.Data.OleDb
Public Class Parking
Private Sub Parking_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Now_Timer.Enabled = True
End Sub
Private Sub Now_Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Now_Timer.Tick
Now_Time_Label.Text = "当前时间:" Date.Now
End Sub
Private Sub Enter_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Enter_Button.Click
'定义一个OLEDB连接字符串
Dim conStr As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=F:\test\test.mdb"
'实例化OLEDB连接
Dim con As OleDbConnection = New OleDbConnection(conStr)
Dim sql As New System.Text.StringBuilder
'定义数据库插入语句
sql.Append("insert into Time_billing([Car_Num],[Enter_Time])")
sql.Append("values('" Trim(Car_Num_Text.Text) "','" Date.Now "')")
'打开数据库链接
con.Open()
'定义执行命令
Dim cmd As New System.Data.OleDb.OleDbCommand(sql.ToString, con)
'执行命令
cmd.ExecuteNonQuery()
'关闭数据库链接
con.Close()
MsgBox("提交成功!")
End Sub
Private Sub Leave_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Leave_Button.Click
Dim Time_Length As Double
Dim Pack_Fee As Double
Dim Enter_time As Date
Dim Leave_time As Date
Dim conStr As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=F:\test\test.mdb"
Dim con As OleDbConnection = New OleDbConnection(conStr)
Dim selSql As New System.Text.StringBuilder
Dim inSql As New System.Text.StringBuilder
Dim upSql As New System.Text.StringBuilder
Dim delSql As New System.Text.StringBuilder
Dim dr As OleDbDataReader
con.Open()
'SQL拼接过程中最后不需要有分号
'在赋值时,读取字段值时必须使用在数据库客户端查询时显示的字段名
selSql.Append("select")
selSql.Append(" Enter_Time")
selSql.Append(" from [Time_billing]")
selSql.Append("where Car_Num = '").Append(Trim(Car_Num_Text.Text)).Append("'")
Dim selcmd As New OleDb.OleDbCommand(selSql.ToString, con)
dr = selcmd.ExecuteReader()
If dr.Read() Then
Enter_time = dr("Enter_Time")
Leave_time = Date.Now
Else
MsgBox("木有数据!")
End If
Enter_Time_Text.Text = Enter_time
Leave_Time_Text.Text = Leave_time
'求时间差
Time_Length = Math.Round(DateDiff(DateInterval.Minute, Enter_time, Leave_time) / 60, 2)
Pack_Fee = Time_Length * 5
Pack_Fee_Text.Text = Pack_Fee
inSql.Append("update [Time_billing]")
inSql.Append(" set [Leave_Time] = '").Append(Trim(Leave_Time_Text.Text)).Append("'")
inSql.Append(" ,[Packing_Fee] = '").Append(Pack_Fee).Append("'")
inSql.Append("where Car_Num = '").Append(Trim(Car_Num_Text.Text)).Append("'")
Dim incom As New OleDb.OleDbCommand(inSql.ToString, con)
incom.ExecuteNonQuery()
'con.Close()
MsgBox("结算完成!")
'con.Open()
upSql.Append("insert into Time_billing_History([Car_Num],[Enter_Time],[Leave_Time],[Packing_Fee])")
upSql.Append(" select")
upSql.Append(" [Car_Num]")
upSql.Append(",[Enter_Time]")
upSql.Append(",[Leave_Time]")
upSql.Append(",[Packing_Fee]")
upSql.Append(" from [Time_billing]")
upSql.Append("where [Car_Num] = '").Append(Trim(Car_Num_Text.Text)).Append("'")
Dim upcom As New OleDb.OleDbCommand(upSql.ToString, con)
upcom.ExecuteNonQuery()
delSql.Append("delete")
delSql.Append(" from")
delSql.Append(" [Time_billing]")
delSql.Append(" where [Car_Num] = '").Append(Trim(Car_Num_Text.Text)).Append("'")
Dim delcom As New OleDb.OleDbCommand(delSql.ToString, con)
delcom.ExecuteNonQuery()
con.Close()
End Sub
Private Sub Clear_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Clear_Button.Click
Car_Num_Text.Clear()
Enter_Time_Text.Clear()
Leave_Time_Text.Clear()
Pack_Fee_Text.Clear()
End Sub
End Class