请大家帮忙:关于类模块

phil2360 2003-08-22 02:23:57
在类模块中用Friend声明属性和过程有什么用吗?和用Public或private声明有什么区别?请高手详细说明。
...全文
81 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
phil2360 2003-08-22
  • 打赏
  • 举报
回复
看了楼上几位解释的,我刚试了一下是不是应该这样,在类模块中用Friend声明的属性或方法在类模块中可见并可直接调用,在窗体模块了除非用As Classname声明变量,否则不能直接调用用Friend声明的属性和方法
hc_z 2003-08-22
  • 打赏
  • 举报
回复
试了一下,在ActiveDll中定义的Friend方法对用户也是不可见的。但在同一个工程(或工程组中)Friend方法都是可见的。
hc_z 2003-08-22
  • 打赏
  • 举报
回复
看了一下msdn,好像最主要的特性(和public相比)是不能被管理者(controller )调用。不太明白controller 是个什么角色。是否可以理解为:public定义的模块(窗体或类模块)可以被其他进程使用,而Friend只能被本工程(进程)继承使用。
qingming81 2003-08-22
  • 打赏
  • 举报
回复
看MSDN:

Visual Basic for Applications Reference

Friend


Modifies the definition of a procedure in a form module or class module to make the procedure callable from modules that are outside the class, but part of the project within which the class is defined. Friend procedures cannot be used in standard modules.

Syntax

[Private | Friend | Public] [Static] [Sub | Function | Property] procedurename

The required procedurename is the name of the procedure to be made visible throughout the project, but not visible to controllers of the class.

Remarks

Public procedures in a class can be called from anywhere, even by controllers of instances of the class. Declaring a procedure Private prevents controllers of the object from calling the procedure, but also prevents the procedure from being called from within the project in which the class itself is defined. Friend makes the procedure visible throughout the project, but not to a controller of an instance of the object. Friend can appear only in form modules and class modules, and can only modify procedure names, not variables or types. Procedures in a class can access the Friend procedures of all other classes in a project. Friend procedures don't appear in the type library of their class. A Friend procedure can't be late bound.


--------------------------------------------------------------------------------
Send feedback to MSDN.Look here for MSDN Online resources.
phil2360 2003-08-22
  • 打赏
  • 举报
回复
可以再讲具体点吗?还是有点不懂。还有所谓的前期绑定和后期绑定又是什么概念,请帮我结实以下。
qqqdong 2003-08-22
  • 打赏
  • 举报
回复
对象之间的私有通信


有时,部件的对象需要不受部件用户干涉地完成对象之间的通信。例如,Widgets 集合类可能要为新创建的 Widget 设置 Parent 属性,并将其设为只读。

类的公共方法可以被其它对象调用,也可以被客户端调用。私有方法则不能从部件外调用,且它对于部件内的其它对象也是不可见的。

解决办法是使用 Friend 方法。下面的代码段中,假想的 Widget 对象显露一个公共的只读 Parent 属性,以及一个 Widgets 集合用来为新创建的 Widget 设置其 Parent 属性的 Friend 方法(称为 SetParent)。

'Widget 总是 mechanism 的组成部分。
Private mmchParent As Mechanism

Public Property Get Parent() As Mechanism
Set Parent = mmchParent
End Property

Friend Sub SetParent(ByVal NewParent As Mechanism)
Set mmchParent = NewParent
End Sub

如果一个方法用 Friend 关键字声明,则它对部件中的其它对象是可见的,但该方法不会添加到类型库或公共接口。

运行时,从 Widgets 集合类(工程内)见到的接口与从客户端见到的接口是不同的。工程内(以及编译好的 DLL )Widget 接口包括 Widgets 集合可调用的 Friend 方法 SetParent。

因为 Friend 方法没有被添加到类型库,所以客户端只能见到 Widget 的接口中的公共属性和方法。

对属性使用 Friend 关键字
也可以使用 Friend 关键字声明属性过程。事实上,组成同一属性的不同的属性过程其范围也不同。前面的代码示例可以用一对属性过程重写:

'Widget 是 mechanism 的组成部分。
Private mmchParent As Mechanism

Public Property Get Parent() As Mechanism
Set Parent = mmchParent
End Property

Friend Property Set Parent(ByVal NewParent As _
Mechanism)
Set mmchParent = NewParent
End Sub

在部件内,Parent 是一个可读/写的属性。对于客户端,它是只读属性,这是因为部件的类型库中只出现 Property Get。

可以认为 Friend 具有介于 Public 和 Private 之间的范围。

重点 要调用 Friend 方法和属性,必须使用严格定义类型的对象变量。在上面的示例中,Widgets 集合为能够访问 SetParent 方法或 Property Set Parent,必须使用声明为 As Widget 的变量。不能从声明为 As Object 的变量中调用 Friend 方法。

隐藏返回私有对象的对象属性
“使用属性和集合创建对象模型”描述了对象模型中私有对象的使用方法。在对象模型中,这种对象与公共对象链接时,可以使用 Friend 关键字来声明属性过程的所有部分。

例如,每一个 Widget 对象都可能具有 Socket 对象,而该 Socket 对象出于某种原因不能显露给部件的用户。可以把下面的对象属性添加到该 Widget 对象。这样,不必将该属性添加到类型库或公共接口,就可以在部件内访问该 Socket:

'创建所需 Socket 对象 (As New)。
Private msoc As New Socket

Friend Property Get Socket() As Socket
Set Socket = msoc
End Property

phil2360 2003-08-22
  • 打赏
  • 举报
回复
微软的书上说有的啊,就是没说有什么用.
射天狼 2003-08-22
  • 打赏
  • 举报
回复
在类模块中修改过程的定义,使得能从类之外的模块调用这个过程,但这些模块必须是定义这个类工程的一部分。

语法

[Private | Friend | Public] [Static] [Sub | Function | Property] procedurename

必需的 procedurename 是一过程名,要使该过程在整个工程中可见,但对于这个类的控制者是不可见的。

说明

在一个类中的 Public 过程能在任何地方被调用,甚至能被这个类的实例的控制者调用。声明过程为 Private,可防止该对象的控制者调用这个过程,但也阻止了从定义该类本身的工程内部调用该过程。Friend 使过程在整个工程中可见,但对于该对象的实例的控制者来说不可见。Friend 只能出现在类模块中,且只能修改过程名,不能修改变量或类型。类中的过程能够访问工程中其它类的 Friend 过程。Friend 过程不出现在它们的类的类型库中。Friend 过程不能是后期绑定的。
hc_z 2003-08-22
  • 打赏
  • 举报
回复
gz,学习
hc_z 2003-08-22
  • 打赏
  • 举报
回复
VB中我没有见到用Friend的。

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧