encapsulation

Package and visibility

*爱你&永不变心* 提交于 2019-12-29 00:59:25
问题 I'm making an SDK and I'm trying to separate classes to different packages, those classes use some other shared classes. The issue is if I made the shared classes public everyone will be able to see them, not only my classes. What's the right way to make them only accessible by my application? Example : Package a MyClass1 Package b MyClass2 Package c public MySharedClass Because c is public MySharedClass will be able to access it, but the issue is that it will also will be visible to the

Understanding the difference between __getattr__ and __getattribute__

时光毁灭记忆、已成空白 提交于 2019-12-28 01:39:40
问题 I am trying to understand the difference between __getattr__ and __getattribute__ , however, I am failing at it. The answer to the Stack Overflow question Difference between __getattr__ vs __getattribute__ says: __getattribute__ is invoked before looking at the actual attributes on the object, and so can be tricky to implement correctly. You can end up in infinite recursions very easily. I have absolutely no idea what that means. Then it goes on to say: You almost certainly want __getattr__ .

Easier way to write encapsulated parent/child data structure?

拜拜、爱过 提交于 2019-12-25 09:03:36
问题 From time to time I find myself often writing a data structure of "parents" and "children", where: A parent has references to 0 to N distinct children. A child has a reference to 0 parents or 1 parent. The reference must be mutual. For any given parent, any child that it references must also reference the given parent back. For any given child, the parent that it references must reference the given child back. It's impossible to violate the above rules through use of members accessible from

Restrict object's creation

徘徊边缘 提交于 2019-12-25 05:16:12
问题 I have a very strange question here. I have Class A, Class B and Class C. I want to create object of Class B which can create the object of A. But how to restrict Class C from creating object of Class A. One way is define Class A inside Class B, so that Only Class B can create the instance of Class A and Class C cannot access Class A. But, I think nesting is a wrong way. Do we have any way to restrict the object creation? Is it possible to use attribute and reflection to help to restrict? If

Unexpected T_CONSTANT_ENCAPSED_STRING error in SQL Query

扶醉桌前 提交于 2019-12-25 04:34:16
问题 I am getting an unexpected T_CONSTANT_ENCAPSED_STRING error in the following SQL query: mysql_query (UPDATE 'wp_posts' SET 'post_status' = 'publish' WHERE 'post_id' = '$id'); Can you guys see where the error might be? Here is the full code in case it helps: $key = 'feed'; $post_ids = array(2263, 2249); foreach ($post_ids as $id) { $feedurl = get_post_custom_values($key, $id); $feedurlstr = implode($feedurl); // Ignore - it determines whether feed is live and returns $result LiveOrNot(

How to implement validation with encapsulation

左心房为你撑大大i 提交于 2019-12-24 22:27:33
问题 Please first see original question: Encapsulation in JavaScript with getter and setter @Jacob Thanks Jacob! That is great information.I am not quite sure how that solution works but placing the methods into that return clause works well. Here is my working Class definition: function vehicle(thewheels, thecolour){ var colour=thecolour; var wheels=thewheels > 4 ? '4' : thewheels; return { getcolour: function() { return colour; }, setcolour: function(value) { colour = value; }, getwheels:

If I'm the only developer on a project, do I still need to use encapsulation? [closed]

扶醉桌前 提交于 2019-12-24 16:43:09
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 5 years ago . I always hear that we need to encapsulate whenever we write object-oriented code. If I'm the only developer on a project, do I still need to use encapsulation? 回答1: One way to put an answer: Encapsulation, conceptually, exists for writing better, safer, less error-prone code.

C++, using a key class as a key of access for a group of classes

扶醉桌前 提交于 2019-12-24 10:38:12
问题 I've seen a pattern like this in some project: class AccessKey{ // a group of classes called privilegedClasses friend class foo; friend class bar; // friend class other classes in privilegedClasses... private : AccessKey(){}; /*! private constructor */ static const AccessKey key; /*! private object, only for friend classes */ } This is a class that only privilegedClasses can access to it and have an object of it. Now consider someone is writing a function and want to limit access to that

Python - Mutliprocess, member functions of classes

流过昼夜 提交于 2019-12-24 09:05:33
问题 I can't figure out if this is because of me, or the multiprocessing module that Python2.7 has. Can anyone figure out why this is not working? from multiprocessing import pool as mp class encapsulation: def __init__(self): self.member_dict = {} def update_dict(self,index,value): self.member_dict[index] = value encaps = encapsulation() def method(argument): encaps.update_dict(argument,argument) print encaps.member_dict p = mp() #sets up multiprocess pool of processors p.map(method,sys.argv[1:])

Combining javascript pseudo-class objects under a single namespace

雨燕双飞 提交于 2019-12-24 08:46:29
问题 I have some classes I ported from Java to JavaScript. I would like to combine them in a library. for example: var myLibrary = { version: 1.0, ...more code... }; Now a class I would like to add to this library (this was ported from Java to JavaScript!) follows: Edit: new version below This class works as beautifully as is, but encapsulating it is proving difficult. I would like to do something like this: var ticker = new myLibrary.jTicker(30,10); var otherObj = new myLibrary.object2(); The