我正在写一个游戏,现在我能够通过类及其方法实现文件系统sqlite.为了让生活更容易我已经打算写像一些功能fopen,fclose,fread,rename,等能够影子基础功能,并直接把我的给我的文件系统调用,而不是原来的一个.对于前三个功能,一切都适合我使用这些原型:
File *fopen(String _Filename, String _Mode); // i have my own optimized File struct
void fclose(File *_File);
size_t fread(String *_DstBuf, size_t _ElementSize, size_t _Count, File *_File);
这工作正常,因为我要么返回另一个结构或参数除了a File*而不是a FILE*,但重命名函数似乎有点棘手!
int rename(String _OldFilename, String _NewFilename);
这几乎是相同的原型.除了我使用std::string(typedef'ed String)比const char*!知道我怎么能说服我的编译器使用我的函数或忽略stdio-one?
GHCi告诉我,类型A不是类型A.为什么?
>>> data A = A
>>> let x = A
>>> let id A = A
>>>
>>> data A = A
>>> let x' = A
>>> let id' A = A
>>>
>>> data A = A
>>>
>>> let y = id' x
<interactive>:18:13:
Couldn't match expected type `main::Interactive.A'
with actual type `main::Interactive.A'
In the first argument of id', namely `x'
In the expression: id' x
In an equation for `y': y …Run Code Online (Sandbox Code Playgroud) 我试图制作更改其他按钮文本的按钮,但是settext不起作用.
这是appdroid.java:
包appdroid;
public class appdroid{
static int a = 640;
static int b = 400;
static int c = 100;
static int d = 100;
public static void main(String[] args) {
new gui();
}
}
Run Code Online (Sandbox Code Playgroud)
这是gui.java:
package appdroid;
import javax.swing.AbstractButton;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class gui extends JFrame implements ActionListener {
String[] a = {"Search device","Device not found","Error, bad device","Device found"};
String[] b = {"Device not found","Error, bad device","Compile"};
String[] c = …Run Code Online (Sandbox Code Playgroud) 这段代码有点问题.该actionPerformed方法不起作用.按钮knappStartSalg和knappStartKunde,当我按动按钮没有反应.
应该导入的所有内容都是导入的.
非常感谢任何帮助.
开始上课.
public class Startmeny extends JFrame implements ActionListener
{
public JButton knappStartSalg, knappStartKunde, knappStartInfo, knappStartStatistikk;
public JPanel startmeny()
{
JPanel startpanel = new JPanel();
startpanel.setLayout(new GridLayout(2, 0, 25, 25) );
startpanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
startpanel.setBackground(Color.white);
JButton knappStartSalg = new JButton();
knappStartSalg.setText("Salg");
knappStartSalg.setVerticalTextPosition(JButton.BOTTOM);
knappStartSalg.setHorizontalTextPosition(JButton.CENTER);
knappStartSalg.setIcon(new javax.swing.ImageIcon(getClass().getResource("salg.png")));
knappStartSalg.setIconTextGap(6);
knappStartSalg.setForeground(Color.black);
knappStartSalg.setBackground(Color.white);
knappStartSalg.setBorderPainted(false);
knappStartSalg.addActionListener(this);
startpanel.add(knappStartSalg);
JButton knappStartKunde = new JButton();
knappStartKunde.setText("Kontroll");
knappStartKunde.setVerticalTextPosition(JButton.BOTTOM);
knappStartKunde.setHorizontalTextPosition(JButton.CENTER);
knappStartKunde.setIcon(new javax.swing.ImageIcon(getClass().getResource("heiskontroll.png")));
knappStartKunde.setIconTextGap(6);
knappStartKunde.setForeground(Color.black);
knappStartKunde.setBackground(Color.white);
knappStartKunde.setBorderPainted(false);
knappStartKunde.addActionListener(this);
startpanel.add(knappStartKunde);
JButton knappStartInfo = new JButton();
knappStartInfo.setText("Informasjonsvindu");
knappStartInfo.setVerticalTextPosition(JButton.BOTTOM); …Run Code Online (Sandbox Code Playgroud) 我正在尝试自己学习java,并希望创建一个安全的文本编辑器,您必须登录才能访问该文本.但是,动作监听器不适用于任何按钮,我无法弄清楚出了什么问题.
请注意我只制作了两个按钮,因为第一个按钮不起作用.
我的代码如下:
public class storage extends JFrame{
private JTextField text1;
public JTextArea storearea;
public JButton newsave;
public JButton save;
public storage(UserProfile person) { //constructor with name passed in
super("Safe Storage");
setLayout(new FlowLayout());
JTextField text1 = new JTextField("Using program as: " + (person.getName()) );
text1.setEditable(false);
add(text1);
JTextArea storearea = new JTextArea(person.getText());
add(storearea);
JButton newsave = new JButton("Save");
add(newsave);
JButton save = new JButton("Save Changes");
add(save);
thehandler handler = new thehandler();
save.addActionListener(handler);
newsave.addActionListener(handler);
} //end constructor
public class thehandler implements ActionListener …Run Code Online (Sandbox Code Playgroud) 我正在尝试运行此代码:
class A {
int x = 123;
public void f(int x) {
new Runnable() {
public void run() {
System.out.println(x);
}
}.run();
}
static {
A a = new A();
a.f(33);
}
}
Run Code Online (Sandbox Code Playgroud)
但它给了我一个错误:
$ javac A.java && java A
A.java:6: local variable x is accessed from within inner class; needs to be declared final
System.out.println(x);
^
1 error
Run Code Online (Sandbox Code Playgroud)
该x参数不是final,所以它不应该是从匿名类访问,但代码编译失败.看起来该println行试图使用x参数而不是x字段.为什么?我怎么能告诉它我想要这个x领域?
我已经读过"如果另一个具有相同名称的变量在范围上更近,则变量被遮蔽".我发现这个Point类以构造函数为例:
public class Point {
public int x = 0;
public int y = 0;
public Point(int x, int y) {
x = x;
y = y;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我在下面的CreateObjectDemo类中创建了Point类的对象,并打印了变量x的值.
public class CreateObjectDemo {
public static void main(String[] args) {
Point originOne = new Point(23, 94);
System.out.println(originOne.x);
}
}
Run Code Online (Sandbox Code Playgroud)
运行编译器后,它打印0.但为什么不打印23?我认为构造函数中的"x = x"就像"23 = 23".我是否误解了阴影变量的定义?
我的教授今天向我们展示了这段代码,但我似乎无法理解它的结果:
# let a = 2;;
val a : int = 2
# let f = fun x -> fun y -> if x = y then a + 2 else a - 10;;
val : f 'a -> 'a -> int = <fun>
# f 1 (2 - 1);;
- : int = 4
# let a = 18;;
val a : int = 18
# f 1 (2 - 1);;
- : int = 4
Run Code Online (Sandbox Code Playgroud)
??? 所以基本上,我希望看到这个结果:
- …Run Code Online (Sandbox Code Playgroud) Rust 中的零运行时成本混合列表概述了如何在 Rust 中使用元组和正常特征(不是这个问题建议的特征对象)创建异构列表。该列表似乎在很大程度上依赖于阴影,并且每次添加新元素时都会有效地更改列表的整个类型。
这个实现对我来说似乎很棒,但是在查看了一些 Rust 的主页和资源后,我找不到任何明确定义阴影为零成本的地方。据我所知,重复放弃堆栈上的数据比间接的成本低,但重复复制和添加到现有数据而不是改变它听起来很昂贵。
你不用的东西,你不用付钱。更进一步:你所使用的,你不能更好地编写代码。
- 比亚恩·斯特劳斯楚普
阴影似乎满足了第一个要求,但第二个呢?
Rust 的影子实际上是零成本的吗?
我正在使用 Rust 的会话类型,并且我有一个非常简单的函数,可以执行以下操作:
fn srv(c: Chan<(), Server>){
let (c, n) = c.recv();
let tmp = false;
if n % 2 == 0 {
let tmp = true;
} else {
let tmp = false;
}
let c = c.send(tmp);
let (c, s) = c.recv();
println!("server side: {}", s);
c.close();
}
Run Code Online (Sandbox Code Playgroud)
当我尝试编译它时,rust 编译器认为该tmp变量未被使用。这对我来说似乎很奇怪,因为我将它传递给函数recv。
fn srv(c: Chan<(), Server>){
let (c, n) = c.recv();
let tmp = false;
if n % 2 == 0 {
let tmp = true; …Run Code Online (Sandbox Code Playgroud) 我在范围内进行了变量b(范围外的声明)的类型转换,并为b赋予了新的val,当范围结束时,b的val似乎是错误的。
这是在我的Macbook上发生的,它的gcc版本是gcc-8(Homebrew GCC 8.3.0)8.3.0。我在gcc版本为5.4.0的linux笔记本电脑上尝试了相同的代码,并且代码运行良好。
std::vector<int> a = {1,2,3,4};
int b;
{
size_t i = 0, b = a[i];
//this time type of b is size_t
++i;
b = a[i];
}
std::cout << "b = " << b << std::endl;
Run Code Online (Sandbox Code Playgroud)
在我的Mac上,结果是b = 0
在Ubuntu 16上,结果是b = 1
类型转换的两个版本的gcc有什么区别?
还是一个错误?