我学DELPHI有一段时间了,但对于访问属性protected还是有些不明白,能接合例子讲一下吗?(100分)

  • 主题发起人 主题发起人 吴明星
  • 开始时间 开始时间

吴明星

Unregistered / Unconfirmed
GUEST, unregistred user!
父类 protected 里申明的变量或函数对他的子类是开放的,其他的类是不能访问的。
 
Delphi的对象有private、protected和public三个级别的访问控制。
而且在同一个unit里的对象可以互相访问对方的protected级别属性!
在构件类型中,非通用的属性、事件和方法都声明为protected。
这样可以禁止构件用户访问,又能被子类型继承和修改。
定义时,一般总是把对用户开放的属性和事件声明为published,把方法声明为public或protected。
属性在基类中是在Protected部分声明,子对象可以继承在父类的protected部分声明的属性和事件。

例如:
type
TSimpleExample = class(TComponent)
private
FFocusControl : TWinControl;
protected
procedure SetFocusControl(const value : TWinControl)

virtual;
public
protected
property FocusControl : TWinControl
read FFocusControl
write SetFocusControl

end


procedure TSimpleExample.SetFocusControl(const Value : TWinControl);
begin
FFocusControl := Value;
end;
 
看看Borland自己怎么说的吧!

What do the PRIVATE, PROTECTED, PUBLIC and PUBLISHED sections of a unit mean ?
They define the level of visibility of properties and methods of an object. In a nutshell, they are as follows:
PRIVATE Things contained in this section are private to the object. External objects do not have access to any
elements contained here.
PROTECTED Similar to private with one exception: Descendants of the object have access to the methods and
properties contained here.
PUBLIC All external objects have access to elements here.
PUBLISHED Similar to PUBLIC, but properties listed here show up at design time under the Object Inspector.
These sections really come into play when you're building components. Typically, you'll put the local field
variables assigned to properties in the PRIVATE section, along with functions and procedures that act only
within the scope of the object itself. In the PROTECTED methods, if you know the object will be inherited
at some point, you'll put properties, functions and procedures to give access to your descendants, but
maintain privacy from external objects. In the PUBLIC section, you'll place methods and procedures that
usually don't have a corresponding property, but require full access by other entities
or, insert properties
that you don't want visible in the Object Inspector. The PUBLISHED section by convention is for properties
only, though I've put procedures there just to see if I still had access to them.
TForms have PRIVATE and PUBLIC sections as well. In most cases you won't put them to use
however, there will
be times when you want to add functionality and capabilities to the form that need to be assigned levels of
visibility.
Play around with this stuff. The more you understand it, the more you'll understand object-oriented programming.
By the way, I suggest getting a book on general object-oriented programming principles. Understanding the theory
behind objects will help you get a better handle on Delphi programming.
 
禁止构件用户访问,又能被子类型继承和修改
 
请举个例子好吧。

如:
 TF=class //父类
protected
I:integer;
end;
 
TZ=class(TF)  //子类
end;
 
TZZ=class(TZZ)
procedure aa
end;

procedure TZZ.aa
var
Z:TZ;
 begin
Z:=TZ.Create;
I:=10
//这样能访问吗?为什么?
Z.I:=10 //这样能访问吗?为什么?C#中的保护元素可以呀。
  Z.Free;
end;
 
我是在子类中访问父类中的保护元素呀。
 
相当于C++中的友元
 
禁止构件用户访问,又能被子类型继承和修改

这句话的意思是说,在处部建立这个对象,就算是从一个类继承下来的,也不能访问保护
元素。只能从类声明中来修改和访问。是不是呀?
 
我想说的是如果那三个类都不在一个UNIT里声明。也就是说它们不是友元。
 
如果你的一些屬性或是方法只有類和其子類可以訪問的話使用Protected,
一般是用在一些通用的接口上的,所以在定義接口是要分清.
 
都是写什么高手啊??????
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部