正如 Kotlin 文档所说,listOf<T>有三种实现:无参数、一个参数和可变参数。每个函数都应该返回 immutable List<T>。
val emptyList = listOf<String>()
val oneArgList = listOf("asd")
val varargsList = listOf("asd", "asd")
if (emptyList is MutableList) println("emptyList is mutable")
if (oneArgList is MutableList) println("oneArgList is mutable")
if (varargsList is MutableList) println("varargList is mutable")
Run Code Online (Sandbox Code Playgroud)
执行上面的代码导致
oneArgList is mutable
varargList is mutable
Run Code Online (Sandbox Code Playgroud)
为什么它会这样工作?这是理想的行为吗?
我刚开始学习Qt,有一些东西不太明白。所以我由创建者制作了小部件应用程序,框架为 MainWindow 创建了头文件和 cpp 文件。
头文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLayout>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
private:
Ui::MainWindow *ui;
QLayout *aLayout;
QLayout *bLayout;
};
#endif // MAINWINDOW_H
Run Code Online (Sandbox Code Playgroud)
.cpp文件
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->...
}
MainWindow::~MainWindow()
{
delete ui;
}
Run Code Online (Sandbox Code Playgroud)
ui我的问题是构造函数创建的对象和this在同一构造函数中使用的对象之间有什么区别。另外,为什么我不能通过ui指针访问 MainWindow 成员?