encapsulation

How can I derive classes from a base class that is encapsulated in a manager class?

核能气质少年 提交于 2019-12-13 02:21:48
问题 I have a ManagerClass that manages classes that derive from an abstract BaseClass. I need it so that only the ManageClass can access certain methods on the BaseClass. I also need certain methods to be accessible outside the scope of the ManagerClass. Since C# doesn't have friends, I'm encapsulating the BaseClass inside the ManagerClass. The problem: BaseClass is not accessible to derive from Making BaseClass public,means allowing DoStuff() and DoOtherStuff() to be called from outside the

Options for setting mandatory fields on components created by code?

泄露秘密 提交于 2019-12-13 02:14:39
问题 When I create a GameObject like below I want my component to have/set mandatory fields. GameObject go = new GameObject("MyGameObject"); MyComponent myComponent = go.addComponent(MyComponent); Since I cannot use a constructor to set private fields I either need a setter for these fields or a public Init function that sets multiple mandatory properties. But this allows changing the values anywhere in the code and unlike a constructor with overloads it's not obvious that Init "needs to" or "can

Is there any function in vb.net as an alternative to magic methods __get() and __set() in php

走远了吗. 提交于 2019-12-13 01:26:37
问题 PHP has magic methods like __get() and __set() . Is there any alternative for it in vb.net. Or is there any other tricks that can be done instead? This was what i done in php private $data = array(); public function __construct($arData) { $data['var1'] = 'value1'; $data['var2'] = 'value2'; $data['var3'] = 'value3'; } public function __get($propertyName) { if(!array_key_exists($propertyName, $this->data)) throw new Exception("Invalid property"); else return $this->data[$propertyName]; } Now i

Call private method from the world in c++

雨燕双飞 提交于 2019-12-13 01:26:22
问题 I realy don't understand why it works, but below code shows an example of calling private method from the world without any friend classes: class A { public: virtual void someMethod(){ std::cout << "A::someMethod"; } }; class B : public A { private: virtual void someMethod(){ std::cout << "B::someMethod"; } }; int main(){ A* a = new B; a->someMethod(); } outputs: B::someMethod Doesn't it violates encapsulation rules in C++? For me this is insane. The inheritance is public, but the access

java using getter and setter methods and returning 0

吃可爱长大的小学妹 提交于 2019-12-12 19:21:44
问题 I have created 2 timers in 2 separate classes. One timer increments int counter. and the other uses a get method and prints out the value of int counter. The problem is the second timer only prints out 0, 0, 0 etc if i use private int counter whereas if i were to use private static counter it prints out 1,2,3,4,5 etc which is what i want. But i would rather not use static because ive been told its bad practice. Here is my main class: import java.util.Timer; public class Gettest { public

Symfony2 : get security.context inside entity class

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-12 16:05:13
问题 Is it possible to get security.context inside an entity class? I know the following doesn't work. I don't know how to implement the $user part. /** * Set createdAt * * @ORM\PrePersist */ public function setCreatedAt() { $user = $this->get('security.context')->getToken()->getUser(); $this->owner = $user->getId(); $this->createdAt = new \DateTime(); $this->updatedAt = new \DateTime(); } 回答1: For datetimes: For the timestamps you are probably best of using the @hasLifeCycleCallback annotation

encapsulation for mixin's members in Scala

你说的曾经没有我的故事 提交于 2019-12-12 15:46:31
问题 Traits in Scala can be used as both mixins and interfaces. It leads to some inconsistence - if I want to close some method inside trait, I just can't do that: object Library { protected trait A { def a: Int = 5 } trait B extends A { private override def a: Int = super.a } //I want to close `a` memeber for all traits extending B; it's still possible to open it in some another trait `C extends A`, or even `Z extends B with C` } // Exiting paste mode, now interpreting. <console>:10: error:

Java: Which is faster? Local variables or accessing encapsulation?

五迷三道 提交于 2019-12-12 08:16:02
问题 I recently read a StackOverflow question that indicated, when accessing variables, it is faster to use the stack than the heap: void f() { int x = 123; // <- located in stack } int x; // <- located in heap void f() { x = 123 } However, I can't work it through my head which is faster in my example (since I assume they are both using the stack). I'm working on hitbox calculation and such, which uses alot of X-Y, width, height variables (up to 10-20 times for each) in the function. Is it faster

can we separate the main serialize method in different class to make it easier and less complex using boost libraries for c++?

若如初见. 提交于 2019-12-12 06:48:13
问题 how to separate the serialize method from this code and encapsulate it to a another class so that we don't have to write it in every class we create. class Test { private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & BOOST_SERIALIZATION_NVP(a); ar & BOOST_SERIALIZATION_NVP(b); ar & BOOST_SERIALIZATION_NVP(emp); } int a; int b; Employee *emp; public: Test(int a, int b,int c, int d) { this->a = a; this->b = b;

Instance-level encapsulation with C++

安稳与你 提交于 2019-12-12 01:59:54
问题 I have a two-part question. First, I understand that C++ provides only class-level data encapsulation, meaning that all objects of the same class have access to one another's private members. I understand the reason for this, but have found some links (i.e. http://www.programmerinterview.com/index.php/c-cplusplus/whats-the-difference-between-a-class-variable-and-an-instance-variable/) which appear to contradict this point, suggesting that I could do the following: class testclass { private: /