private-members

Is there a way to write an equality test for a VBA class with private members without exposing knowledge of the existence of those private members?

五迷三道 提交于 2019-11-29 14:58:09
问题 I do a fair amount of Excel VBA programming, but not a lot of it is object-oriented. Here is something that comes up every now and then that bugs me, and I'm wondering if there's something I'm missing. In VBA, say I have a class C defined with some private members like so: '... Private hidden1_ As Double Private hidden2_ As Double '... If VBA worked like C++ or (most?) other languages that support OOP, I could write a member function to do an equality test between instances of class C like

Java private field access possible when having a reference?

早过忘川 提交于 2019-11-29 09:58:06
I came across the following "strange" feature today - if you have a reference to an object from the class A in the body of the class A you can access the private fields of this object - i.e: public class Foo{ private int bar; private Foo foo; public void f() { if(foo.bar == bar) // foo.bar is visible here?! { // } } } Anyone has a good explanation about this? Access modifiers work at the class level, not at the instance level: all code in the same class can access private members of all instances of the class. Nothing particularly strange about it. It is intended to be this way. Citing the

Private method in groovy is not private

廉价感情. 提交于 2019-11-29 09:14:47
class A { private def sayHello() { println "Anish" } } def a_obj = new A() a_obj.sayHello() output : Anish Is there any way to protect sayHello() in groovy or am I missing something? Andrey Adamovich There is defect on that in Groovy issue tracking system and that defect is still open. tim_yates Searching for [groovy] private reveals: groovy call private method in Java super class What does 'private' mean in Groovy? How to define private getter method in Groovy Bean? It's not clear if it is a bug or by design, but it is going to get looked at again in Groovy 2.0 Jason You can use closures to

Java - access private instance variables

删除回忆录丶 提交于 2019-11-29 08:57:46
I need to access the private variables from the following class listing (Species.java) in order to use them in the KlingonOx.java class. The purpose of the KlingonOx.java class is to determine after how many years the population of the Elephant species will be larger than the population of the Klingon Ox species. Here is the Species.java class: import java.util.Scanner; public class Species { private String name; private int population; private double growthRate; public void readInput() { Scanner keyboard = new Scanner(System.in); System.out.println("What is the species' name?"); name =

Declaring private member variables

不打扰是莪最后的温柔 提交于 2019-11-29 06:43:26
I've started learning Objective-C a few weeks ago and I still don't understand how to manage the encapsulation of a class correctly. What is the best way to declare a private member variable in a class? It seems that setting the right getter/setter for your member variable with "@property" is the right way to go, more than just declaring it "@private" in the interface. But it seems to me that this still gives other classes an access to these variables. Even if you declare the property "readonly", an outside class can access the reference to the member variable and modify it! So I'm guessing

Why do people write private-field getters returning a non-const reference?

坚强是说给别人听的谎言 提交于 2019-11-29 06:00:55
We can all agree on public variables being bad for encapsulation and all that. However, I noticed a lot of code that does this type of stuff: class foo { private: int integer_; string someString_; // other variables public: int& integer() { return integer_; } string& someString() { return someString_; } // other "functions" } int main() { foo f; f.integer() = 10; f.someString() = "something"; return 0; } I have seen this being used in many places and I don't get why. Basically it returns a reference to the data and thus exposes it directly to the outside. So encapsulation is not really

Naming convention for private fields

烈酒焚心 提交于 2019-11-29 02:57:05
First, I know this question has been asked several times before and that in the end, it is mostly a matter of personal preference, but reading all the threads about the subject, some things are not clear to me. Basically, something that most people agree with at least is that public member should be PascalCased while private members should be lowerCamelCased. The matter that usually brings debate is whether or not to prefix the private members by an underscore, or anything else. Prefixing violates several StyleCop rules (which can obviously be turned off though) The rationale to not prefixing

Why can private member variable be changed by class instance?

∥☆過路亽.° 提交于 2019-11-29 02:55:41
class TestClass { private string _privateString = "hello"; void ChangeData() { TestClass otherTestClass = new TestClass(); otherTestClass._privateString = "world"; } } This code compiles in C# and the equivalent works in PHP, but can someone explain the reason why otherTestClass._privateString can be changed here ? I would have thought an instance of a class should not be able to change a private member variable under any circumstances, and that trying to access otherTestClass._privateString would give an 'inaccessible due to protection level' error. This is not the case though, so why does

Why can private member variable be changed by class instance?

时光总嘲笑我的痴心妄想 提交于 2019-11-29 02:53:57
class TestClass { private string _privateString = "hello"; void ChangeData() { TestClass otherTestClass = new TestClass(); otherTestClass._privateString = "world"; } } This code compiles in C# and the equivalent works in PHP, but can someone explain the reason why otherTestClass._privateString can be changed here ? I would have thought an instance of a class should not be able to change a private member variable under any circumstances, and that trying to access otherTestClass._privateString would give an 'inaccessible due to protection level' error. This is not the case though, so why does

Make a property that is read-only to the outside world, but my methods can still set

这一生的挚爱 提交于 2019-11-28 23:42:39
问题 In JavaScript (ES5+), I'm trying to achieve the following scenario: An object (of which there will be many separate instances) each with a read-only property .size that can be read from the outside via direct property read, but cannot be set from the outside. The .size property must be maintained/updated from some methods which are on the prototype (and should stay on the prototype). My API is already defined by a specification so I can't modify that (I'm working on a polyfill for an already