private

What's the best way to unit test protected & private methods in Ruby?

旧街凉风 提交于 2019-11-30 06:11:59
问题 What's the best way to unit test protected and private methods in Ruby, using the standard Ruby Test::Unit framework? I'm sure somebody will pipe up and dogmatically assert that "you should only unit test public methods; if it needs unit testing, it shouldn't be a protected or private method", but I'm not really interested in debating that. I've got several methods that are protected or private for good and valid reasons, these private/protected methods are moderately complex, and the public

Java reflection get all private fields

此生再无相见时 提交于 2019-11-30 05:55:50
问题 I wonder is there a way to get all private fields of some class in java and their type. For example lets suppose I have a class class SomeClass { private String aaa; private SomeOtherClass bbb; private double ccc; } Now I would like to get all private fields ( aaa , bbb , ccc ) of class SomeClass (Without knowing name of all fields upfront) and check their type. 回答1: It is possible to obtain all fields with the method getDeclaredFields() of Class . Then you have to check the modifier of each

Private Constructor in Python

我的未来我决定 提交于 2019-11-30 05:33:59
How do I create a private constructor which should be called only by the static function of the class and not from else where? mac How do I create a private constructor? In essence, it's impossible both because python does not use constructors the way you may think it does if you come from other OOP languages and because python does not enforce privacy, it just has a specific syntax to suggest that a given method/property should be considered as private. Let me elaborate... First: the closest to a constructor that you can find in python is the __new__ method but this is very very seldom used

C++ why use public, private or protected inheritance?

廉价感情. 提交于 2019-11-30 05:05:44
Well there is enough information about this subject. For example this thread was very clear to me: Difference between private, public, and protected inheritance Except one point; Why is it useful? Use public inheritance to reflect an is-a relationship . This is the main use for inheritance, especially in combination with virtual functions. It allows re-use of interface, not just of old code by new code, but also re-use of new code by old code! (because of virtual function dispatch at runtime). In exceptional circumstances, use private inheritance to reflect an is-implemented-in-terms-of

Dosen't Reflection API break the very purpose of Data encapsulation?

隐身守侯 提交于 2019-11-30 03:40:09
问题 Very recently I came across the Reflection API and to my surprise we can access and even alter the private variables.I tried the following code import java.lang.reflect.Field; public class SomeClass{ private String name = "John"; } public class Test{ public static void main(String args[]) throws Exception { SomeClass myClass = new SomeClass(); Field fs = myClass.getClass().getDeclaredField("name"); fs.setAccessible(true); System.out.println("Variable is " + fs.getName() + " and value is " +

Unit testing private code [duplicate]

爷,独闯天下 提交于 2019-11-30 03:30:12
This question already has an answer here: Unit testing private methods in C# 11 answers I am currently involved in developing with C# - Here is some background: We implement MVP with our client application and we have a cyclomatic rule which states that no method should have a cyclomatic complexity greater than 5. This leads to a lot of small private methods which are generally responsible for one thing. My question is about unit testing a class: Testing the private implementation through the public methods is all fine... I don't have a problem implementing this. But... what about the

What exactly is a Private API, and why will Apple reject an iOS App if one is used?

不羁的心 提交于 2019-11-30 02:38:48
I've read several articles about this, and I just want to see if I understand this correctly: Apple will reject your app if you use a Private API... What is the main difference between a "Private API" and a "Non-private API?" Are the "Non-private" APIs only the APIs provided and verified by Apple? Isn't an API just a way of interacting with a Framework, and a Framework is just a set of encapsulated classes/headers that people can use for trivial purposes? Wouldn't this mean that I cannot reuse anyone's code (other than Apple's) at all in my app? If this is true, whenever a programmer makes his

Are private constants possible in PHP? [duplicate]

孤人 提交于 2019-11-30 02:36:46
This question already has an answer here: Why doesn't PHP permit private const? 2 answers PHP does not allow class Foo { private const my_private_const; but of course allows const my_const; So in effect constants are global because I can access my_const anywhere using Foo::my_const Is there a way to make private constants? The answer is a simple "no". PHP doesn't support this concept. The best you can do is a private static variable in the class, which isn't as good of course because it's not readonly. But you just have to work around it. Edit Your question got me thinking - here's something I

C++ Exceptions and Inheritance from std::exception

三世轮回 提交于 2019-11-30 01:39:19
问题 Given this sample code: #include <iostream> #include <stdexcept> class my_exception_t : std::exception { public: explicit my_exception_t() { } virtual const char* what() const throw() { return "Hello, world!"; } }; int main() { try { throw my_exception_t(); } catch (const std::exception& error) { std::cerr << "Exception: " << error.what() << std::endl; } catch (...) { std::cerr << "Exception: unknown" << std::endl; } return 0; } I get the following output: Exception: unknown Yet simply making

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