标签: private-functions

如何在Perl模块中创建私有函数?

我正在研究一个小的Perl模块,由于某种原因,我有一个测试驱动程序脚本,它使用我的新模块调用我认为是私有的函数之一,并且它是成功的.我很惊讶,所以我开始搜索谷歌,我真的找不到任何关于如何在Perl模块中创建私有函数的文档...

我看到一个地方说要在你的"私人"功能的右大括号之后加一个分号,如下所示:

sub my_private_function {
...
}; 
Run Code Online (Sandbox Code Playgroud)

我尝试过,但我的驱动程序脚本仍然可以访问我想要私有的功能.

我会做一些简短的例子,但这就是我所追求的:

模块TestPrivate.pm:

package TestPrivate;

require 5.004;

use strict;
use warnings;
use Carp;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);

require Exporter;

@ISA = qw(Exporter AutoLoader);

our @EXPORT_OK = qw( public_function );
our @EXPORT    = qw( );

$VERSION = '0.01';

sub new {
    my ( $class, %args ) = @_;
    my $self = {};
    bless( $self, $class );
    $self->private_function("THIS SHOULD BE PRIVATE");
    $self->{public_variable} = "This is public";
    return $self;
}

sub public_function {
    my …
Run Code Online (Sandbox Code Playgroud)

perl module perl-module private-functions

32
推荐指数
5
解决办法
3万
查看次数

Kotlin中如何从类外部调用类的私有函数

我想SomeClass从该类的外部调用该类的私有函数:

class SomeClass {
    private fun somePrivateFunction() {
        //...
    }

    private fun somePrivateFunctionWithParams(text: String) {
        //...
    }
}
Run Code Online (Sandbox Code Playgroud)

在代码中的某个地方我引用了SomeClass对象:

val someClass = SomeClass()
// how can I call the private function `somePrivateFunction()` from here?
// how can I call the private function `somePrivateFunctionWithParams("some text")` from? here
Run Code Online (Sandbox Code Playgroud)

如何从类外部调用 Kotlin 中带参数和不带参数的私有函数?

reflection private private-functions private-methods kotlin

8
推荐指数
2
解决办法
2万
查看次数

使用私有框架:导入RadioPreferences.h

我正在关注在Xcode中导入私有框架的选定答案

基本上我希望能够在我的应用程序中控制飞行模式.当我导入RadioPreferences.h到我的应用程序,并尝试编译,我得到Expected Identifier@class <RadiosPreferencesDelegate>;

我不确定接下来要做什么.我甚至不知道你可以转发声明协议.

iphone objective-c private-functions ios

6
推荐指数
1
解决办法
5776
查看次数

JavaScript中的私有函数

在基于jQuery的Web应用程序中,我有各种脚本,其中可能包含多个文件,我一次只使用其中一个(我知道不包括所有这些都会更好,但我只负责JS所以这不是我的决定).所以我将每个文件包装在一个函数中,该函数注册各种事件并进行一些初始化等.initModule()

现在我很好奇以下两种定义函数之间是否存在任何差异,而不是混淆全局命名空间:

function initStuff(someArg) {
    var someVar = 123;
    var anotherVar = 456;

    var somePrivateFunc = function() {
        /* ... */
    }

    var anotherPrivateFunc = function() {
        /* ... */
    }

    /* do some stuff here */
}
Run Code Online (Sandbox Code Playgroud)

function initStuff(someArg) {
    var someVar = 123;
    var anotherVar = 456;

    function somePrivateFunc() {
        /* ... */
    }

    function anotherPrivateFunc() {
        /* ... */
    }

    /* do some stuff here */
}
Run Code Online (Sandbox Code Playgroud)

javascript scope private-functions

6
推荐指数
1
解决办法
1554
查看次数

EUnit未能测试私人功能

我正在为Erlang代码编写EUnit测试.

我有一个源模块:

-module(prob_list).
-export([intersection/2,union/2]).

probability([], _Item) -> false;
probability([{First,Probability}|Rest], Item) ->
    if
        First == Item -> Probability;
        true          -> probability(Rest, Item)
    end.
...
...
...
Run Code Online (Sandbox Code Playgroud)

和单元测试模块:

-module(prob_list_tests).
-include_lib("eunit/include/eunit.hrl").

-define(TEST_LIST,[{3,0.2},{4,0.6},{5,1.0},{6,0.5}]).
-define(TEST_LIST1,[{2,0.9},{3,0.6},{6,0.1},{8,0.5}]).
-define(TEST_UNO_LIST,[{2,0.5}]).

probability_test() -> ?assertNot(prob_list:probability([],3)),
                      ?assertEqual(0.5,prob_list:probability(?TEST_UNO_LIST,2)),
                      ?assertNot(prob_list:probability(?TEST_UNO_LIST,3)),
                      ?assertEqual(0.2,prob_list:probability(?TEST_LIST,3)),
                      ?assertEqual(1.0,prob_list:probability(?TEST_LIST,5)),
                      ?assertNot(prob_list:probability(?TEST_LIST,7)).
...
...
...
Run Code Online (Sandbox Code Playgroud)

我跑的 eunit:test(prob_list,[verbose])时候说:

 prob_list_tests: probability_test...*failed*
::undef
Run Code Online (Sandbox Code Playgroud)

但是当我probability/2在我的prob_list模块中导出时,一切都很好.

有没有办法测试私人功能?

erlang unit-testing private-functions eunit

6
推荐指数
3
解决办法
1146
查看次数

在mathematica包中定义私有函数

我不确定我是如何正确定义私人职能的.当我写一个包mathematica时,我就是这样做的:

BeginPackage["myPackage`"]
myPublicFunction::usage="myPublicFunction blahblahblah";
Begin["Private"]
myPrivateFunction[input_]:= ... ;
myPublicFunction[input_]:= ... ;
End[]
EndPackage[]
Run Code Online (Sandbox Code Playgroud)

这是正确的方式还是我错过了什么?

wolfram-mathematica package private-functions

5
推荐指数
1
解决办法
1770
查看次数