我正在看一些看起来像的代码
this.f.call(this);
Run Code Online (Sandbox Code Playgroud)
或者在其他一些情况下
this.someObj.f.call(this.someObj);
Run Code Online (Sandbox Code Playgroud)
这些和之间有什么区别吗?
this.f();
this.someObj.f();
Run Code Online (Sandbox Code Playgroud)
在任何情况下行为会有所不同吗?(例如,行为是不同的,如果this或者someObj为null或者实际上不是一个对象,或者f实际上不是一个函数?我想不出一种方法会抛出一个异常而另一种不会,但是也许我错过了什么...)
编辑:澄清:是的,我知道.call可以用来指定this函数看到的值,它可以在你不能使用obj.f()语法的情况下很有用(因为f它不是obj你的属性,或者你不是知道是否是).我的问题不是.call一般的工作方式.我的问题是关于这种情况,我看不出明显的使用原因.call而不是对象属性语法.
这有效:
stringstream temp;
temp << i;
result_stream << transform(temp.str());
Run Code Online (Sandbox Code Playgroud)
(transform是一个带a string和返回a 的函数string; i是一个int).但是,我尝试让C++ 11创建一个没有名称的临时对象不起作用:
result_stream << transform((stringstream() << i).str());
Run Code Online (Sandbox Code Playgroud)
我认为它会起作用,因为第二个<<应该只返回第一个参数,我可以使用str()它.但我得到这个错误:
error: 'class std::basic_ostream<char>' has no member named 'str'
Run Code Online (Sandbox Code Playgroud)
我正在使用g ++ 4.8.1(MinGW-W64).
有没有办法实现这一点(即使用未命名的临时代码编写这样的代码)?(上面的代码有点简化,实际代码涉及使用<<除int.之外的参数.)
我有一个循环,直到用户单击一个就绪按钮,然后它启动if语句中的循环,但它只能在它之前有一个print语句.它与规则集是如何静态有关吗?Button无论如何都可以工作,但只有print语句存在才能进入循环.
package gameoflife;
public class GameOfLife {
public static final int HEIGHT = 16;
public static final int LENGTH = 16;
public static Grid current;
public static void main(String[] args) {
Ui gui = new Ui();
int time = 0;
while (true) {
RuleSet.checkReady();
//System.out.println(RuleSet.checkReady());
if (RuleSet.checkReady() == true) {
//System.out.println("ready!");
if(time == 0){
current = gui.getUserSeed();
}
while (time < 100) {
current.print();
Grid next = new Grid(HEIGHT, LENGTH);
for (int i = 0; i < HEIGHT; i++) { …Run Code Online (Sandbox Code Playgroud) 我正在尝试学习使用Stream API的细节,我给自己的一个任务是尝试编写一个无限的方法DoubleStream并尝试计算总和(假设它收敛).也就是说,我想写一个方法
public static double infiniteSum(DoubleStream ds) { ... }
Run Code Online (Sandbox Code Playgroud)
我可以用类似的东西打电话
double sum = infiniteSum(IntStream.iterate(1, (i -> i + 1))
.mapToDouble(n -> 1 / ((double)n * n)));
Run Code Online (Sandbox Code Playgroud)
得到的总和(1 + 1/2 2 + 1/3 2 + ...)=ζ(2)=π 2 /6号
我粗略的方法这样做的方式:
public static void yeOldeWaye() {
double sum = 0;
for (double n = 1; ; n++) {
double term = 1 / (n * n);
if (Math.abs(term) <= 1e-12 * Math.abs(sum)) {
break;
}
sum += term;
}
System.out.println(sum); …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个扩展函数,其实现使用 Spring bean。通过在包的顶层定义扩展函数似乎不可能做到这一点。我试过这个:
@Component
class Converter {
companion object {
@Autowired
lateinit var transformer: Transformer
fun Class1.convert(): Class2 {
return Class2 (this, transformer.transform(someStringProperty))
}
}
}
Run Code Online (Sandbox Code Playgroud)
wheretransform是一个将某个字符串转换为另一个字符串的函数,并且Class1是某个具有someStringProperty属性的类。我希望其他类可以import pkg.Converter.Companion.convert然后能够使用x.convert()wherex是类型的对象Class1。
语法有效,x.convert()在其他类中使用编译正常。但它在运行时导致异常:kotlin.UninitializedPropertyAccessException: lateinit property transformer has not been initialized
看起来 Spring 没有自动装配变量,因为它位于伴随对象中,而不是实际的组件对象中。
注释伴随对象@Component不起作用。
我不认为Class1.convert直接在内部移动Converter会起作用,因为然后使用x.convert()将需要Converter对象的实例,并且我不知道如何使用扩展函数语法来做到这一点。
有什么办法可以做到这一点,还是必须放弃扩展函数语法?
使用 pyspark,我有一个包含两TimestampType列的数据框:
df.schema
...StructField(session_start,TimestampType,true),StructField(session_end,TimestampType,true)...
Run Code Online (Sandbox Code Playgroud)
但我不知道如何计算差异:
df2 = df.withColumn("session_length",col("session_end")-col("session_start"))
Run Code Online (Sandbox Code Playgroud)
给我
AnalysisException: u"cannot resolve '(`session_end` - `session_start`)' due to data type mismatch: '(`session_end` - `session_start`)' requires (numeric or calendarinterval) type, not timestamp ...
Run Code Online (Sandbox Code Playgroud)
我还没有找到可行的替代方案。(有一个datediff函数,但它以天为单位返回结果,我需要以秒为单位的差异。)
我应该怎么写这个?
编辑:这个问题的原始版本有一个不同的错误,col因为在我的笔记本中作为变量重用。在重做import以恢复功能后,我现在得到了上面的AnalysisException.
好吧,这是有史以来编程最糟糕的例子之一,但我在研究别人的问题时尝试了它,发现结果有点奇怪.任何解释?
public class Test {
static class Bizarre extends RuntimeException {
public void throwMe() {
throw this; // line 6
}
}
public static void main(String[] args) {
Bizarre biz = new Bizarre(); // line 12
System.out.println("Output line 1"); // line 13
biz.throwMe(); // line 14
System.out.println("Output line 2"); // line 15
}
}
Run Code Online (Sandbox Code Playgroud)
结果输出:
Output line 1
Exception in thread "main" Test$Bizarre
at Test.main(Test.java:12)
Run Code Online (Sandbox Code Playgroud)
为什么第12行?
这是Spring文档的第6.12.5节中的示例:
@Configuration
public class ServiceConfig {
@Autowired
private AccountRepository accountRepository;
@Bean
public TransferService transferService() {
return new TransferServiceImpl(accountRepository);
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:为什么必须accountRepository在它被使用之前创建new TransferServiceImpl()?另外,我不知道Spring怎么知道第二个依赖于第一个被设置(除非它通过transferService()字节码).是因为关于Spring执行命令的顺序保证@Autowired在@Bean可能调用方法之前处理变量?什么是处理订单?什么样的情况会导致Spring处理这些乱序?
我问的原因是我有一个案例,这样的事情不起作用,即new正在用一个null参数执行.无论是@Autowired变量被设置得太晚,或者完全不成立(我的猜测是后者的基础上,一些log4j.logger.org.springframework.beans调试输出,但我不知道).这种情况当然要复杂得多 - 这是一个很大的应用程序,配置类中还有一些@Autowired和@Bean定义.使用@DependsOn没有帮助.通过删除代码来缩小问题需要花费大量时间,直到我能得到一个最小的例子,但我想看看我是否可以通过了解有关Spring如何处理事情的更多细节来了解问题,然后再开始困难的代码减少路径.
我正试图在Swing中完成图形内容(绘制线条等).到目前为止,我所见过的所有教程都声明了一个覆盖的类paintComponent,并且所有paintComponent方法都做了一些设置,特定的事情,比如绘制一个红色方块(尽管可能每次都在不同的位置绘制它).或者他们可能会绘制一些线条和形状,但该paintComponent方法可以同时完成所有操作.
我想弄清楚:假设我想在一个组件中绘制一个东西,然后在它上面绘制其他东西而不删除我之前绘制的东西.我的第一个想法是让我的
paintComponent覆盖调用回调.
import java.awt.*;
import javax.swing.*;
public class DrawTest {
private interface GraphicsAction {
public void action (Graphics g);
}
private static class TestPanel extends JPanel {
private GraphicsAction paintAction;
public void draw (GraphicsAction action) {
paintAction = action;
repaint();
}
@Override
public void paintComponent (Graphics g) {
super.paintComponent (g);
if (paintAction != null)
paintAction.action(g);
}
}
private static void createAndShowGui() {
JFrame frame = new JFrame ("DrawTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(500,500));
TestPanel …Run Code Online (Sandbox Code Playgroud) 大约 20 年前第一次学习 C++ 基础知识后,我试图重新开始学习 C++,但后来把它放在一边。我首先拿起一本我当时学到的书。这是他的示例之一(我已经发现它有一些过时的语法):
#include <fstream.h>
void main() {
ifstream infile("iocopy.cpp");
if (!infile) cerr << "couldn't open iopcopy.cpp" << endl;
... other junk
}
Run Code Online (Sandbox Code Playgroud)
!他对运算符 on的解释infile是,它“检查对象是否非零”,这让我感到困惑(对象为零意味着什么?)。所以我决定自己尝试一下:
#include <fstream>
#include <iostream>
void main () {
std::ifstream f ("something");
// if (f == 0) std::cout << "Couldn't open file" << std::endl;
if (!f) std::cout << "Couldn't open file" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
这按预期工作,如果文件不存在则显示一条消息,如果存在则不显示任何内容。如果我取消注释该行(f == 0)并注释掉以下行,它也有效。
我想我找到了!这里的运算符;我operator!在定义为返回字段的头文件之一中找到了fail。不过,我对为什么第一个示例(f == 0)有效感到困惑,因为我没有看到任何 …
假设我有一个Iterator<Class1>对象. Class1提供了一个stringFunction()返回a 的方法String.
有没有办法以String.join一种方式调用迭代器返回的stringFunction()每一个Class1,并连接由分隔符分隔的函数结果?
有一个String.join过载,需要一个Iterable<CharSequence>; 是否可以从Iterator<Class1>和创建一个stringFunction,以便join可以使用它?
使用enum类型时,您可以为每个常量添加参数,为每个常量提供一个主体,并为该类型提供一个主体.但如果你不这样做:
enum enumType { C1, C2, C3 };
Run Code Online (Sandbox Code Playgroud)
使用enum类型是否有效,因为它只是声明整数常量?
static final int C1 = 1;
static final int C2 = 2;
static final int C3 = 3; // or whatever
Run Code Online (Sandbox Code Playgroud)
或者由于此功能(提供物体的能力)仍有一些开销,即使在这种情况下没有使用该功能?
所以我一直在使用Chip-8模拟器作为我的CompSci类的最终项目,并且遇到了一个似乎超出我的代码的问题.我下载的大量演示(我确信它们是真正的Chip-8程序,而不是SuperChip或类似的东西)包含的机器指令不符合任何Chip-8操作码的格式.
http://mattmik.com/files/chip8/mastering/chip8.html
在页面的底部是所有操作码的列表,每个操作码长2个字节,以及它们中的每个半字节数字代表什么.但是,相当数量的程序具有不符合任何指令格式的指令.例如,这是一个来自其中一个的十六进制转储 - 我会在其中指出一些个别情况
0000000 6a 00 6b 04 6c 01 6d 00 6e 02 23 26 23 20 60 30
0000010 61 01 f0 15 f0 07 f1 18 30 00 12 14 22 42 23 20
0000020 7d 01 23 20 60 08 e0 a1 23 0a 4a 00 12 3e a3 62
0000030 d8 91 79 01 d8 91 4f 01 12 f4 49 18 12 e4 22 b2
0000040 12 1e 4c 01 22 6c …Run Code Online (Sandbox Code Playgroud) java ×7
c++ ×2
java-8 ×2
spring ×2
apache-spark ×1
c++11 ×1
chip-8 ×1
class ×1
emulation ×1
graphics ×1
graphics2d ×1
hex ×1
if-statement ×1
java-stream ×1
javascript ×1
kotlin ×1
machine-code ×1
pyspark ×1
swing ×1