access-specifier

How do I dynamically define a method as private?

蓝咒 提交于 2019-11-30 16:45:44
问题 This does not seem to work: class Test private define_method :private_method do "uh!" end end puts Test.new.private_method 回答1: Test.instance_eval { private :private_method } Or, just run private :private_method from within the Test class. 回答2: It seems that starting with Ruby 2.1, define_method respects private : $ rvm 2.1.0 $ ruby /tmp/test.rb /tmp/test.rb:10:in `<main>': private method `private_method' called for #<Test:0x00000102014598> (NoMethodError) $ rvm 2.0 $ ruby /tmp/test.rb uh! (I

Why access specifiers can't be used for variables declared inside method in a Java Class?

雨燕双飞 提交于 2019-11-30 10:22:49
Why we can't use access specifiers for variables declared inside method in a Java Class? Because it doesn't make sense. Variables declared in a method are local to the method; i.e. they can't be accessed outside the method. What would modifying the variable's declaration achieve? It would make no sense to do so. A local variable (one declared in a method) is only in scope during that method - what would it even mean to declare that as "public" or "protected"? Only code within that method is going to know about it, and it's not like you're going to differentiate between different bits of code

How to make the class constructor private in Ruby?

最后都变了- 提交于 2019-11-30 01:12:10
class A private def initialize puts "wtf?" end end A.new #still works and calls initialize and class A private def self.new super.new end end doesn't work altogether So what's the correct way? I want to make new private and call it via a factory method. Try this: class A private_class_method :new end More on APIDock The second chunk of code you tried is almost right. The problem is private is operating in the context of instance methods instead of class methods. To get private or private :new to work, you just need to force it to be in the context of class methods like this: class A class <<

Difference between the default access specifier and protected access specifier in java

隐身守侯 提交于 2019-11-29 22:54:56
I was trying to learn java and when I went through access specifiers I had a doubt, what is the difference between the default one if none is specified and the protected access specifier? The protected specifier allows access by all subclasses of the class in question, whatever package they reside in, as well as to other code in the same package. The default specifier allows access by other code in the same package, but not by code that is in subclasses residing in different packages. See Java Language Specification Section 6.6 . EDIT: Per request of Michael Schmeißer (so others don't have to

Are there good reasons for 'private' to work the way it does in Ruby?

耗尽温柔 提交于 2019-11-29 21:27:52
It took me a while to understand how private methods work in Ruby, and it really strikes me as being very awkward. Does anyone know if there are good reasons for private methods to be handled the way they are? Is it just historic reasons? Or implementation reasons? Or are there good solid logical reasons (ie. semantic)? For example: class Person private attr_reader :weight end class Spy < Person private attr_accessor :code public def test code #(1) OK: you can call a private method in self Spy.new.code #(2) ERROR: cannot call a private method on any other object self.code #(3) ERROR!!! cannot

What's the access modifier of the default constructor in java?

寵の児 提交于 2019-11-29 21:23:44
We all know that if we don't specifically define a constructor, the compiler inserts an invisible zero-parameter constructor. I thought its access modifier was public, but in dealing with an inner class issue, I found maybe I was wrong. Here is my code: public class Outer { protected class ProtectedInner { // adding a public constructor will solve the error in SubOuterInAnotherPackage class //public ProtectedInner() {} } } And there is a subclass of Outer in another package: public class SubOuterInAnotherPackage extends Outer { public static void main(String[] args) { SubOuterInAnotherPackage

Why access specifiers can't be used for variables declared inside method in a Java Class?

流过昼夜 提交于 2019-11-29 15:40:09
问题 Why we can't use access specifiers for variables declared inside method in a Java Class? 回答1: Because it doesn't make sense. Variables declared in a method are local to the method; i.e. they can't be accessed outside the method. What would modifying the variable's declaration achieve? 回答2: It would make no sense to do so. A local variable (one declared in a method) is only in scope during that method - what would it even mean to declare that as "public" or "protected"? Only code within that

C++: Why does my DerivedClass's constructor not have access to the BaseClass's protected field?

自古美人都是妖i 提交于 2019-11-29 00:31:56
问题 I have a constructor attempting to initialize a field in a base class. The compiler complains. The field is protected, so derived classes should have access. //The base class: class BaseClass { public: BaseClass(std::string); BaseClass(const BaseClass& orig); virtual ~BaseClass(); const std::string GetData() const; void SetData(const std::string& data); protected: BaseClass(); std::string m_data; }; BaseClass::BaseClass(const std::string data) : m_data(data) { } BaseClass::BaseClass() { }

Why is it allowed to call derived class' private virtual method via pointer of base class?

穿精又带淫゛_ 提交于 2019-11-28 23:31:39
# include <iostream> using namespace std; class A { public: virtual void f() { cout << "A::f()" << endl; } }; class B:public A { private: virtual void f() { cout << "B::f()" << endl; } }; int main() { A *ptr = new B; ptr->f(); return 0; } This code works correctly and prints B::f(). I know how it works, but why is this code allowed? Access control is performed at compile time, not runtime. There's no way in general for the call to f() to know the runtime type of the object pointed to by ptr , so there's no check on the derived class's access specifiers. That's why the call is permitted. As for

Java tutorial says I can have a package-private interface, but I can't

人走茶凉 提交于 2019-11-28 19:11:40
In the Java tutorial "Defining an Interface" , it says If you do not specify that the interface is public , your interface will be accessible only to classes defined in the same package as the interface. However, this interface PPInterface { void foo(); void bar(); } class NewClass implements PPInterface { void foo() {} void bar() {} } generates compiler errors in NewClass because I am 'attempting to assign weaker access privileges; was public'. So the documentation is wrong, or I did something wrong, or I misinterpreted the documentation? I suppose I don't have to use an interface-- I like it