encapsulation

Set and Get Methods in java?

十年热恋 提交于 2019-11-26 17:26:18
How can I use the set and get methods, and why should I use them? Are they really helpful? And also can you give me examples of set and get methods? Set and Get methods are a pattern of data encapsulation. Instead of accessing class member variables directly, you define get methods to access these variables, and set methods to modify them. By encapsulating them in this manner, you have control over the public interface, should you need to change the inner workings of the class in the future. For example, for a member variable: Integer x; You might have methods: Integer getX(){ return x; } void

Properties vs. Fields: Need help grasping the uses of Properties over Fields

北城以北 提交于 2019-11-26 15:26:00
First off, I have read through a list of postings on this topic and I don't feel I have grasped properties because of what I had come to understand about encapsulation and field modifiers (private, public..ect). One of the main aspects of C# that I have come to learn is the importance of data protection within your code by the use of encapsulation. I 'thought' I understood that to be because of the ability of the use of the modifiers (private, public, internal, protected). However, after learning about properties I am sort of torn in understanding not only properties uses, but the overall

How do I return a reference to something inside a RefCell without breaking encapsulation?

北城余情 提交于 2019-11-26 14:43:00
I have a struct that has inner mutability. use std::cell::RefCell; struct MutableInterior { hide_me: i32, vec: Vec<i32>, } struct Foo { //although not used in this particular snippet, //the motivating problem uses interior mutability //via RefCell. interior: RefCell<MutableInterior>, } impl Foo { pub fn get_items(&self) -> &Vec<i32> { &self.interior.borrow().vec } } fn main() { let f = Foo { interior: RefCell::new(MutableInterior { vec: Vec::new(), hide_me: 2, }), }; let borrowed_f = &f; let items = borrowed_f.get_items(); } Produces the error: error[E0597]: borrowed value does not live long

Why encapsulation is an important feature of OOP languages? [closed]

試著忘記壹切 提交于 2019-11-26 13:10:29
问题 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 came across different interview where question was asked to me why encapsulation is used? Whose requirement actually is encapsulation? Is it for users of program? Or is it for co-workers? Or is it to protect code from hackers? 回答1: Encapsulation helps in isolating

Java: Subpackage visibility?

夙愿已清 提交于 2019-11-26 12:57:22
问题 I have two packages in my project: odp.proj and odp.proj.test . There are certain methods that I want to be visible only to the classes in these two packages. How can I do this? EDIT: If there is no concept of a subpackage in Java, is there any way around this? I have certain methods that I want to be available only to testers and other members of that package. Should I just throw everything into the same package? Use extensive reflection? 回答1: You can't. In Java there is no concept of a

C#: Difference between List<T> and Collection<T> (CA1002, Do not expose generic lists) [duplicate]

落花浮王杯 提交于 2019-11-26 12:53:23
问题 This question already has an answer here: Collection<T> versus List<T> what should you use on your interfaces? 8 answers Tried to run Run Code Analysis on a project here, and got a number of warnings that said something like this: CA1002 : Microsoft.Design : Change \'List< SomeType >\' in \' SomeClass.SomeProtectedOrPublicProperty \' to use Collection, ReadOnlyCollection or KeyedCollection Why should I use Collection<T> instead of List<T> ? When I look at the msdn documentation, they seem

Why “private” methods in the object oriented?

☆樱花仙子☆ 提交于 2019-11-26 12:21:40
问题 I understand it is a very basic concept in the oops. But still I cannot get my head around. I understood why member variables are private, so class user cannot abuse it by setting up invalid values. But how can this apply to the methods ? 回答1: Lot of good answers, but maybe one more from a self-taught Java programmer as I went through all that by myself with a lot of pain ;) Think about a Class as something seen from the outside, not as something you see internally . If you look at a Class

Should I return a Collection or a Stream?

寵の児 提交于 2019-11-26 11:33:13
Suppose I have a method that returns a read-only view into a member list: class Team { private List < Player > players = new ArrayList < > (); // ... public List < Player > getPlayers() { return Collections.unmodifiableList(players); } } Further suppose that all the client does is iterate over the list once, immediately. Maybe to put the players into a JList or something. The client does not store a reference to the list for later inspection! Given this common scenario, should I return a stream instead? public Stream < Player > getPlayers() { return players.stream(); } Or is returning a stream

How to access private data members outside the class without making “friend”s? [duplicate]

一世执手 提交于 2019-11-26 11:15:01
问题 This question already has answers here : Can I access private members from outside the class without using friends? (24 answers) Closed 2 years ago . I have a class A as mentioned below:- class A{ int iData; }; I neither want to create member function nor inherit the above class A nor change the specifier of iData . My doubts:- How to access iData of an object say obj1 which is an instance of class A ? How to change or manipulate the iData of an object obj1 ? Note : Don\'t use friend . 回答1:

Doctrine2 ORM does not save changes to a DateTime field

帅比萌擦擦* 提交于 2019-11-26 11:09:36
问题 I have a User entity: use Doctrine\\ORM\\Mapping as ORM; /** * ExampleBundle\\Entity\\User * * @ORM\\Entity() */ class User { // ... /** * @ORM\\Column(type=\"service_expires_at\", type=\"date\", nullable=true) */ private $service_expires_at; public function getServiceExpiresAt() { return $this->service_expires_at; } public function setServiceExpiresAt(\\DateTime $service_expires_at) { $this->service_expires_at = $service_expires_at; } } When i update the User\'s service_expires_at as