access-specifier

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

人盡茶涼 提交于 2019-11-28 18:59:53
问题 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

How to Access package private Class from a Class in some other package?

前提是你 提交于 2019-11-28 18:59:43
I have following classses Hello.java package speak.hello; import java.util.Map; import speak.hi.CustomMap; import speak.hi.Hi; public class Hello { private Hi hi; Hello(Hi hi) { this.hi = hi; } public String sayHello() { return "Hello"; } public String sayHi() { return hi.sayHi(); } public Map<String, Object> getMap() { return hi.getMap(); } public void clearMap() { hi.getMap().clear(); } public void discardMap() { CustomMap map = (CustomMap) hi.getMap(); map.discard(); } public static void main(String[] args) { Hello hello = new Hello(new Hi()); System.out.println(hello.sayHello()); System

What is the difference between access specifiers and access modifiers?

故事扮演 提交于 2019-11-28 17:12:18
In Java, are access specifiers and access modifiers the same thing? "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. Referring to the Sun Java Docs they both seem to be the same: Access Modifier Search for access specifier on this page . The term Access specifier used by c++ programmers not in java. In java Officially we use Access Modifier . For example: when we declare a

Why would a virtual function be private?

别说谁变了你拦得住时间么 提交于 2019-11-28 17:03:55
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) See this Herb Sutter article as to why you'd want to do such a thing. 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++ and a lot of times people think of these as public. There are cases where you may want to define an interface that

Make java methods visible to only specific classes

无人久伴 提交于 2019-11-28 12:40:30
I have a manager class that is responsible for managing Objects of a certain kind. To do so it needs to manipulate these Objects, but these Objects have no relation to the manager whatsoever, so design technically, they are in separate packages "project.managers" and "project.objects" . The important thing is that the Objects in question should only be manipulated by the managers and nowhere else, but need to be accessible by every other class in the project. As such I'd like to have the managers have access to manipulating methods, but restrict access to every other class. The most obvious

How to prevent derived class from making a private/protected virtual function public?

时间秒杀一切 提交于 2019-11-28 08:01:07
问题 There are good reasons for constructing the base class interface with all virtual functions as private or protected (see this). But then how does one prevent the derived classes (which may be in the hands of external clients) from making the private virtual function as public? In Virtually Yours, the authors talk about this problem, but no solution is discussed. Edit : From your answers and as I previously thought, it seems there is no way to prevent this. But since in this situation, it is

Interfaces in Java: cannot make implemented methods protected or private

若如初见. 提交于 2019-11-28 06:48:44
I know that an interface must be public. However, I don't want that. I want my implemented methods to only be accessible from their own package, so I want my implemented methods to be protected. The problem is I can't make the interface or the implemented methods protected. What is a work around? Is there a design pattern that pertains to this problem? From the Java guide, an abstract class wouldn't do the job either. read this . "The public access specifier indicates that the interface can be used by any class in any package. If you do not specify that the interface is public, your interface

How to set private instance variable used within a method test?

时光毁灭记忆、已成空白 提交于 2019-11-28 06:11:10
Given a class with a couple of instance variables and some methods. Some instance variables are set accessible via attr_reader and attr_accessor . Thus the others are private. Some of the private instance variables get set within one of the instance methods and read/utilized within another method. For testing I'm using RSpec. As I'm still new to Ruby and want to get all things right, I defined my tests being rather fine-grained. Thus I've got one describe block for each instance method, which themselves are partitioned into a subset of context s and it s. General environmental preconditions

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

亡梦爱人 提交于 2019-11-28 05:16:40
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? Julio Marins 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 the default object self is an instance of the same class as the object whose method you're

How to create a private class method?

倾然丶 夕夏残阳落幕 提交于 2019-11-28 02:43:23
How come this approach of creating a private class method works: class Person def self.get_name persons_name end class << self private def persons_name "Sam" end end end puts "Hey, " + Person.get_name puts "Hey, " + Person.persons_name #=> raises "private method `persons_name' called for Person:Class (NoMethodError)" But this does not: class Person def self.get_name persons_name end private def self.persons_name "Sam" end end puts "Hey, " + Person.get_name puts "Hey, " + Person.persons_name tjwallace private doesn't seem to work if you are defining a method on an explicit object (in your case