vb6.0和vb.net_VB.NET中的朋友和受保护的朋友

vb6.0和vb.net

Access modifiers (also called scoping rules) determine what code can access an element—that is, what code has permission to read it or write to it. In previous versions of Visual Basic, there were three types of classes. These have been carried forward to .NET. In each of these, .NET allows access only to code:

访问修饰符(也称为作用域规则)确定哪些代码可以访问元素,即哪些代码有权读取或写入该元素。 在Visual Basic的早期版本中,存在三种类型的类。 这些已结转至.NET。 在每种情况下,.NET都仅允许访问代码:

  • Private - within the same module, class, or structure.

    专用-在同一模块,类或结构内。
  • Friend - within the same assembly.

    朋友-在同一程序集中。
  • Public - anywhere in the same project, from other projects that reference the project, and from any assembly built from the project. In other words, any code that can find it.

    公用-同一项目中的任何地方,引用该项目的其他项目以及从该项目构建的任何程序集。 换句话说,任何可以找到它的代码。

VB.NET has also added one and a half new ones.

VB.NET还添加了一个半新的VB.NET

  • Protected

    受保护的
  • Protected Friend

    受保护的朋友

The "half" is because Protected Friend is a combination of the new Protected class and the old Friend class.

之所以“半”,是因为“保护的朋友”是新的“保护的”类和旧的“朋友”类的组合。

The Protected and Protected Friend modifiers are necessary because VB.NET implements the last OOP requirement that VB was missing: Inheritance.

因为VB.NET实现了VB缺少的最后一个OOP要求,所以必须使用Protected和Protected Friend修饰符: Inheritance

Previous to VB.NET, supercilious and disdainful C++ and Java programmers would belittle VB because it was, according to them, "not fully object oriented." Why? Previous versions lacked inheritance. Inheritance allows objects to share their interfaces and/or implementation in a hierarchy. In other words, inheritance makes it possible for one software object that takes on all of the methods and properties of another one.

在VB.NET之前,无聊而卑鄙的C ++和Java程序员会轻视VB,因为根据他们的说法,它不是“完全面向对象的”。 为什么? 以前的版本缺少继承。 继承允许对象在层次结构中共享其接口和/或实现。 换句话说,继承使一个软件对象可以承担另一软件对象的所有方法和属性。

This is often called the "is-a" relationship.

这通常称为“是”关系。

  • A truck "is-a" vehicle.

    卡车“是”车辆。
  • A square "is-a" shape.

    正方形“是”形状。
  • A dog "is-a" mammal.

    狗是哺乳动物。

The idea is that more general and widely used methods and properties are defined "parent" classes and these are made more specific in "child" classes (often called subclasses). "Mammal" is a more general description than "dog." Whales are mammals.

这个想法是,在“父”类中定义了更通用和更广泛使用的方法和属性,而在“子”类(通常称为子类)中使它们更加具体。 “哺乳动物”比“狗”更具笼统的描述。 鲸鱼是哺乳动物。

The big benefit is that you can organize your code so you only have to write code that does something that lots of objects have to do once in the parent. All "employees" have to have an "employee number" assigned to them. More specific code can be part of the child classes. Only employees that work in the general office need to have an employee door card key assigned to them.

最大的好处是您可以组织代码,因此您只需要编写执行许多对象在父级中必须要做的事情的代码即可。 所有“雇员”都必须分配一个“雇员号”。 更具体的代码可以成为子类的一部分。 只有在总公司工作的员工才需要为其分配员工门卡钥匙。

This new capability of inheritance requires new rules, however. If a new class is based on an old one, Protected is an access modifier that reflects that relationship. Protected code can be accessed only from within the same class, or from a class derived from this class. You don't want employee door card keys being assigned to anyone except employees.

但是,这种新的继承功能需要新的规则。 如果新类基于旧类,则Protected是一种访问修饰符,它反映了这种关系。 受保护的代码只能从相同的类中访问,也不能从从该类派生的类中访问。 您不希望将员工门卡钥匙分配给除员工以外的任何人。

As noted, Protected Friend is a combination of the access of both Friend and Protected. Code elements can be accessed either from derived classes or from within the same assembly, or both. Protected Friend can be used to create libraries of classes since code that accesses your code only has to be in the same assembly.

如前所述,受保护的朋友是朋友和受保护的访问权限的组合。 可以从派生类或在同一程序集中或从两者访问代码元素。 受保护的朋友可以用来创建类的库,因为访问您的代码的代码仅必须在同一程序集中。

But Friend also has that access, so why would you use Protected Friend? The reason is that Friend can be used in a Source file, Namespace, Interface, Module, Class, or Structure. But Protected Friend can only be used in a Class. Protected Friend is what you need for building your own object libraries. Friend is just for difficult code situations where assembly wide access is really required.

但是Friend也具有该访问权限,那么为什么要使用Protected Friend? 原因是可以在源文件, 命名空间 ,接口, 模块,类或结构中使用Friend。 但是受保护的朋友只能在课程中使用。 保护自己的朋友是构建自己的对象库所需要的。 Friend仅适用于真正需要汇编范围访问的困难代码情况。

翻译自: https://www.thoughtco.com/friend-and-protected-friend-in-vbnet-3424246

vb6.0和vb.net

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值