小编cat*_*lla的帖子

什么是拒绝遗赠?

有人可以解释一下Refused Bequest的含义吗?我尝试阅读一些文章,并说它是一种代码气味,或者在wiki中它告诉它它是一个类,它覆盖基类的方法,使得基类的契约不受派生类的尊重.

但简而言之,或者更简单的说法,实际上是什么?

duck-typing liskov-substitution-principle solid-principles

11
推荐指数
1
解决办法
3904
查看次数

如何使用书签将 HTML 转换为 PDF

我正在尝试将自定义的 html 文件另存为 pdf..通常我会在浏览器(chrome)上按 ctrl-P 并打印为 pdf..

将 html 保存为 pdf

但是当我打开pdf文件时,pdf阅读器(adobe)左侧没有书签选项卡。

没有书签的pdf

我想要的是将 html 文件另存为 pdf,并且书签应出现在 pdf 阅读器的左侧:

在此输入图像描述

我创建了 html 文件。我使用id超链接添加了指向其中某些部分的链接:

<a href="#part1">part1</a>
...some codes here...
<div id="part1">
Run Code Online (Sandbox Code Playgroud)

它有效,但我不知道如何从 html 在 pdf 中创建书签...通常 MS Word 或 Libre Office 可以使用书签将其文档转换为 pdf..

但是如何使用 HTML 制作带有书签的 pdf 呢?

html pdf

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

在php中记住斐波纳契函数

我已经创建了斐波那契递归版的memoized函数.我将此作为使用memoization的其他类型函数的示例.我的实现很糟糕,因为如果我将它包含在库中,这意味着global仍然可以看到变量..

这是原始的递归斐波那契函数:

function fibonacci($n) {
  if($n > 1) {
    return fibonacci($n-1) + fibonacci($n-2);
  }
  return $n;
}
Run Code Online (Sandbox Code Playgroud)

我把它修改为一个memoized版本:

$memo = array();
function fibonacciMemo($n) {
  global $memo;
  if(array_key_exists($n, $memo)) {
    return $memo[$n];
  }
  else {
    if($n > 1) {
      $result = fibonacciMemo($n-1) + fibonacciMemo($n-2);
      $memo[$n] = $result;
      return $result;
    }
    return $n;
  }
}
Run Code Online (Sandbox Code Playgroud)

我故意没有使用迭代方法来实现斐波那契.有没有更好的方法来记住php中的fibonacci函数?你能建议我更好的改进吗?我见过func_get_args()call_user_func_array的另一种方式,但我似乎无法知道什么是好?

所以我的主要问题是:我如何正确地在php中记住fibonacci函数?或者在php中记忆fibonacci函数的最佳方法是什么?

php recursion memoization fibonacci

4
推荐指数
1
解决办法
2127
查看次数

当在子例程中传递时,"=〜"和"!〜"运算符在Perl中进行奇数哈希计数

我正在创建一个子例程,它接受具有不同值的相同键的哈希数组.作为要求,存在具有条件操作的值.

样品:

use strict;
use warnings;
use Data::Dumper;

sub test {
  my @data = @_;
  print Dumper(@data);
}

test(
  {
    'value' => 1 == 2
  },
  {
    'value2' => 4 == 4
  }
);
Run Code Online (Sandbox Code Playgroud)

输出:

$VAR1 = {
          'value' => ''
        };
$VAR2 = {
          'value2' => 1
        };
Run Code Online (Sandbox Code Playgroud)

但是当我使用=~!~运算符时,解释器输出此错误:

Odd number of elements in anonymous hash at ...

test(
  {
    'value' => 1 == 2
  },
  {
    'value2' => 'a' =~ /b/
  }
);
Run Code Online (Sandbox Code Playgroud)

输出: …

perl

3
推荐指数
1
解决办法
81
查看次数

单元测试带有打印到屏幕的参数的Perl函数

如何使用打印到屏幕的参数对perl函数进行单元测试?

sub test {
  my $var = shift;
  if( $var eq "hello") {
    print "Hello";
  }
  else {
    print "World";
  }
} 
Run Code Online (Sandbox Code Playgroud)

我想完全覆盖打印到屏幕的功能中的所有条件,但我不知道如何...

我在stackoverflow上看到了这个答案如何单元测试打印到屏幕的Perl函数?

是的答案可以单元测试一个输出字符串的函数,但只有在所需的函数中没有参数时...在我的情况下我可以使用:

stdout_is(\&test, "World", 'test() return World');
Run Code Online (Sandbox Code Playgroud)

但我怎么测试print "Hello";

编辑

我尝试使用这个测试用例:

should_print_hello();
should_print_world();


sub should_print_world
{
  stdout_is(\&test, "World", "should_print_world");
}

sub should_print_hello
{
  # this does not work and outputs error
  stdout_is(\&test("hello"), "Hello", "should_print_hello");
}
Run Code Online (Sandbox Code Playgroud)

因为stdout_is'函数的参数只是对函数的代码引用(如果我没有弄错),它没有function(variables_here).

我还阅读了perl Test :: Output手册,但我仍然无法找到解决方案..有没有其他方法或者我错过了什么?

所以我的主要问题是: 我如何单独测试只打印到屏幕(stdout)的perl函数(带参数)?

perl unit-testing

2
推荐指数
1
解决办法
357
查看次数