可以的,把項目的類型設(shè)成類庫,將所有的函數(shù)用Public修飾附封裝在類里面,生成dll文件。這樣別人就可以在別人項目屬性的引用頁里面添加對你的dll文件的引用,然后導(dǎo)入命名空間,直接使用了。
化隆網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、自適應(yīng)網(wǎng)站建設(shè)等網(wǎng)站項目制作,到程序開發(fā),運營維護。創(chuàng)新互聯(lián)公司自2013年創(chuàng)立以來到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)公司。
比如說,在一個項目里:
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的類里,實現(xiàn)一個接口的語句是:
implements接口名稱
例如,下面定義一個車(總稱)的接口,這里的車是各種車的總稱:
Public Interface ICar
Property color() As String
Property speed() As Long
Sub PrintInfo()
End Interface
然后在不同類型的“車”類里實現(xiàn)它:
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()
運行之后就創(chuàng)建了一個 A_Car 類的實例 myCar,然后出現(xiàn)兩個對話框,分別說明實例已經(jīng)創(chuàng)建和汽車的信息。當(dāng)窗體卸載時,這個類的實例 myCar 將自動銷毀,這時將出現(xiàn)一個“I was deconstructed!”的對話框。
聲明一個接口時,需要考慮以下幾點:
1.接口主體只限于對方法,索引器以及屬性的聲明;
2.接口成員是隱式公開的,如果對其顯式指定訪問級別,就會出現(xiàn)編譯器錯誤;
3.接口中不能包含字段,構(gòu)造函數(shù)和常量等;
4.在接口中不能實現(xiàn)任何方法,屬性或者索引器;
5.在指定方法時,只需要給出返回類型,名稱和參數(shù)列表,然后分號結(jié)束。
面向?qū)ο蟮慕涌?/p>
在C++中,一個類被允許繼承多個類。但是在Java以后的語言不被允許。
這樣,如果想繼承多個類時便非常困難。所以開發(fā)方想出了新辦法:接口。
一個接口內(nèi),允許包含變量、常量等一個類所包含的基本內(nèi)容。但是,接口中的函數(shù)不允許設(shè)定代碼,也就意味著不能把程序入口放到接口里。由上可以理解到,接口是專門被繼承的。接口存在的意義也是被繼承。和C++里的抽象類里的純虛函數(shù)是相同的。不能被實例化。
定義接口的關(guān)鍵字是interface,例如:
publicinterfaceMyInterface{
public void add(int x,int y);
public void volume(int x,int y,int z);
}
繼承接口的關(guān)鍵字是implements,相當(dāng)于繼承類的extends。
需要注意的是,當(dāng)繼承一個接口時,接口里的所有函數(shù)必須全部被覆蓋。例如:
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);
}
}
輸出結(jié)果:
30
1000
當(dāng)想繼承多個類時,開發(fā)程序不允許,報錯。這樣就要用到接口。因為接口允許多重繼承(,),而類不允許。所以就要用到接口。
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 = "當(dāng)前時間:" 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
'定義數(shù)據(jù)庫插入語句
sql.Append("insert into Time_billing([Car_Num],[Enter_Time])")
sql.Append("values('" Trim(Car_Num_Text.Text) "','" Date.Now "')")
'打開數(shù)據(jù)庫鏈接
con.Open()
'定義執(zhí)行命令
Dim cmd As New System.Data.OleDb.OleDbCommand(sql.ToString, con)
'執(zhí)行命令
cmd.ExecuteNonQuery()
'關(guān)閉數(shù)據(jù)庫鏈接
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拼接過程中最后不需要有分號
'在賦值時,讀取字段值時必須使用在數(shù)據(jù)庫客戶端查詢時顯示的字段名
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("木有數(shù)據(jù)!")
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("結(jié)算完成!")
'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