标签: access-specifier

对象的大小是否受访问说明符类型和继承类型的影响?

在回答其中一个问题时,我的回答下面有一个讨论主题.这表明,根据访问符(或者可能是继承的类型)private/protected/publicsizeofclass对象可能会有所不同!

从他们的简短讨论中我仍然不明白,这怎么可能?

c++ inheritance access-specifier

10
推荐指数
1
解决办法
342
查看次数

为什么==重载可以访问参数的私有成员

可能重复:
为什么可以通过类实例更改obj的私有值?

考虑以下(部分)代码:

class Group {
    private:
      int id;

    public:
      void set_id(int);
      int get_id();
      bool operator==(const Group&);
};


bool Group::operator==(const Group& g) {
    if(g.id == this->id) {  /* id is private? */
            return true;
    }

    return false;
}
Run Code Online (Sandbox Code Playgroud)

代码编译和结果似乎正确.但是,在if运算符重载实现的部分中,我们直接访问其参数的私有成员 - const Group& g但是这样的访问不是无效的吗?

c++ operator-overloading access-specifier

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

使用swift 4访问扩展中的fileprivate和私有变量以及另一个类

我一直在浏览最近的swift文档,并在swift4中了解私有和fileprivate关键字的几个例子.我试图在同一个类的扩展中访问fileprivate和private变量,并且另一个类继承类,但输出是无效的.我正在以下列方式使用

  class privateUsageExample: UIViewController {
    private var priVar = false
    fileprivate var fPriVar = false
  }

  // usage of extension in the same class

  extension privateUsageExample: UITextFieldDelegate {

     if priVar{ // do something} // error : expected declaration
     if fPriVar{ // do something} // error : expected declaration

     func randFunc(){ 
        self. fPriVar = true // accessible don't know the reason 
      }
  }

  // access of private and fileprivate variables in another class different file

  class anotherUsageInDiffSwiftFile: privateUsageExample {

   priVar = …
Run Code Online (Sandbox Code Playgroud)

access-control access-specifier ios swift swift4

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

Ruby中的私有/受保护块?

Ruby似乎没有像这样定义受保护/私有块的工具:

protected do
  def method
  end
end
Run Code Online (Sandbox Code Playgroud)

这比较好

protected 

def method 
end 

public
Run Code Online (Sandbox Code Playgroud)

你可能会忘记在受保护的方法后"公开".

似乎可以使用元编程实现这一点.有什么想法?

ruby access-specifier

8
推荐指数
2
解决办法
2219
查看次数

如何从其他包中的类访问包私有类?

我有以下课程

Hello.java

package speak.hello;

import java.util.Map;

import speak.hi.CustomMap;
import speak.hi.Hi;

public class Hello {

    private Hi hi;

    Hello(Hi hi) {
        this.hi = hi;
    }

    public String sayHello() {
        return "Hello";
    }

    public String sayHi() {
        return hi.sayHi();
    }

    public Map<String, Object> getMap() {
        return hi.getMap();
    }

    public void clearMap() {
        hi.getMap().clear();
    }

    public void discardMap() {
        CustomMap map = (CustomMap) hi.getMap();
        map.discard();
    }

    public static void main(String[] args) {
        Hello hello = new Hello(new Hi());
        System.out.println(hello.sayHello());
        System.out.println(hello.sayHi());
        System.out.println(hello.getMap());
        hello.clearMap();
        System.out.println("--"); …
Run Code Online (Sandbox Code Playgroud)

java access-specifier

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

c ++喜欢java中的protected

可能重复:
如何使数据成员只能访问类和子类

在java中,
可以从类,它的子类以及同一个包中存在的所有类中访问受保护的成员,
但是我希望只能从类及其子类访问成员(就像c ++中的受保护成员一样).

例如:::

class A
{
    protected void fun()
    {
        System.out.println("Hello");
    }
}
class B
{
    public static void main(String args[])
    {
        A a = new A();
        a.fun();        
    }
}
Run Code Online (Sandbox Code Playgroud)

在这里,A的fun()可以被B访问,即使B不是A的子类.

如何使A不能进入所有不属于A的子类的类?

编辑::我想在java中实现这一点.

c++ java protected access-specifier

8
推荐指数
1
解决办法
3198
查看次数

使用接口"无法降低继承方法的可见性"的含义

我有两个文件:

public interface PrintService {
    void print(PrintDetails details);
    class PrintDetails {
        private String printTemplate;
    }
    public interface Task {
        String ACTION = "print";
    }
}
Run Code Online (Sandbox Code Playgroud)

public class A implements PrintService {
    void print(PrintDetails details) {
        System.out.println("printing: " + details);
    }
    String action = PrintService.Task.ACTION;   
}
Run Code Online (Sandbox Code Playgroud)

我认为代码看起来没问题,但是我在第二个文件中收到错误信息void print(PrintDetails details) {:

无法降低继承方法的可见性PrintService.

有人能解释这对我意味着什么吗?

java interface access-specifier

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

SFINAE和Clang vs. GCC与MSVC的能见度检查 - 这是正确的吗?

我写了一个与C++ 03兼容的实现的尝试is_default_constructible:

template<class = void> struct is_default_constructible;
template<> struct is_default_constructible<>
{
protected:
    // Put base typedefs here to avoid pollution
    struct twoc { char a, b; };
    template<bool> struct test { typedef char type; };
    template<class T> static T declval();
};
template<> struct is_default_constructible<>::test<true> { typedef twoc type; };
template<class T> struct is_default_constructible : is_default_constructible<>
{
private:
    template<class U> static typename test<!!sizeof(::new U())>::type sfinae(U*);
    template<class U> static char sfinae(...);
public:
    static bool const value = sizeof(sfinae<T>(0)) > 1; …
Run Code Online (Sandbox Code Playgroud)

c++ access-specifier sfinae template-meta-programming c++03

7
推荐指数
1
解决办法
297
查看次数

需要理解语句"静态检查可访问性而不是在C++中动态检查"

关于静态或动态检查访问说明符我很困惑.据说不会动态检查访问说明符.那是什么意思 ?

这个例子来自SO的不同帖子.考虑这个例子

例A:

class Base 
{
public:
    virtual void Message() = 0;
};

class Intermediate : public Base 
{
   //Is Message method virtual here too ? is it private or public ?
};

class Final : public Intermediate {
    void Message() {
        cout << "Hello World!" << endl;
    }
};

 Final final;
Run Code Online (Sandbox Code Playgroud)

现在假设我做了这样的事情

Final* finalPtr = &final;
finalPtr->Message();
Run Code Online (Sandbox Code Playgroud)

以上不会工作,我的理解是在Final类中,Message Method是私有的.那是对的吗 ?如果是这样,为什么这样做?方法

  Intermediate* finalPtr = &final; // or Base* finalPtr = &final;
  finalPtr->Message();
Run Code Online (Sandbox Code Playgroud)

上述代码的工作原因是因为基类指针是使用派生类实例化的.如果是这样,为什么调用Message()会起作用.关于SO的帖子说,由于继承是公开的,因此它将作为公共功能继承?另一方面,类中的函数本身具有私有访问说明符(因为默认情况下它在类中是私有的).我在这里很困惑,如果有人能澄清这一点,我将不胜感激.如果基类使用派生类进行实例化,那么这是正确的吗?那么基类方法的访问说明符优先于派生类的访问说明符?

更新:

我也注意到,如果我改变Intermediate …

c++ inheritance access-specifier

7
推荐指数
1
解决办法
205
查看次数

为什么无法从子类访问受保护的方法?

请考虑以下代码段:

package vehicle;

public abstract class AbstractVehicle {
    protected int speedFactor() {
        return 5;
    }
}

package car;

import vehicle.AbstractVehicle;

public class SedanCar extends AbstractVehicle {
    public static void main(String[] args) {
        SedanCar sedan = new SedanCar();
        sedan
                .speedFactor();
        AbstractVehicle vehicle = new SedanCar();
        // vehicle //WON'T compile
        // .speedFactor();
    }
}
Run Code Online (Sandbox Code Playgroud)

SedanCarAbstractVehicle包含protected方法的子类speedFactor.speedFactor如果它被同一个类引用,我可以调用该方法.当超类用于参考时,该方法speedFactor不可访问.

隐藏方法的原因是什么?

java oop inheritance encapsulation access-specifier

7
推荐指数
1
解决办法
4707
查看次数