VB.NET是否破坏了封装... [英] Is VB.NET breaking leaking encapsulation ...

查看:49
本文介绍了VB.NET是否破坏了封装...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




在VB.NET项目ClassLibrary1中考虑这段代码...


Public Interface ITest


函数名称()为字符串

函数年龄()为整数


结束界面


公共类测试

实现ITest


私人功能年龄()作为整数实现ITest.Age

返回10

结束函数


公共函数名称()如String实现ITest.Name

返回测试

结束功能


结束班级


以及一个新的C#(VB.NET)控制台项目和以上ClassLibrary1的参考


class Class1

{

///< summary>

///主要切入点申请。

///< / summary>

[STAThread]

static void Main(string [] args)

{

ClassLibrary1.ITest t = new ClassLibrary1.Test();

Console.WriteLin e(t.Age());

}

}


请注意,VB中的Age是私有的C#/ VB.NET通过

界面... -Hhh


C#不允许这样,因为它不会编译!


问候

Ashok

Hi,

Consider this code in VB.NET project ClassLibrary1...

Public Interface ITest

Function Name() As String
Function Age() As Integer

End Interface

Public Class Test
Implements ITest

Private Function Age() As Integer Implements ITest.Age
Return 10
End Function

Public Function Name() As String Implements ITest.Name
Return "Test"
End Function

End Class

And a new C# (VB.NET too) Console Project and referefer above ClassLibrary1

class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
ClassLibrary1.ITest t = new ClassLibrary1.Test();
Console.WriteLine(t.Age());
}
}

Note that Age is private in VB is getting accessing from C#/VB.NET through
interface... -Huh

C# doen''t allow this as it won''t compile!

Regards
Ashok

推荐答案

AshokG写道:
AshokG wrote:




在VB.NET项目ClassLibrary1中考虑此代码...


Public Interface ITest


函数名称()为字符串

函数年龄()为整数


结束界面


公共类测试

实现ITest


私有函数年龄()作为整数实现ITest.Age

返回10

结束函数


公共函数名称()字符串实现ITest.Name

返回"测试"

结束功能


结束课程


和一个新的C#(VB.NET)控制台项目和上面的ClassLibrary1


class Class1

{

///< summary>

///应用程序的主要入口点。

///< / summary> ;

[STAThread]

static void Main(string [] args)

{

ClassLibrary1.ITest t = new ClassLibrary1.Test();

Console.WriteLine(t.Age());

}

}


注意,VB中的Age是私有的,是通过

接口从C#/ VB.NET访问... -Hhh


C#不允许这样做,因为它不会编译!


问候

Ashok
Hi,

Consider this code in VB.NET project ClassLibrary1...

Public Interface ITest

Function Name() As String
Function Age() As Integer

End Interface

Public Class Test
Implements ITest

Private Function Age() As Integer Implements ITest.Age
Return 10
End Function

Public Function Name() As String Implements ITest.Name
Return "Test"
End Function

End Class

And a new C# (VB.NET too) Console Project and referefer above ClassLibrary1

class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
ClassLibrary1.ITest t = new ClassLibrary1.Test();
Console.WriteLine(t.Age());
}
}

Note that Age is private in VB is getting accessing from C#/VB.NET through
interface... -Huh

C# doen''t allow this as it won''t compile!

Regards
Ashok



您将Age方法与Age方法混淆。 :)


课堂上实际上有两种方法Age。

测试类中的方法Age是私有的。 ITest界面中的Age方法是公开的。

它们碰巧是相同的方法,碰巧有相同的名称。


当方法有与其实现的方法不同的名称,

它变得不那么混乱:

私有函数答案()作为整数实现IQuestion.Reply

返回42

结束功能

方法答案是私有的,但方法回复是公开的。

In C#它有点严格,因而不那么令人困惑。

实现界面的方法必须与界面中的名称相同,

并且必须公开。


-

G?跑Andersson

_____
http://www.guffa.com


G?ran Andersson,


谢谢回复。即使你在实现中重命名方法它也会被调用。


我的观点是允许通过接口调用私有方法。如果您使用课程名称进行学习,那么您就不能打电话。


因此,不应允许私人成员从课外打电话和<同样地,它不应该允许接口实现的成员声明为

私人


问候,

Ashok


" G?ran Andersson" < gu *** @ guffa.com写信息

新闻:OC ************** @ TK2MSFTNGP04.phx.gbl ...
G?ran Andersson,

Thanks for the reply. Even if you rename the method in the implementation it
get''s called.

My point here is allowing to call a private method through the Interface. If
you delcare with class name then you can''t call.

So, Private members should not be allowed to call from outside the class and
similarly it should not allow Interface implemented member to declared as
Private

Regards,
Ashok

"G?ran Andersson" <gu***@guffa.comwrote in message
news:OC**************@TK2MSFTNGP04.phx.gbl...

AshokG写道:
AshokG wrote:

>

在VB.NET中考虑这段代码project ClassLibrary1 ...

公共接口ITest

函数名称()为字符串
函数Age()为整数

结束接口

公开课测试
实现ITest

私函数年龄()作为整数实现ITest.Age
返回10
结束函数

公共函数Name()As String Implements ITest.Name
返回测试
结束功能

结束类

还有一个新的C#(VB.NET)控制台项目和上面的参考
ClassLibrary1

类Class1
{
///< summary>
///应用程序的主要入口点。
///< / summary>
[STAThread]
static void Mai n(string [] args)
{ClassLibrary1.ITest t = new ClassLibrary1.Test();
Console.WriteLine(t.Age());
}
}

请注意,VB中的Age是私有的,是通过接口从C#/ VB.NET访问的...... -Huh

C#doen''因为它不会编译而允许这个!

问候
Ashok
>Hi,

Consider this code in VB.NET project ClassLibrary1...

Public Interface ITest

Function Name() As String
Function Age() As Integer

End Interface

Public Class Test
Implements ITest

Private Function Age() As Integer Implements ITest.Age
Return 10
End Function

Public Function Name() As String Implements ITest.Name
Return "Test"
End Function

End Class

And a new C# (VB.NET too) Console Project and referefer above
ClassLibrary1

class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
ClassLibrary1.ITest t = new ClassLibrary1.Test();
Console.WriteLine(t.Age());
}
}

Note that Age is private in VB is getting accessing from C#/VB.NET
through interface... -Huh

C# doen''t allow this as it won''t compile!

Regards
Ashok



你把Age方法和Age混淆了方法。 :)


课堂上实际上有两种方法Age。

测试类中的方法Age是私有的。 ITest界面中的Age方法是公开的。

它们碰巧是相同的方法,碰巧有相同的名称。


当方法有与其实现的方法不同的名称,

它变得不那么混乱:

私有函数答案()作为整数实现IQuestion.Reply

返回42

结束功能

方法答案是私有的,但方法回复是公开的。


在C#中它更加严格,因而不那么令人困惑。

实现界面的方法必须与界面中的名称相同,

并且必须公开。


-

G?跑Andersson

_____
http://www.guffa.com



On 6 Ago,08:56,AshokG < gw2ks ... @ hotmail.comwrote:
On 6 Ago, 08:56, "AshokG" <gw2ks...@hotmail.comwrote:




在VB.NET项目ClassLibrary1中考虑此代码...


公共接口ITest


函数名称()字符串

函数年龄()为整数


结束界面


公共类测试

实现ITest


私函数年龄()为整数实现ITest.Age

返回10

结束函数


公共函数名称()As String Implements ITest.Name

返回测试

结束功能


结束班级


和一个新的C#(VB.NET)控制台项目和上面的ClassLibrary1


class Class1

{

///< summary>

///应用程序的主要入口点。

///< / summary>

[STAThread]

static void Main( string [] args)

{

ClassLibrary1.ITest t = new ClassLibrary1.Test();

Console.WriteLine(t.Age( ));

}


}


请注意,VB中的Age是私有的,是从C#访问的VB.NET通过

界面... -Hhh


C#不允许这样,因为它不会编译!


问候

Ashok
Hi,

Consider this code in VB.NET project ClassLibrary1...

Public Interface ITest

Function Name() As String
Function Age() As Integer

End Interface

Public Class Test
Implements ITest

Private Function Age() As Integer Implements ITest.Age
Return 10
End Function

Public Function Name() As String Implements ITest.Name
Return "Test"
End Function

End Class

And a new C# (VB.NET too) Console Project and referefer above ClassLibrary1

class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
ClassLibrary1.ITest t = new ClassLibrary1.Test();
Console.WriteLine(t.Age());
}

}

Note that Age is private in VB is getting accessing from C#/VB.NET through
interface... -Huh

C# doen''t allow this as it won''t compile!

Regards
Ashok



Nope。在VB中,如果你试试这个,你会看到你从编译器得到一个投诉




Public Interface ITest

函数名称()为字符串

函数年龄()为整数

结束界面


类ClassLibrary

实现ITest


公共类测试

实现ITest


私有功能年龄()作为整数实现ITest .Age

返回10

结束函数


公共函数名称()As String Implements ITest.Name

返回测试

结束功能


结束班级


结束班级

Public Class Form1


Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理MyBase.Load

Dim t As New ClassLibrary

MsgBox(t.Test)

End Sub


End Class


它想要:

公共职能年龄()A ■整数实现ITest.Age


结束函数


公共函数名称()As String Implements ITest.Name

>
结束功能

编译器自动提供它们。

T


Nope. Within VB, if you try this, you will see you get a complaint
from the compiler:

Public Interface ITest
Function Name() As String
Function Age() As Integer
End Interface

Class ClassLibrary
Implements ITest

Public Class Test
Implements ITest

Private Function Age() As Integer Implements ITest.Age
Return 10
End Function

Public Function Name() As String Implements ITest.Name
Return "Test"
End Function

End Class

End Class
Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim t As New ClassLibrary
MsgBox(t.Test)
End Sub

End Class

It wants:
Public Function Age() As Integer Implements ITest.Age

End Function

Public Function Name() As String Implements ITest.Name

End Function
The compiler autonatically provides them.
T



这篇关于VB.NET是否破坏了封装...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆