encapsulation

Dosen't Reflection API break the very purpose of Data encapsulation?

旧街凉风 提交于 2019-11-30 19:29:51
Very recently I came across the Reflection API and to my surprise we can access and even alter the private variables.I tried the following code import java.lang.reflect.Field; public class SomeClass{ private String name = "John"; } public class Test{ public static void main(String args[]) throws Exception { SomeClass myClass = new SomeClass(); Field fs = myClass.getClass().getDeclaredField("name"); fs.setAccessible(true); System.out.println("Variable is " + fs.getName() + " and value is " + fs.get(myClass)); fs.set(myClass, "Sam"); System.out.println("Variable is " + fs.getName() + " and value

Why am I able to use my value constructor even though I don't export it?

∥☆過路亽.° 提交于 2019-11-30 19:22:13
For practice, I'm implementing a queue data type in a module called "Queue". My data type is also called "Queue", as is its only value constructor: module Queue (Queue, enq, emptyQueue) where data Queue a = Queue { inbox :: [a], outbox :: [a] } deriving (Eq, Show) emptyQueue :: Queue a emptyQueue = Queue [] [] enq :: a -> Queue a -> Queue a enq x (Queue inb out) = Queue (x:inb) out -- other function definitions (irrelevant here)... As far as I understand, because I wrote Queue , not Queue(..) or Queue(Queue) in the export statement, I don't expect the value constructor of my data type to be

Encapsulation C# newbie

巧了我就是萌 提交于 2019-11-30 18:56:42
问题 New to C#, and I understand that encapsulation is just a way of "protecting data". But I am still unclear. I thought that the point of get and set accessors were to add tests within those methods to check to see if parameters meet certain criteria, before allowing an external function to get and set anything, like this: private string myName; public string MyName;// this is a property, speical to c#, which sets the backing field. private string myName = "mary";// the backing field. public

Unencapsulated means Unchangeable?

限于喜欢 提交于 2019-11-30 17:24:50
问题 I came across this line in Effective C++ : Public means unencapsulated, and practically speaking, unencapsulated means unchangeable, especially for classes that are widely used.Yet widely used classes are most in need of encapsulation, because they are the ones that can most benefit from the ability to replace one implementation with a better one What does the author mean by "Public means unencapsulated, and practically speaking, unencapsulated means unchangeable"? And how is unencapsulated

Access to dll methods

我是研究僧i 提交于 2019-11-30 16:18:05
问题 I prepared some C# dll for my customer that doing some functionality. The thing is that I use also same dll. How can I make some methods available to him and all methods available for me. Thanks, 回答1: Use a shared code base Simply compile two projects. One contains the source for the DLL you are providing to the customer, and the other contains all the source, which you keep for yourself. Advantage: They see none of the source you want to keep for yourself, and you don't have to set up any

Confused with Java Encapsulation Concept

风流意气都作罢 提交于 2019-11-30 15:54:31
问题 Good day! I am reading a Java book about encapsulation and it mentioned the getter and setter method. I've read that to hide the attributes, I must mark my instance variables as "PRIVATE" and make a "PUBLIC" method of getter and setter to access the data. So I tried making a similar but not the conventional code about it as follows: public class AddressBookEntry { private String name; private String address; private String telNo; private String email; public void getAllInfo() { name =

Confused with Java Encapsulation Concept

ぐ巨炮叔叔 提交于 2019-11-30 15:26:27
Good day! I am reading a Java book about encapsulation and it mentioned the getter and setter method. I've read that to hide the attributes, I must mark my instance variables as "PRIVATE" and make a "PUBLIC" method of getter and setter to access the data. So I tried making a similar but not the conventional code about it as follows: public class AddressBookEntry { private String name; private String address; private String telNo; private String email; public void getAllInfo() { name = JOptionPane.showInputDialog("Enter Name: "); address = JOptionPane.showInputDialog("Enter Address: "); telNo =

Linking enum value with localized string resource

有些话、适合烂在心里 提交于 2019-11-30 14:36:53
Related: Get enum from enum attribute I want the most maintainable way of binding an enumeration and it's associated localized string values to something. If I stick the enum and the class in the same file I feel somewhat safe but I have to assume there is a better way. I've also considered having the enum name be the same as the resource string name, but I'm afraid I can't always be here to enforce that. using CR = AcmeCorp.Properties.Resources; public enum SourceFilterOption { LastNumberOccurences, LastNumberWeeks, DateRange // if you add to this you must update FilterOptions.GetString }

Advantages to Nested Classes For Listeners in GUIs

我是研究僧i 提交于 2019-11-30 12:49:39
For decently sized projects I've been told that when you have classes extending JPanels that the best practice is to use nested classes to implement the listeners. For example I could have a class FactoryScreen that extends JPanel, and have a nested class FactoryScreenBrain that implements all the necessary listeners. I've never been able to get a good explanation for specific benefits or disadvantages to encapsulating my classes in this fashion, and until now have always just had classes that both extend JPanel and implement listeners. Can someone provide me some guidance on this? Having

How to integrate a library that uses expression templates?

霸气de小男生 提交于 2019-11-30 11:19:40
I would like to use the Eigen matrix library as the linear algebra engine in my program. Eigen uses expression templates to implement lazy evaluation and to simplify loops and calculations. For example: #include<Eigen/Core> int main() { int size = 40; // VectorXf is a vector of floats, with dynamic size. Eigen::VectorXf u(size), v(size), w(size), z(size); u = 2*v + w + 0.2*z; } Since Eigen uses expression templates, code like u = 2*v + w + 0.2*z; In the above mentioned sample reduces to a single loop of length 10 (not 40, the floats are put into regiser by chunks of 4) without creating a