private-members

Private Member Access Java

匆匆过客 提交于 2019-11-26 20:29:28
问题 Is the private member access at the class level or at the object level. If it is at the object level, then the following code should not compile class PrivateMember { private int i; public PrivateMember() { i = 2; } public void printI() { System.out.println("i is: "+i); } public void messWithI(PrivateMember t) { t.i *= 2; } public static void main (String args[]) { PrivateMember sub = new PrivateMember(); PrivateMember obj = new PrivateMember(); obj.printI(); sub.messWithI(obj); obj.printI();

Private Variables and Methods in Python [duplicate]

强颜欢笑 提交于 2019-11-26 19:39:41
问题 Possible Duplicate: The meaning of a single- and a double-underscore before an object name in Python Which should I use _foo (an underscore) or __bar (double underscore) for private members and methods in Python? 回答1: Please note that there is no such thing as "private method" in Python. Double underscore is just name mangling: >>> class A(object): ... def __foo(self): ... pass ... >>> a = A() >>> A.__dict__.keys() ['__dict__', '_A__foo', '__module__', '__weakref__', '__doc__'] >>> a._A__foo(

Why make private inner class member public in Java?

这一生的挚爱 提交于 2019-11-26 19:28:51
问题 What is the reason of declaring a member of a private inner class public in Java if it still can't be accessed outside of containing class? Or can it? public class DataStructure { // ... private class InnerEvenIterator { // ... public boolean hasNext() { // Why public? // ... } } } 回答1: If the InnerEvenIterator class does not extend any class or implement any interface, I think it is nonsense because no other class can access any instance of it. However, if it extends or implements any other

How to make instance variables private in Ruby?

拥有回忆 提交于 2019-11-26 19:18:53
问题 Is there any way to make instance variables "private"(C++ or Java definition) in ruby? In other words I want following code to result in an error. class Base def initialize() @x = 10 end end class Derived < Base def x @x = 20 end end d = Derived.new 回答1: Like most things in Ruby, instance variables aren't truly "private" and can be accessed by anyone with d.instance_variable_get :@x . Unlike in Java/C++, though, instance variables in Ruby are always private. They are never part of the public

Why are private fields private to the type, not the instance?

泄露秘密 提交于 2019-11-26 15:45:35
In C# (and many other languages) it's perfectly legitimate to access private fields of other instances of the same type. For example: public class Foo { private bool aBool; public void DoBar(Foo anotherFoo) { if (anotherFoo.aBool) ... } } As the C# specification (sections 3.5.1, 3.5.2) states access to private fields is on a type, not an instance. I've been discussing this with a colleague and we're trying to come up with a reason why it works like this (rather than restricting access to the same instance). The best argument we could come up with is for equality checks where the class may want

Which is better between a readonly modifier and a private setter?

送分小仙女□ 提交于 2019-11-26 15:25:49
问题 I've been working on creating a class and suddenly a thought came to my mind of what is the difference between the two codes: public readonly string ProductLocation; AND public string ProductLocation { get; private set; } Can you guys give me idea when to use the following better. thanks. 回答1: The first one is a read-only field, while the second one gets compiled as a pair of methods (and all reads of the property ProductLocation gets compiled into calls to the corresponding get method and

Hiding members in a C struct

孤街浪徒 提交于 2019-11-26 12:54:48
问题 I\'ve been reading about OOP in C but I never liked how you can\'t have private data members like you can in C++. But then it came to my mind that you could create 2 structures. One is defined in the header file and the other is defined in the source file. // ========================================= // in somestruct.h typedef struct { int _public_member; } SomeStruct; // ========================================= // in somestruct.c #include \"somestruct.h\" typedef struct { int _public_member

Why and how does C# allow accessing private variables outside the class itself when it&#39;s within the same containing class?

蹲街弑〆低调 提交于 2019-11-26 11:25:08
问题 I don\'t know if the question is descriptive enough but why and how does this behaviour exist?: public class Layer { public string Name { get; set; } private IEnumerable<Layer> children; public IEnumerable<Layer> Children { get { return this.children.Where ( c => c.Name != null ).Select ( c => c ); } set { this.children = value; } } public Layer ( ) { this.children = new List<Layer> ( ); // Fine Layer layer = new Layer ( ); layer.children = new List<Layer> ( ); // Isn\'t .children private

“Incomplete type” in class which has a member of the same type of the class itself

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 11:22:14
I have a class that should have a private member of the same class, something like: class A { private: A member; } But it tells me that member is an incomplete type. Why? It doesn't tell me incomplete type if I use a pointer, but I'd rather not use a pointer. Any help is appreciated At the time you declare your member, you are still defining the A class, so the type A is still undefined. However, when you write A* , the compiler already knows that A stands for a class name, and so the type "pointer to A" is defined . That's why you can embed a pointer to the type your are defining. The same

What is the benefit of private name mangling in Python?

情到浓时终转凉″ 提交于 2019-11-26 10:59:23
问题 Python provides private name mangling for class methods and attributes. Are there any concrete cases where this feature is required, or is it just a carry over from Java and C++? Please describe a use case where Python name mangling should be used, if any? Also, I\'m not interested in the case where the author is merely trying to prevent accidental external attribute access. I believe this use case is not aligned with the Python programming model. 回答1: It's partly to prevent accidental