access-modifiers

Which part of JLS said anonymous classes cannot have public/protected/private member classes

大兔子大兔子 提交于 2020-01-01 08:29:57
问题 Consider this piece of code: public class TopLevelClass { Cloneable c = new Cloneable() { private int privateField; private void privateMethod() {}; }; } There is an anonymous class that has a private member field and a private member method. It has been successfully compiled. Then consider this one: public class TopLevelClass { Cloneable c = new Cloneable() { private class PrivateInnerClass {} }; } There is an anonymous class that has a private member class. However... javac said: error:

Why can't I use protected constructors outside the package? [duplicate]

谁说胖子不能爱 提交于 2019-12-31 17:51:12
问题 This question already has answers here : What is the difference between public, protected, package-private and private in Java? (28 answers) Closed 4 years ago . Why can't I use protected constructors outside the package for this piece of code: package code; public class Example{ protected Example(){} ... } Check.java package test; public class Check extends Example { void m1() { Example ex=new Example(); //compilation error } } Why do i get the error even though i have extended the class?

How do I mimic access modifiers in JavaScript with the Prototype library?

≡放荡痞女 提交于 2019-12-30 11:24:10
问题 I've been working with the prototype library for some time now and occasionally find myself wishing I had multiple access levels (public, private, and protected). The closest I've come so far is the following: SampleBase = Class.create({ /* virtual public constructor */ initialize: function(arg1, arg2) { // private variables var privateVar1, privateVar2; // private methods var privateMethod1 = function() { } function privateMethod2() { } // public (non virtual) methods this.publicNonVirtual1

Prevent visual studio from limiting the setter method to internal

不羁的心 提交于 2019-12-30 02:47:08
问题 Well, I use visual studio 2015 CE, update 2. One productivity hack I usually do is that I create empty model classes like: public class PersonModel { } and then use them in a select expression like: db.People.Where(p => someCondition) .Select(p => new PersonModel { Id = p.Id, Name = p.Name, //set other properties }).ToList(); Then I go to the yet non-existing properties Id and Name , ... and press Control+. to ask visual studio to generate property Id for me. All great, but it will create:

Is there a reason you can not define the access modifier on a method or in an interface?

老子叫甜甜 提交于 2019-12-29 08:41:31
问题 The responsibility of the visibility of a method is relegated to the class that implements the interface. public interface IMyInterface { bool GetMyInfo(string request); } In C# set access modifier public, private or protected before the method GetMyInfo() generates the following error: The modifier 'private' is not valid for this item. Is there a reason you can not define the access modifier on a method or in an interface? (Question already asked in french here) 回答1: The interface defines a

What is the difference between access specifiers and access modifiers?

拈花ヽ惹草 提交于 2019-12-29 02:54:46
问题 In Java, are access specifiers and access modifiers the same thing? 回答1: "access modifier" is the official term for private , protected and public used in the Java language specification. "access specifier" is used synonymously in the Java API doc, but this is the first time I've noticed that. It's probably better to stick with the JLS term. 回答2: Referring to the Sun Java Docs they both seem to be the same: Access Modifier Search for access specifier on this page. 回答3: The term Access

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

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

Internal interfaces - exposing my ignorance

╄→尐↘猪︶ㄣ 提交于 2019-12-25 00:08:34
问题 I'm aware of these two questions which explain why I can't have a protected/private method in an interface, what I'm trying to work out is how to control from where my methods are called, briefly: public class EditField : IEditField { public EditField() {} public EditField(IStateMachine stateMachine) {} public void Action_Commit() {} public void Action_Undo() {} } Consumers can use the default IStateMachine, or roll their own. I was wondering if there is any way to ensure that Action_ methods

storing data in another form protection issues

谁都会走 提交于 2019-12-24 15:43:59
问题 I'm trying to store a string from one form to a label on another from. However, when doing so it says it cannot be done because of its protection level. Any ideas on how to fix this? maskedTxtLogin.Text = FormInvisible.lblInitials.Text(); 回答1: The controls are generated as a private field in your form designer: private System.Windows.Forms.Label lblInitials; If you want to access them outside of the form you need to create a property for them. To see the above declaration and write a property