但是我学习编程,在用Pascal语言进行结构化编程之后,我开始用Delphi学习OOP.
所以,我真的不明白strict private指令和那个指令之间的区别protected.所以这是我的代码,它是关于"包"的创建,它只是我的Delphi课程的介绍,老师告诉我们如何创建对象:
uses
SysUtils;
Type
Tbag= class (Tobject)
strict private
FcontenM : single;
Fcontent : single;
protected
function getisempty : boolean;
function getisfull: boolean;
public
constructor creer (nbliters : single);
procedure add (nbliters : single);
procedure clear (nbliters : single);
property contenM : single read FcontenM;
property content : single read Fcontent;
property isempty : boolean read getisempty;
property isfull : boolean read getisfull;
end;
function Tseau.getisempty;
begin
result := Fcontent = 0;
end;
function …Run Code Online (Sandbox Code Playgroud) 有没有理由可以更改被覆盖方法的访问修饰符?例如,
abstract class Foo{
void start(){...}
}
Run Code Online (Sandbox Code Playgroud)
然后将package-private访问修饰符更改为public,
final class Bar extends Foo{
@Override
public void start(){...}
}
Run Code Online (Sandbox Code Playgroud)
我只是出于好奇而问这个问题.
我有一个由几个有用的小工具组成的程序集.在其中我有一个包含简单公共功能的模块.
Module FishTrackerConfigurations
Public Function GetValueOfUseProductId As Boolean
Return VtlGetUseProductId 'A simple private routine in the same module
End Function
End Module
Run Code Online (Sandbox Code Playgroud)
当我从另一个项目(其中引用此程序集)调用此函数时,我得到以下错误.
Error BC30390 'FishTrackerConfigurations.Public Function GetValueOfUseProductId() As Boolean' is not accessible in this context because it is 'Public'.
Run Code Online (Sandbox Code Playgroud)
从我的项目Application.Xaml.VB文件中调用该函数,特别是在Protected Overrides Sub OnStartup(e As StartupEventArgs)例程中.
我想知道为什么会这样.
正如你可能已经知道,.NET框架的内部保护的访问修饰符工作在一种奇怪的方式:这并不意味着类是保护 和 内部,它说,类保护 或 内部 ; 也就是说,可以从同一程序集中以及从同一层次结构中访问修改后的类或成员.
所以,知道这个:你什么时候使用它?你能给我举个例子吗?.NET基类库中是否有一个很好的,有启发性的用法示例?
如protected在C#中定义方法有什么好处?
喜欢 :
protected void KeyDemo_KeyPress( object sender, KeyPressEventArgs e )
{
// some code
}
Run Code Online (Sandbox Code Playgroud)
与这样的事情相比:
private void FormName_Click( object sender, EventArgs e )
{
//some code
}
Run Code Online (Sandbox Code Playgroud)
我在很多书中看过这样的例子,我不明白为什么以及何时使用privatevs protected?
我已经编写了一些代码来使用反射来查看属性.我使用反射从类中检索了一个属性列表.
但是我需要找出该物业是公共的还是受保护的.例如:
public string Name{get;set;}
protected int Age{get;set;}
Run Code Online (Sandbox Code Playgroud)
PropertyInfo类似乎不公开有关该属性的此信息.还有另一种方法吗?
如果标题不完全不言自明,这里的代码让我困惑:
public interface IFoo<T>
{
}
public class MyClass : IFoo<MyClass.NestedInMyClass>
{
private class NestedInMyClass
{
}
}
Run Code Online (Sandbox Code Playgroud)
我很惊讶这个编译没有错误.感觉就像我暴露了一种private类型.这不应该是违法的吗?
也许你的回答只是"没有规则反对,所以为什么不行呢?" MyClass.NestedInMyClass即使在"范围"中也许同样令人惊讶.如果我删除了MyClass.资格,它将无法编译.
(如果我改为IFoo<>通用类,它应该成为基类MyClass,这是非法的,因为基类型必须至少与类型本身一样可访问.)
我尝试使用Visual Studio 2010的C#4编译器.
下面是一些使用Java 6编译但不能在Java 7中编译的代码的简单示例.
public class Test<T extends Test> {
private final int _myVar;
public Test(int myVar) {
_myVar = myVar;
}
public int get(TestContainer<T> container){
T t = container.get();
return t._myVar;
}
private static class TestContainer<T extends Test> {
private final T _test;
private TestContainer(T test) {
_test = test;
}
public T get(){
return _test;
}
}
}
Run Code Online (Sandbox Code Playgroud)
在Java 7中,它无法在get(TestContainer<T> container)方法中编译,错误如下:
错误:_myVar在Test中具有私有访问权限
我不明白为什么这不再编译 - 在我看来它应该.变量t是类型T,必须扩展Test.它试图从类中访问_myVar实例的字段.TestTest
实际上,如果我将方法更改为get(TestContainer<T> …
在使用C#时,我最近意识到我可以Foo从Foo静态函数调用对象的私有函数,甚至可以从其他Foo对象调用.在我了解了访问修饰符的所有内容之后,这对我来说听起来很奇怪.
据我所知,当你做一些属于某种内部过程的事情时,你会把一个函数设为私有.只有对象本身知道何时使用这些函数,因为其他对象不应该/不能控制对象的流.是否有任何理由为什么同一类别的其他对象应该从这个相当简单的规则中排除?
根据要求,一个例子:
public class AClass {
private void doSomething() { /* Do something here */ }
public void aFunction() {
AClass f = new AClass();
f.doSomething(); // I would have expected this line to cause an access error.
}
}
Run Code Online (Sandbox Code Playgroud) 好吧,我使用visual studio 2015 CE,更新2.我通常做的一个生产力黑客是我创建空模型类,如:
public class PersonModel
{
}
Run Code Online (Sandbox Code Playgroud)
然后在选择表达式中使用它们,如:
db.People.Where(p => someCondition)
.Select(p => new PersonModel
{
Id = p.Id,
Name = p.Name,
//set other properties
}).ToList();
Run Code Online (Sandbox Code Playgroud)
然后我去了又非现有属性Id和Name...并按Control+.要求Visual Studio来generate property Id对我.一切都很好,但它会创造:
public int Id { get; internal set; }
Run Code Online (Sandbox Code Playgroud)
如果我在asp.net webapi模型绑定中使用相同的方法,绑定将无声地失败并给我Id = 0.
所以我的问题是:有没有选择让VS创建公共setter,即:
public int Id { get; set; }
Run Code Online (Sandbox Code Playgroud) access-modifiers ×10
c# ×5
.net ×4
generics ×2
java ×2
properties ×2
class ×1
delphi ×1
java-7 ×1
module ×1
object ×1
oop ×1
overriding ×1
reflection ×1
vb.net ×1
wpf ×1