这可能听起来很愚蠢,但是当你有(键,值)对的对象并且你根据键对它们进行排序时它是有意义的.用代码说明我的观点:
public class Pair implements Comparable<Pair> {
private int value;
private int key;
public Pair(int key, int value) {
this.key = key;
this.value = value;
}
@Override
public int compareTo(Pair o) {
if (this.key > o.key)
return 1;
else if (this.key < o.key)
return -1;
return 0;
}
}
public class program {
public static void main(String[] args) {
PriorityQueue<Pair> queue = new PriorityQueue<Pair>;
queue.add(new Pair(1,1));
queue.add(new Pair(1,2));
queue.add(new Pair(1,3));
Pair pair = queue.poll(); // What would be in pair? …Run Code Online (Sandbox Code Playgroud) 我正在使用一个成员变量,并且在程序的某个时刻我想要更改它,但我更喜欢在其他地方"锁定"以防止意外更改.
代码解释:
class myClass {
int x; // This should be prevented to being changed most of the time
int y; // Regular variable
myclass() {x = 1;}
void foo1 () {x++; y++;} // This can change x
void foo2 () {x--; y--;} // This shouldn't be able to change x
// I want it to throw a compile error
};
Run Code Online (Sandbox Code Playgroud)
问题是:能以某种方式实现吗?像永久const_cast这样的东西?
我知道我可以立即使用构造函数初始化列表和常量,但我需要稍后更改我的变量.
我已经在网上搜索了几个小时,但找不到解决方法。我已经能够使用以下命令创建自签名CA证书:
openssl genrsa -out ca.key 1024
openssl req -new -x509 -extensions v3_ca -key ca.key -out ca.crt -days 3650
Run Code Online (Sandbox Code Playgroud)
现在,我想创建新证书并用我的CA对其进行签名。在新证书中,我想拥有自己的扩展名-我们可以将其称为“ abc”,以使其具有整数值“ 1”。我尝试了以下命令:
openssl req -new -nodes -newkey rsa -extensions abc -keyout mycert.key -out mycsr.csr -days 365 -config ./openssl.cnf
Run Code Online (Sandbox Code Playgroud)
使用openssl.cnf仅包括以下内容的文件时:
[ abc ]
abc = ASN1:INTEGER:1
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
Error Loading extension section abc
3073632456:error:0D06407A:asn1 encoding routines:a2d_ASN1_OBJECT:first num too large:a_object.c:109:
3073632456:error:22074073:X509 V3 routines:V3_GENERIC_EXTENSION:extension name error:v3_conf.c:271:name=abc
Run Code Online (Sandbox Code Playgroud)
我在SO上找到了这个模糊相关的topis,但它根本没有帮助...
似乎是什么问题?我该如何纠正自己?任何想法表示赞赏。
我正在尝试测试用 Cobra 编写的 CLI 应用程序,特别是测试子命令是否正确写入 STDOUT。为此,我尝试将 STDOUT 的输出重定向到我的缓冲区。不幸的是,无论出于何种原因,SetOut() 函数在通过调用 Commands() 获得的子命令上的行为并不如预期。
如何在 Cobra 中的子命令上正确调用 SetOut()?
这是我的代码:
package cmd
import (
"os"
"testing"
"bytes"
"io/ioutil"
"github.com/spf13/cobra"
)
func NewCmd() *cobra.Command {
cmd := &cobra.Command{}
cmd.AddCommand(NewChildCmd())
return cmd
}
func NewChildCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "child",
Run: func(cmd *cobra.Command, args []string) {
os.Stdout.WriteString("TEST\n")
},
}
return cmd
}
func TestChild(t *testing.T) {
cmd := NewCmd()
buffer := new(bytes.Buffer)
subCommands := cmd.Commands()
for i := range subCommands {
subCommands[i].SetOut(buffer)
}
cmd.SetOut(buffer) …Run Code Online (Sandbox Code Playgroud)