encapsulation

Encapsulation concept

耗尽温柔 提交于 2019-11-29 10:28:39
问题 I have problem with concept and implementation of encapsulation. Can someone explain it to me? 回答1: Encapsulation is a moderately easy concept once you realise it (probably) comes from the same base word as capsule. It's simply a containment of information. Encapsulation means that a class publishes only what is needed for others to use it, and no more. This is called information hiding and it means classes can totally change their internals without having an effect on any of their users. In

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

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

Pattern for Creating a Simple and Efficient Value type

蓝咒 提交于 2019-11-29 02:28:17
问题 Motivation: In reading Mark Seemann’s blog on Code Smell: Automatic Property he says near the end: The bottom line is that automatic properties are rarely appropriate. In fact, they are only appropriate when the type of the property is a value type and all conceivable values are allowed. He gives int Temperature as an example of a bad smell and suggests the best fix is unit specific value type like Celsius. So I decided to try writing a custom Celsius value type that encapsulates all the

Objective-c: why private ivars are not hidden from the outside access when using KVC

孤街醉人 提交于 2019-11-29 02:20:54
After trying to access ivars using KVC, I have noticed that there was no protection on private and protected ivars. It doesn't matter what I put a in front of the ivar (private or protected keyword) - an ivar is always a public ivar when using KVC method "setValue". Here is my code where all of the seven ivars and properties are changeble outside the class instance: //************ interface file ***************// @interface MyClass : NSObject { @public NSNumber *public_num; @protected NSNumber *protected_num; @private NSNumber *private_num; NSNumber *private_property; } @property (retain)

Why would you declare getters and setters method private? [duplicate]

限于喜欢 提交于 2019-11-29 01:09:49
This question already has an answer here: Why use getters and setters/accessors? [closed] 38 answers I saw a code where getters and setters methods are declared private. I am trying to figure out the logic behind it, and I am really having hard time to understand why would you declare them as private? That's exactly opposite of what we are trying to achieve through getters and setters. I can think of several reasons: you want to prevent future public access. If a different programmer sees your code and wants access to a variable, but there are no setters and getters, he might think you just

Can you explain this thing about encapsulation?

怎甘沉沦 提交于 2019-11-28 21:23:11
In response to What is your longest-held programming assumption that turned out to be incorrect? question, one of the wrong assumptions was: That private member variables were private to the instance and not the class. ( Link ) I couldn't catch what he's talking about, can anyone explain what is the wrong/right about that with an example? public class Example { private int a; public int getOtherA(Example other) { return other.a; } } Like this. As you can see private doesn't protect the instance member from being accessed by another instance. BTW, this is not all bad as long as you are a bit

SessionsHelper in railstutorial.org: Should helpers be general-purpose modules for code not needed in views?

旧巷老猫 提交于 2019-11-28 21:19:03
railstutorial.org has a suggestion which strikes me as a little odd. It suggests this code : class ApplicationController < ActionController::Base protect_from_forgery include SessionsHelper end The include SessionsHelper makes the methods available from ApplicationController , yes, but it makes them available in any view, as well. I understand that authentication/authorization is cross-cutting, but is this really the best place? That seems to me to be potentially too broad of a scope. Putting code which implements, say, a before_filter which conditionally redirects (as the railstutorial.org