在回答其中一个问题时,我的回答下面有一个讨论主题.这表明,根据访问符(或者可能是继承的类型)private/protected/public中sizeof的class对象可能会有所不同!
从他们的简短讨论中我仍然不明白,这怎么可能?
可能重复:
为什么可以通过类实例更改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但是这样的访问不是无效的吗?
我一直在浏览最近的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) Ruby似乎没有像这样定义受保护/私有块的工具:
protected do
def method
end
end
Run Code Online (Sandbox Code Playgroud)
这比较好
protected
def method
end
public
Run Code Online (Sandbox Code Playgroud)
你可能会忘记在受保护的方法后"公开".
似乎可以使用元编程实现这一点.有什么想法?
我有以下课程
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中,
可以从类,它的子类以及同一个包中存在的所有类中访问受保护的成员,
但是我希望只能从类及其子类访问成员(就像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中实现这一点.
我有两个文件:
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.
有人能解释这对我意味着什么吗?
我写了一个与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) 关于静态或动态检查访问说明符我很困惑.据说不会动态检查访问说明符.那是什么意思 ?
这个例子来自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 …
请考虑以下代码段:
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)
SedanCar是AbstractVehicle包含protected方法的子类speedFactor.speedFactor如果它被同一个类引用,我可以调用该方法.当超类用于参考时,该方法speedFactor不可访问.
隐藏方法的原因是什么?