标签: access-modifiers

在AS3中,成员变量/方法/ getter和setter是否可以包含在一个公共访问修饰符中?

有没有办法用一个公共访问修饰符声明一堆成员?我相信这可以用C++和其他一些语言来完成,但如果它存在于AS3中则很奇怪.

代替:

class FooBar {
   public var theDog:String = "Bark!";
   public var theCat:String = "Miao!";

   private var myBird:String = "Chirp!";
   private var myPig:String =  "Oink!";
}
Run Code Online (Sandbox Code Playgroud)

它可以写成:

class FooBar {
   public {
     var theDog:String = "Bark!";
     var theCat:String = "Miao!";
   }
   private {
     var myBird:String = "Chirp!";
     var myPig:String =  "Oink!";
   }
}
Run Code Online (Sandbox Code Playgroud)

syntax access-modifiers actionscript-3 curly-braces

4
推荐指数
1
解决办法
161
查看次数

C# - 内部属性在quickwatch中"可读"但不使用反射?

我看到"快速监视"窗口可以访问所有属性,而不管库中类的访问限制(内部,受保护,私有),即使在完全不同的应用程序,库和命名空间中引用库也是如此.虽然我没有找到使用"反射"访问这些的方法.我特别想"阅读"(注意 - 只是阅读)程序集的内部属性.如果通过设计"内部"如何工作(在同一命名空间外无法访问),这是不可能的,那么VS.NET中的"快速监视"窗口是如何"读取"的呢?

这是我使用的示例代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestLib
{
    public class TestInteralProp
    {
        internal string PropertyInternal
        {
            get { return "Internal Property!";  }
        }

        protected string PropertyProtected
        {
            get { return "Protected Property!"; }
        }

        string PropertyPrivate
        {
            get { return "Private Property!"; }
        }

        public string PropertyPublic
        {
            get { return "Public Property!";  }
        }

        protected internal string PropertyProtectedInternal
        {
            get { return "Protected Internal Property!"; }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

当我为TestInernalProp类创建一个对象时,我可以在quickwatch中看到所有4个属性 -

道具

当我使用反射访问除公共属性(PropertyPublic)之外的任何这些,我得到一个空引用异常. …

c# properties protected access-modifiers internal

4
推荐指数
1
解决办法
2069
查看次数

允许特定类的实例只由Java中的另一个类创建?

假设我有两个A和B类,我想做它,这样B的实例只能在A和B本身中创建.我不希望允许任何其他类(包括A的子类)创建B的实例.有没有办法在Java中执行此操作?

如果我不清楚我想要做什么,这里有一些代码:

public class A {
    B instance;
    public A(){
        // Still allows for subclasses to access B
        instance = B.getInstance((Object)this);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我想要限制的类:

public class B {

    // If I make this public all classes can create it, but
    // if I make it private without any getter methods then
    // no other classes but itself can create it
    private B(){}

    // Problem with this is that subclasses of  A
    // can also create instances of B
    public static …
Run Code Online (Sandbox Code Playgroud)

java singleton access-modifiers

4
推荐指数
1
解决办法
1050
查看次数

在没有编译错误的情况下更改对方法的访问

有人可以演示一个简单程序的示例,其中在工作程序中将一个方法的访问权限从私有更改为公共将不会导致编译错误,但只会导致程序行为不同吗?

此外,何时添加新的私有方法会导致编译错误或导致程序行为不同?

java private public access-modifiers

4
推荐指数
1
解决办法
60
查看次数

从嵌套类的函数访问父类的非静态成员

我试图在论坛中窥探一个类似的问题,这可能会帮助我取得成功.

我的C++程序中有一个嵌套类.我试图从嵌套类中的函数访问父类的变量,但我遇到以下错误

ERROR: A non static member reference must be relative to a specific object
Run Code Online (Sandbox Code Playgroud)

我试图访问的变量受到保护,嵌套类是公共的(它的功能也是如此)

以下是描述(或努力)该场景的代码片段

头文件

class A
{
    protected:
        D x;

    public:

        int func1(int a);

        class B : protected C
        {
            int func2(int a);   
        }
}   
Run Code Online (Sandbox Code Playgroud)

CPP文件

int A::func1(int a)
{
    x = 5;
    B z;
    int b = z.func2(a);
}

int A::B::func2(int a)
{
    int c = x.getValue(a);      /* ERROR: A non static member reference 
                                   must be relative to a specific object */
}
Run Code Online (Sandbox Code Playgroud)

从某个地方

A …
Run Code Online (Sandbox Code Playgroud)

c++ access-modifiers nested-class

4
推荐指数
1
解决办法
2386
查看次数

访问修饰符的Scala参数?

有什么区别

class Test {
  private[this] val foo = 0
}
Run Code Online (Sandbox Code Playgroud)

VS

class Test {
  private val foo = 0
}
Run Code Online (Sandbox Code Playgroud)

什么都可以进去[]?另外,当我想查看其中的规格时,我应该搜索什么?我尝试使用Google搜索"scala访问修饰符参数/参数化scala访问修饰符"的各种组合,并没有出现任何问题.

scala access-modifiers

4
推荐指数
1
解决办法
789
查看次数

我可以限制 C# 扩展方法对同一程序集中的类的可见性吗?

假设我有这些文件:

MyCode.cs

namespace MyCodeNamespace
{
  public class MyClass
  {
    //OMITTED
  }

  internal static class MyExtensions
  {
    internal static void Foo(this string str)
    {
      //OMITTED
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

其他代码.cs

using MyCodeNamespace;
namespace OtherCodeNamespace
{
  //OMITTED
}
Run Code Online (Sandbox Code Playgroud)

这两个文件是同一程序集的一部分。有什么方法可以使 Foo 可以访问 MyCode.cs 但不能访问 OtherCode.cs?我的问题与这个问题类似: C# ExtensionMethods onlyvisible andaccessible inside one class ("private") 但它接受的答案并不是我真正想要的。我想创建一个仅对我正在处理的代码可见的扩展方法,并且根据上述问题的答案,有人仍然可以通过添加“using”语句来访问它。有没有一种方法可以创建一个仅对我的代码可见的扩展方法,而在其他地方都看不到,甚至对同一程序集中的另一个类也不可见?

我问这个问题是因为调用扩展方法的语法很方便,并且对我正在处理的事情很有用(否则我只是创建一个私有方法),但我不希望其他人看到它并使用它他们的代码,以防它没有按照他们假设的那样进行。感谢 Visual Studio 的智能感知,我的扩展方法当前显示在可用方法列表中(以及添加它们所在命名空间的选项)。

.net c# extension-methods access-modifiers class-visibility

4
推荐指数
1
解决办法
3151
查看次数

JavaFx 和 @FXML 中的访问修饰符

我是 JavaFx 的新手,在我看过的一些教程中,存在一些差异。在其中一篇教程中,他总是private在控制器类中用作访问修饰符,并这样写:

@FXML private Button button;

但其他教程始终使用 public 并且没有在控制器类中包含 @FXML。这两个似乎都工作得很好,但是它们之间有我应该知道的实际区别吗?

java javafx access-modifiers fxml

4
推荐指数
1
解决办法
737
查看次数

Java - 为什么另一个包中的子级无法通过父级引用访问父级的受保护方法?

我在两个不同的包中有两个类:

package package1;

public class ParentClass {
    public void testPublic() {
    }

    protected void testProtected() {
    }
}


package package2;

import package1.ParentClass;

public class ChildClass extends ParentClass {
   void test() {
        ParentClass par = new ParentClass();
        par.testProtected(); // Line 1 : ERROR: testProtected() has protected access in ParentClass
        testProtected();     // Line 2 : No error

        ChildClass ch = new ChildClass();
        ch.testProtected(); // Line 3 : No ERROR
        testProtected();    // Line 4 : No error
    }    
}
Run Code Online (Sandbox Code Playgroud)

我能够理解为什么调用时没有错误testProtected() -- …

java inheritance protected access-modifiers package

4
推荐指数
1
解决办法
96
查看次数

Python 访问修饰符作为装饰器

是否可以在Python中使用装饰器来实现protectedprivate访问类的修饰符?如何?

功能应该类似于下面的代码:

class A:
    
    def public_func(self):
        self.protected_func()    # Runs without warning and error    (Because is called in the owner class)
        self.private_func()      # Runs without warning and error    (Because is called in the owner class)
    
    @protected
    def protected_func(self):
        print('protected is running')
        self.private_func()      # Runs without warning and error    (Because is called in the owner class)
    
    @private
    def private_func(self):
        print(f'private is running')


a = A()
a.public_func()      # Runs without any warning and error            (Because has no access modifier) …
Run Code Online (Sandbox Code Playgroud)

python oop access-modifiers python-decorators

4
推荐指数
1
解决办法
208
查看次数