我正在使用C# 我有一个功能:
#include "notepadwindow.h"
#include "ui_notepadwindow.h"
#include <QFileDialog>
#include <QComboBox>
NotepadWindow::NotepadWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::NotepadWindow)
{
ui->setupUi(this);
ui->actionBold->setCheckable(true);
ui->actionItalics->setCheckable(true);
ui->actionUnderline->setCheckable(true);
ui->actionLeft->setCheckable(true);
ui->actionRight->setCheckable(true);
ui->actionCenter->setCheckable(true);
ui->actionJustify->setCheckable(true);
this->setCentralWidget(ui->textEdit); // Wyśrodkuj Pole tekstowe
QComboBox* myComboBox = new QComboBox;
ui->mainToolBar->addWidget(myComboBox);
for (int i = 1; i < 102; i += 2) {
myComboBox->addItem(QString::number(i));
}
myComboBox->setCurrentText("11");
ui->textEdit->setFontPointSize(11);
ui->actionLeft->setChecked(true);
ui->actionRight->setChecked(false);
ui->actionCenter->setChecked(false);
ui->actionJustify->setChecked(false);
QObject::connect(myComboBox, SIGNAL(currentTextChanged(QString)), this, SLOT(onFontSizeChanged(QString)));
connect(ui->textEdit, SIGNAL(QTextEdit::cursorPositionChanged()), this, SLOT());
}
NotepadWindow::~NotepadWindow()
{
delete ui;
}
void NotepadWindow::openfile(QString textfile)
{
QFile sFile(textfile);
if(sFile.open(QFile::ReadOnly | QFile::Text)) // Jeśli plik jest otwarty
{
mFilename = textfile;
QTextStream in(&sFile);
in.setCodec("UTF-8"); // Ustaw kodowania znaków na UTF-8
QString text = in.readAll();
sFile.close();
ui->textEdit->setHtml(text);
}
}
void NotepadWindow::onFontSizeChanged(QString selected)
{
ui->textEdit->setFontPointSize(selected.toInt());
}
void NotepadWindow::on_actionUndo_triggered()
{
ui->textEdit->undo();
}
void NotepadWindow::on_actionRedo_triggered()
{
ui->textEdit->redo();
}
void NotepadWindow::on_actionCut_triggered()
{
ui->textEdit->cut();
}
void NotepadWindow::on_actionCopy_triggered()
{
ui->textEdit->copy();
}
void NotepadWindow::on_actionPaste_triggered()
{
ui->textEdit->paste();
}
void NotepadWindow::on_actionNew_triggered()
{
mFilename = "";
ui->textEdit->setPlainText("");
}
void NotepadWindow::on_actionOpen_triggered()
{
QString file = QFileDialog::getOpenFileName(this, "open"); // Otwórz okienko wyboru plików
if(!file.isEmpty()) // Jeśli plik nie jest pusty
{
QFile sFile(file);
if(sFile.open(QFile::ReadOnly | QFile::Text)) // Jeśli plik jest otwarty
{
mFilename = file;
QTextStream in(&sFile);
in.setCodec("UTF-8"); // Ustaw kodowania znaków na UTF-8
QString text = in.readAll();
sFile.close();
ui->textEdit->setHtml(text);
}
}
}
void NotepadWindow::on_actionSave_triggered()
{
QFile sFile(mFilename);
if(sFile.open(QFile::WriteOnly | QFile::Text)) // Jeśli plik jest otwarty
{
QTextStream out(&sFile);
out << ui->textEdit->toHtml();
out.setCodec("UTF-8"); // Ustaw kodowania znaków na UTF-8
sFile.flush();
sFile.close();
}
else if(!sFile.open(QFile::WriteOnly | QFile::Text)) on_actionSave_as_triggered();
}
void NotepadWindow::on_actionSave_as_triggered()
{
QString file = QFileDialog::getSaveFileName(this, tr("Text File"), "", tr("Text files (*.txt)"));
if(!file.isEmpty())
{
mFilename = file;
on_actionSave_triggered();
}
}
void NotepadWindow::on_actionBold_triggered(bool checked)
{
if(checked)
{
ui->textEdit->setFontWeight(QFont::Bold);
}
if(!checked)
{
ui->textEdit->setFontWeight(QFont::Normal);
}
}
void NotepadWindow::on_actionItalics_triggered(bool checked)
{
if(checked)
{
ui->textEdit->setFontItalic(1);
}
if(!checked)
{
ui->textEdit->setFontItalic(0);
}
}
void NotepadWindow::on_actionUnderline_triggered(bool checked)
{
if(checked)
{
ui->textEdit->setFontUnderline(1);
}
if(!checked)
{
ui->textEdit->setFontUnderline(0);
}
}
void NotepadWindow::on_actionLeft_triggered()
{
ui->textEdit->setAlignment(Qt::AlignLeft);
ui->actionLeft->setChecked(true);
ui->actionRight->setChecked(false);
ui->actionCenter->setChecked(false);
ui->actionJustify->setChecked(false);
}
void NotepadWindow::on_actionCenter_triggered()
{
ui->textEdit->setAlignment(Qt::AlignCenter);
ui->actionLeft->setChecked(false);
ui->actionRight->setChecked(false);
ui->actionCenter->setChecked(true);
ui->actionJustify->setChecked(false);
}
void NotepadWindow::on_actionRight_triggered()
{
ui->textEdit->setAlignment(Qt::AlignRight);
ui->actionLeft->setChecked(false);
ui->actionRight->setChecked(true);
ui->actionCenter->setChecked(false);
ui->actionJustify->setChecked(false);
}
void NotepadWindow::on_actionJustify_triggered()
{
ui->textEdit->setAlignment(Qt::AlignJustify);
ui->actionLeft->setChecked(false);
ui->actionRight->setChecked(false);
ui->actionCenter->setChecked(false);
ui->actionJustify->setChecked(true);
}
我把public double[] PerformBeeDance(double[] vector, double r) {
int index = rand.Next(0, vector.Length);
double[] result = vector;
result[index] = vector[index] + rand.NextDouble() * r;
return result;
}
放到这个函数中并在调用之后:
bees[i].Position
newbee.Position = PerformBeeDance(bees[i].Position, r);
正在改变bees[i].Position
但它应该保持不变。
此代码有什么问题?
答案 0 :(得分:5)
double[]
是一个ReferenceType,所以虽然它是作为副本传递的,但副本只是引用,它们都引用同一个实例。
修改强>
可能的解决方案正如@RaymondChen在评论中指出的那样,是Array.Clone
:
public double[] PerformBeeDance(double[] vector, double r)
{
int index = rand.Next(0, vector.Length);
double[] result = vector.Clone();
result[index] = vector[index] + rand.NextDouble() * r;
return result;
}
或使用来电代码制作副本:
newbee.Position = PerformBeeDance(bees[i].Position.Clone(), r);