我不确定这是否可行,但我希望能够让父类可以看到基类的字段/方法.
比方说,我有一个班级:
public class ExampleFile
{
private Stream _stream;
private long _baseoffset;
public ExampleFile(Stream input)
{
_stream = input;
_baseoffset = input.Position;
}
public void SeekTo(long offset)
{
_stream.Seek(offset + _baseoffset, SeekOrigin.Begin);
}
}
Run Code Online (Sandbox Code Playgroud)
然后我将该类用作另一个类的基础:
public class ExampleClass : ExampleFile
{
public ExampleClass(Stream input)
: base(input)
{
}
public byte[] GetSomething()
{
byte[] id = new byte[5];
SeekTo(2);
base._stream.Read(id, 0, 5);
return id;
}
}
Run Code Online (Sandbox Code Playgroud)
有什么方法可以让字段/方法ExampleFile只显示ExampleClass?
asa*_*yer 10
在protected要向子类公开的字段/属性/方法上使用修饰符而不是public/private.
public class ExampleFile
{
protected Stream _stream; // no longer private, so the inherited
protected long _baseoffset; //classes can access them
public ExampleFile(Stream input)
{
_stream = input;
_baseoffset = input.Position;
}
public void SeekTo(long offset)
{
_stream.Seek(offset + _baseoffset, SeekOrigin.Begin);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3233 次 |
| 最近记录: |