我给了一个有私有方法的类,比如setCoors(int x,int y).该类的构造函数也包含setCoors.在另一个类中,我想要一个调用setCoors的方法setLocation.这可能吗?
新问题:
如果我不被允许将方法设置为公开,这可能吗?
public class Coordinate{
public Coordinate(int a, int b){
setCoors(a,b)
}
private void setCoords(int x, int y)
}
public class Location{
private Coordinate loc;
public void setLocation(int a, int b)
loc = new Coordinate(a,b)
}
Run Code Online (Sandbox Code Playgroud) 我想测试一个私有内部类中存在的私有方法
public class MyBigClass {
private class MyInnerClass {
private void wantedMethod() {
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想打电话wantedMethod()
来测试它
这是我的代码
Class[] classes = MyBigClass.class.getDeclaredClasses();
for (int i = 0; i < classes.length; i++) {
// this code print "MyInnerClass"
System.out.println(">> inner classes >> " + classes[i].getSimpleName());
if (classes[i].getSimpleName().equals("MyInnerClass")) {
Class clazz = classes[i];
// Constructor c=clazz.getConstructor();
Method[] methods = clazz.getDeclaredMethods();
// this code print "wantedMethod"
for (int j = 0; j < methods.length; j++) {
System.out.println("inner class methods >> " …
Run Code Online (Sandbox Code Playgroud) 注意到PHP的类,我不知道它是一个错误或它为什么工作,这是代码:
<?php
class A {
private $prop = 'value';
public function fun()
{
$obj = new A;
$obj->echoProp();
}
private function echoProp()
{
echo 'Prop has value: '.$this->prop;
}
}
$obj = new A;
$obj->fun();
Run Code Online (Sandbox Code Playgroud)
结果不是我所期待的错误,因为我正在调用一个私有方法(在PHP 5.3.10-1ubuntu3.7上用Suhosin-Patch测试).结果是"道具有价值:价值"
每当我定义这样的私有方法时:
class ParentClass {
sayHi() { _initiateGreeting(); }
_initiateGreeting() { print("I'm a parent class"); }
}
Run Code Online (Sandbox Code Playgroud)
不可能在子类中重新定义它:
class ChildClass extends ParentClass {
_initiateGreeting() { print("I'm a child class"); } // redefining the method
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我创建了一个 aChildClass
和 callsayHi()
方法的实例,那么ParentClass
实际调用的是 a 中的私有方法:
var child = new ChildClass();
child.sayHi(); // outputs "I'm a parent class"
Run Code Online (Sandbox Code Playgroud)
但是,如果_initiateGreeting()
在任何地方都公开(即剥离了_
),则child.sayHi()
在输出中调用results I'm a child class
。
为什么?如何重新定义私有方法以便在子类中调用它?
我在控制器中有一个私有方法
private
def body_builder
review_queue = ReviewQueueApplication.where(id: params[:review_queue_id]).first
...
...
end
Run Code Online (Sandbox Code Playgroud)
我只想测试该body_builder
方法,它是一种为 rest 客户端 api 调用构建有效负载的方法。但是,它需要访问参数。
describe ReviewQueueApplicationsController, type: :controller do
describe "when calling the post_review action" do
it "should have the correct payload setup" do
@review_queue_application = ReviewQueueApplication.create!(application_id: 1)
params = ActionController::Parameters.new({ review_queue_id: @review_queue_application.id })
expect(controller.send(:body_builder)).to eq(nil)
end
end
end
Run Code Online (Sandbox Code Playgroud)
如果我运行上面的它会发送body_builder
方法,但它会中断,因为参数没有像在调用操作时那样正确设置。
我总是可以为该body_builder
方法创建一个条件参数,以便它接受一个参数,或者它将使用这样的参数def body_builder(review_queue_id = params[:review_queue_id])
然后在测试中controller.send(:body_builder, params)
,但我觉得更改代码以使测试通过是错误的,它应该只是测试它照原样。
在将私有方法发送给控制器之前,如何将参数输入控制器?
我写了一个简单的代码:
const secure = new class {
#privateProperty = 4;
#privateMethod() {
console.log( 'The property ' + this.#privateProperty + ' should not be accessible outside this class' );
}
}
Run Code Online (Sandbox Code Playgroud)
如果紧接着下面的语法
secure.#privateMethod();
我收到一条错误消息Uncaught SyntaxError: Private field '#privateMethod' must be declared in an enclosing class
但是,如果我不立即调用secure.#privateMethod()
,然后转到开发人员工具 - 控制台并在那里编写语法,它会输出:
The property 4 should not be accessible outside this class
发生这种情况有什么特殊原因吗?
javascript class private-members private-methods google-chrome-devtools
我试图在我的twitter feed控制器中调用tweet.entities时出现以下错误.
私有方法`entities'被称为#Twitter :: Status:0x94d4bf4
我在代码中没有称为实体的方法,我甚至做了一个完整的文件搜索来检查.
我要么需要重命名对象的实体部分,要么以某种方式找到这个所谓的私有方法的位置或以某种方式环绕它.我的twitter_feeds控制器中的方法如下:
def hometimeline
@user = User.find(current_user.id)
tweets = @user.tweeting.user_timeline(count: '10', include_entities: true)
parsed_tweets = []
i = 0
tweets.each do |tweet|
more = Hash.new
more['test'] = tweet
internal_hash = Hash.new
mappings = {"source" => "fixed"}
another = more['test']
boo = Array(tweet)
#newhash = Hash[twee.map {|k, v| [mapping[k], v] }]
#newhash = Hash[more.map {|k, v| [mappings[k], v] }]
#newhash = Hash[tweet.map {|k, v| [mappings[k] || k, v] }]
internal_hash['parsed_text'] = tweet.entities
internal_hash['id'] = tweet.id
internal_hash['raw_text'] …
Run Code Online (Sandbox Code Playgroud) 对于会员,我使用
//.......vv
SomeType m_XXX;
//.......^^
Run Code Online (Sandbox Code Playgroud)
我喜欢_
用作成员函数的前缀,但名称以_
或__
以保留开头且不应使用.
我的想法是,当我有:
SomeClass myObject;
myObject.[XXX]
Run Code Online (Sandbox Code Playgroud)
当用户(lib)写入dot(.
)时,只能查看所有函数(一个接一个)public
.
这有一个共同的命名约定吗?
我知道,我可以使用pImpl
或继承接口和实现类
我正在使用类和CoffeeScript/JavaScript进行一系列测试.请参阅以下代码:
class Example
someFunction = ->
alert @getText()
constructor: ->
@text = 'Hello world! ;)'
someFunction()
getText: ->
@text
### Instance ###
example = new Example
Run Code Online (Sandbox Code Playgroud)
这只是一个例子,编译时我得到错误:
Uncaught TypeError: Object [object global] has no method 'getText'
Run Code Online (Sandbox Code Playgroud)
你知道我怎么能解决这个问题吗? http://jsfiddle.net/P4Xdz/
我试图模拟一个私有方法并验证它已被执行.
模拟本身工作正常,即无论我评论/取消注释"何时"行,我都会得到正确的结果.但是,我在验证时遇到了麻烦.
@RunWith(PowerMockRunner.class)
@PrepareForTest(ClassToTest.class)
public class TestClass {
@Test
public void test() throws Exception {
ClassToTest spy = PowerMockito.spy(new ClassToTest());
when(spy, method(ClassToTest.class, "privMethod", int.class)).withArguments(anyInt()).thenReturn(20);
System.out.println(spy.pubMethod());
PowerMockito.verifyPrivate(spy, times(1)).invoke("privMethod", int.class);
}
}
class ClassToTest {
public int pubMethod() {
return privMethod(231);
}
private int privMethod(int whatever) {
return 10;
}
}
Run Code Online (Sandbox Code Playgroud)
基于https://code.google.com/p/powermock/wiki/MockitoUsage13创建此示例 这是我得到的,任何想法为什么?
java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:1873)
at org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:773)
at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:638)
at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:401)
at org.powermock.api.mockito.internal.verification.DefaultPrivateMethodVerification.invoke(DefaultPrivateMethodVerification.java:39)
at test.TestClass.test(TestClass.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native …
Run Code Online (Sandbox Code Playgroud) private-methods ×10
java ×3
class ×2
javascript ×2
c++ ×1
coffeescript ×1
controller ×1
dart ×1
inheritance ×1
junit ×1
methods ×1
oop ×1
php ×1
powermock ×1
private ×1
reflection ×1
rspec ×1
twitter ×1