access-modifiers

Why do we declare private fields when we have accessors and mutators? [duplicate]

强颜欢笑 提交于 2019-11-30 09:23:47
问题 This question already has answers here : Why are getter and setter method important in java? [duplicate] (6 answers) Closed 4 years ago . If I create a class in Java, I've always been taught that the convention is to make all fields of a class private. If I need to access or change them, I can create an accessor and mutator method. I do understand the importance of private variables, as they help reduce complexity and allow for encapsulation. What I don't understand is that if I create a

How do I make my controls inside a UserControl private?

好久不见. 提交于 2019-11-30 08:07:31
I have a user control with a ComboBox and a TextBox. Everything is working great except I noticed that from my user control's instance object, I can access those two controls. They shouldn't be accessible except via my own exposed properties. You can use the x:FieldModifier attribute on your controls, thusly: <TextBox x:Name="textBox1" x:FieldModifier="private" /> Interestingly the MSDN page for x:FieldModifier doesn't give "private" as a valid value for the attribute, but I've tested it and it works. 来源: https://stackoverflow.com/questions/300255/how-do-i-make-my-controls-inside-a-usercontrol

Change the access modifier of an overridden method in Java?

痞子三分冷 提交于 2019-11-30 06:45:47
Is there a reason one can change the access modifier of an overridden method? For instance, abstract class Foo{ void start(){...} } And then change the package-private access modifier to public , final class Bar extends Foo{ @Override public void start(){...} } I'm just asking this question out of curiosity. Java doesn't let you make the access modifier more restrictive , because that would violate the rule that a subclass instance should be useable in place of a superclass instance. But when it comes to making the access less restrictive... well, perhaps the superclass was written by a

Protected Classes in .NET

。_饼干妹妹 提交于 2019-11-30 04:44:08
Can a class be protected in.NET? Why is / isn't this possible? Yes, you just cannot make them top level classes, they must be inner classes public class Outer { protected class Foo { } } This is fine, it means that the only classes allowed to see Foo are sub classes of Outer class X { // 'Outer.Foo' is inaccessible due to its protection level private void Flibble(Outer.Foo foo) { } } class X : Outer { // fine private void Flibble(Outer.Foo foo) { } } Note that you cannot declare any outer class as private, protected (or protected internal) in c# since the access modifier for outer level

Access Modifiers (Private, Protected) in ES6

别等时光非礼了梦想. 提交于 2019-11-30 04:00:43
Note: I already went through the below SO Question and 7 Answers (as of now) about Symbols , WeekMaps and Maps, Please read the full question before you vote: Private properties in JavaScript ES6 classes Article: https://esdiscuss.org/topic/es7-property-initializers Below is my Simple Class which contains Private, Public and Protected Properties and Methods. 'use strict'; class MyClass { constructor () { this.publicVar = 'This is Public Variable'; this.privateVar = 'This is Private Variable'; this.protectedVar = 'This is Protected Variable'; } // Public Constructor Method. publicMethod () {

C# private, static, and readonly

丶灬走出姿态 提交于 2019-11-30 03:02:14
I was reviewing some code for log4net and I came across this. private static readonly ILog logger = LogManager.GetLogger(typeof(AdminClient)); I am wondering why would you need to have private static readonly. From my understanding private would mean that the variable cannot not be used outside the class unless there is a accessor method or get property. static would mean that the variable is scoped only in this file only. readonly would mean that you can only read from the value and cannot assign it. So, I am thinking that the person who wrote this code. declared it private as they don't want

Why should constructors on abstract classes be protected, not public?

人盡茶涼 提交于 2019-11-30 01:45:50
ReSharper suggests changing the accessibility of a public constructor in an abstract class to protected , but it does not state the rationale behind this. Can you shed some light? JaredPar Simply because being public makes no sense in an abstract class. An abstract class by definition cannot be instantiated directly. It can only be instantiated by an instance of a derived type. Therefore the only types that should have access to a constructor are its derived types and hence protected makes much more sense than public. It more accurately describes the accessibility. It technically makes no

Difference between Private Sub, Function and Class

时光怂恿深爱的人放手 提交于 2019-11-29 22:46:48
What are the differences between the following: Private Sub Private Function Private Class When should each one be used? Private is a modifier than gives the scope of the class, sub, or function. A sub and a function are both subroutines, or sections of code that can be called in the program. The difference between them is that a function has a return value and a sub does not. A class is a group of code that can include subs, functions, and other stuff. Sub is like a function but it doesnt returns any values it just executes a proccess Class is a Class, Sub and Function are methods, private is

MVVM: Should a VM object expose an M object directly, or only through getters delegating to M's getters?

别等时光非礼了梦想. 提交于 2019-11-29 20:26:24
the best way to explain is with example so: this is the model public class Person { public int age; public string name; } this is the view model public class PersonVM { } my question is: should the vm expose the person to the data template or encapsulate the model properties with his own properties? The view model should declare its own properties and hide the specifics of the model from the view. This gives you the most flexibility, and helps keep view model-type issues from leaking into the model classes. Usually your view model classes encapsulate the model by delegation. For example, class

Why do we declare private fields when we have accessors and mutators? [duplicate]

让人想犯罪 __ 提交于 2019-11-29 15:19:07
This question already has an answer here: Why are getter and setter method important in java? [duplicate] 6 answers If I create a class in Java, I've always been taught that the convention is to make all fields of a class private. If I need to access or change them, I can create an accessor and mutator method. I do understand the importance of private variables, as they help reduce complexity and allow for encapsulation. What I don't understand is that if I create a public accesor and mutator method, isn't the variable public at that point? Why is it still convention to use private variables