access-specifier

Why can I access private/protected methods using Object#send in Ruby?

Deadly 提交于 2019-12-21 17:02:08
问题 The class class A private def foo puts :foo end public def bar puts :bar end private def zim puts :zim end protected def dib puts :dib end end instance of A a = A.new test a.foo rescue puts :fail a.bar rescue puts :fail a.zim rescue puts :fail a.dib rescue puts :fail a.gaz rescue puts :fail test output fail bar fail fail fail .send test [:foo, :bar, :zim, :dib, :gaz].each { |m| a.send(m) rescue puts :fail } .send output foo bar zim dib fail The question The section labeled "Test Output" is

What is the difference between access specifier protected and internal protected in C#

旧时模样 提交于 2019-12-21 12:58:06
问题 What is the difference between access specifier protected and internal protected in C# ? 回答1: Internal can be seen within the assembly. Protected can be seen by classes inheriting from the class where it is defined. Protected internal can be seen within the assembly OR types derived from the class where it is defined (including types from other assemblies). See: http://msdn.microsoft.com/en-us/library/ba0a1yw2.aspx Copied from the page: public Access is not restricted. protected Access is

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

半城伤御伤魂 提交于 2019-12-17 22:22:38
问题 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

Why would a virtual function be private?

拥有回忆 提交于 2019-12-17 21:54:43
问题 I just spotted this in some code: class Foo { [...] private: virtual void Bar() = 0; [...] } Does this have any purpose? (I am trying to port some code from VS to G++, and this caught my attention) 回答1: See this Herb Sutter article as to why you'd want to do such a thing. 回答2: This is a pure virtual function that happens to be private. This makes it so that a derived class must implement the method. In this case Bar. I think you may be confused b/c this is done to create "interfaces" in C++

What are the differences between “private”, “public”, and “protected methods”?

旧巷老猫 提交于 2019-12-17 17:49:07
问题 I'm learning Ruby, and have come up to a point where I am confused. The book I am using is talking about private , public , and protected methods , but I am still a bit confused. What are the differences between each? 回答1: Public - can be called from anywhere Private - The method cannot be called outside class scope. The object can only send the message to itself ex: the baker has bake method as public but break_eggs is private Protected - You can call an object's protected methods as long as

What is a good example to differentiate between fileprivate and private in Swift3

旧城冷巷雨未停 提交于 2019-12-17 05:32:31
问题 This article has been helpful in understanding the new access specifiers in Swift 3 . It also gives some examples of different usages of fileprivate and private . My question is - isn't using fileprivate on a function that is going to be used only in this file the same as using private ? 回答1: fileprivate is now what private used to be in earlier Swift releases: accessible from the same source file. A declaration marked as private can now only be accessed within the lexical scope it is

What actually occurs when stating “private”/“protected” in Ruby?

谁说胖子不能爱 提交于 2019-12-12 08:49:11
问题 What is actually occurring when private / protected is stated within a Ruby class definition? They are not keywords, so that implies they must be method calls, but I cannot find where they are defined. They do not appear to be documented. Are the two different ways of declaring private / protected methods (shown below) implemented differently? (The second way is obviously a method call, but this is not so apparent in the first way.) class Foo private def i_am_private; end def so_am_i; end end

Private vs Protected method in ruby

别说谁变了你拦得住时间么 提交于 2019-12-12 00:06:49
问题 If a method is protected, it may be called by any instance of the defining class or its subclasses. If a method is private, it may be called only within the context of the calling object—it is never possible to access another object’s private methods directly, even if the object is of the same class as the caller. This definition i got from net when searching for the difference between private and protected methods in ruby. I have 2 doubts in this class Abc def abc xyz end protected def xyz p

static and private method behavior when calling direct on object of child class sounds like overriding?

回眸只為那壹抹淺笑 提交于 2019-12-11 12:34:06
问题 public class B extends A{ public static void main(String[] args) { new B().privateMethod();//no error -output B-privateMethod.Sounds like overriding new B().staticMethod(); //no error -output B-StaticMethod.Sounds like overriding } private void privateMethod() { System.out.println("B-privateMethod."); } static void staticMethod() { System.out.println("B-StaticMethod."); } } class A{ private void privateMethod() { System.out.println("A-privateMethod."); } static void staticMethod() { System

C++ access specifiers

末鹿安然 提交于 2019-12-11 08:37:01
问题 I just want to make sure I got the idea of public and private right. Regarding the private access specifier, does it mean: Only accessed inside the class Cannot be accessed from the object of the class unless there are public class methods that can be used to access them ( Can other objects use those public functions? ) No other object can access them And for public: Accessed from the object of the class Accessed from any other object Is that right? 回答1: I think there is an issue of