标签: access-modifiers

在C++中是否存在与访问修饰符区域等效的C#

可以在C++中使用以下结构声明变量

private:
public:
protected:
    float bla1;
    float bla2;
    float bla3;
Run Code Online (Sandbox Code Playgroud)

C#中有等价物吗?不得不重复自己似乎相当乏味;

protected float bla1;
protected float bla2;
protected float bla3;
Run Code Online (Sandbox Code Playgroud)

c# access-modifiers

3
推荐指数
3
解决办法
328
查看次数

可以从Java中的main访问类中的私有变量吗?

我最近开始使用JDK1.6学习Java.如果这是一个愚蠢的问题,请原谅.

如果私有变量可以由main()中的对象直接访问,它们如何"私有"?

public class Account1
{
private int accountNum;
private String name;

Account1() {
    accountNum = 1101;
    name = "Scott";
}

public void showData() {
    System.out.println("Account Number: " + accountNum +
        "\nName: " + name);
}

public static void main(String[] args) {
    Account1 myA1 = new Account1();
    myA1.showData();
    System.out.println(myA1.accountNum); //Works! What about "Private"?!
}
}
Run Code Online (Sandbox Code Playgroud)

这给出了输出:

Account Number: 1101  
Name: Scott  
1101
Run Code Online (Sandbox Code Playgroud)

java access-modifiers

3
推荐指数
1
解决办法
2973
查看次数

C#显式声明成员接口

如何声明显式接口的成员?.ie:

    public interface IPerfil
    {
        int IDPerfil
        {
            get;
            set;
        }
        int IDMarca
        {
            get;
            set;
        }
        int IDRegional
        {
            get;
            set;
        }
        int IDFilial
        {
            get;
            set;
        }
}
Run Code Online (Sandbox Code Playgroud)

然后

    public class ComentariosPerfil : BaseComentarios, IPerfil
    {
        public int IPerfil.IDFilial
        {
            get;
            set;
        }
[...]
Run Code Online (Sandbox Code Playgroud)

我收到编译错误,说"public"修饰符不能应用于此项目.

问题是:

我希望这个属性是公开的.我不能在界面中编写修饰符,如:

   public int IDPerfil
        {
            get;
            set;
        }
Run Code Online (Sandbox Code Playgroud)

那么,我如何显式实现接口成员,并使其成为公共?

c# interface access-modifiers

3
推荐指数
1
解决办法
263
查看次数

我可以在Scala中定义"方法 - 私有"字段吗?

鉴于这种情况:

object ResourceManager {

  private var inited = false

  def init(config: Config) {
    if (inited)
      throw new IllegalStateException
    // do initialization
    inited = true
  }

}
Run Code Online (Sandbox Code Playgroud)

有什么方法可以让我以inited某种方式"私有init()",这样我可以确定这个类中没有其他方法能够设置inited = false

scala initialization private access-modifiers

3
推荐指数
2
解决办法
943
查看次数

想要使用的朋友访问修饰符在哪里?

我见过Friend修饰符的唯一地方是在WinForms设计器中,为什么修改器在Winforms中设置为Friend?VB.NET:'朋友'修饰符有什么作用?.

朋友修饰符似乎是一个几乎任意宽的访问级别,是为解决VB中的一些历史性架构问题而创建的,我只是想知道是否有人有意义的继续使用它?

我有一些愿望只将方法暴露给给定的命名空间,以便将相关的对象集合的功能集合在一起并管理它们的任何非线程安全方法,同时将安全的公共方法暴露给更广泛的范围.同一个集会.朋友没有此访问级别.可能是一个必然的问题,那就是我对程序集和命名空间的使用与预期的不一致?

在许多情况下,由于程序集具有严格的层次结构,因此无法将功能分离到不同的程序集中,从而导致具有相互但不同的对象组,这些对象可以访问彼此不安全的方法.

编辑: 虽然我知道修饰符的功能,但我很好奇人们对它的实际目的,因为我没有遇到过这样一个合适的解决方案.

vb.net access-modifiers friend

3
推荐指数
1
解决办法
4442
查看次数

编译时为什么会出现重复修饰符错误?

import java.util.*;
import java.io.*;
public final class FileListing2 
{
    public static void main(String... aArgs) throws FileNotFoundException 
    {
         File startingDirectory= new File(aArgs[0]);
         List<File> files = FileListing.getFileListing(startingDirectory);
         //print out all file names, in the the order of File.compareTo()
         for(File file : files )
         {
              System.out.println(file);
         }
    }
    static public List<File> getFileListing(File aStartingDir) throws FileNotFoundException 
    {
        validateDirectory(aStartingDir);
        List<File> result = getFileListingNoSort(aStartingDir);
        Collections.sort(result);
        return result;
    }
    static private List<File> getFileListingNoSort(File aStartingDir) throws FileNotFoundException 
    {
          List<File> result = new ArrayList<File>();
          File[] filesAndDirs = aStartingDir.listFiles();
          List<File> …
Run Code Online (Sandbox Code Playgroud)

java access-modifiers

3
推荐指数
1
解决办法
2万
查看次数

将'final'修饰符与getter和setter一起使用是一个好主意吗?

我想知道为什么最终修饰符不与getter和setter一起使用?

为什么这样:

private int x;

public void setX(int x) 
{ 
  if(x >= 1) throw new IllegalArgumentException("X must be lower than 1");
  this.x = x; 
}
Run Code Online (Sandbox Code Playgroud)

而不是这个:

private int x;

public final void setX(int x) 
{ 
  if(x >= 1) throw new IllegalArgumentException("X must be lower than 1");
  this.x = x; 
}
Run Code Online (Sandbox Code Playgroud)

它没有改善封装?我一直试图用谷歌澄清它,但我没有运气.

谢谢你提前.

java oop encapsulation access-modifiers

3
推荐指数
1
解决办法
3105
查看次数

C#中的访问修饰符

我正在教自己C#,我遇到了一些模棱两可的情况.

我正在尝试做的是为一些数据创建一个容器类,相当直接,但我试图尊重封装,并且只能通过setter和getter访问数据.所以我正在阅读有关访问修饰符的内容,根据 此MSDN文章,默认访问级别为内部.我来自Java-land所以我不熟悉内部,但是从该页面上的资源来看,内部看起来比我想要的更宽松.所以我想把东西设为私人.

我的困惑来自这里的代码示例.看起来如果我这样做

class whatever {
    private int thing;
    string ambiguous; 
}
Run Code Online (Sandbox Code Playgroud)

模棱两可的变量将是私有的,而不是内部的.

它真的像那样工作吗?或者第二个例子写错了?

c# private access-modifiers

3
推荐指数
1
解决办法
117
查看次数

java8接口允许公共默认方法

在java 8中,默认方法实现可以同时使用publicdefault修饰符.以下两种方法的主要区别是什么?在哪种条件下需要遵循哪种类型.

default int makeMul(int x, int y) {
    return x * y;
}

public default int makeMul(int x, int y) {
    return x * y;
}
Run Code Online (Sandbox Code Playgroud)

java access-modifiers java-8 default-method

3
推荐指数
1
解决办法
394
查看次数

为什么头文件中有多个“公共”关键字?

我是一名新编码员,因此请原谅我提出一个非常简单的问题。我正在查看要实现的方法的一些示例代码,并且看到下面显示的标头(第一段代码)。

class CIndividual  
{

public:
    CIndividual();
    virtual ~CIndividual();

public:
         vector<int> profit;
       vector<int> weight;    

public:
    CRandomNumber Rnd;
    bool dominated;
};
Run Code Online (Sandbox Code Playgroud)

为什么公众多次使用?我对这种编码结构感到非常困惑,希望这个问题不太基本。

c++ access-modifiers header-files

3
推荐指数
1
解决办法
105
查看次数