小编Rya*_* Yu的帖子

Java - 静态和动态类型 - "这打印什么?"

所以我在理解类中显示的这个例子时遇到了一些麻烦 - 它应该说明Java中静态和动态类型之间的微妙之处.

public class Piece {

    public static void main (String[] args) {
        Piece p2 = new Knight();
        Knight p1 = new Knight();
        p1.capture(p2); // call 1; "Knight is bored" is supposed to be printed
        p2.capture(p1); // call 2; "knight is bored" is supposed to be printed
    }

    public void capture () {
        System.out.println("Capturing");
    }

    public void capture (Piece p) {
        System.out.println("I'm bored")
    }

public class Knight extends Piece {

    public void capture (Piece p) {
        System.out.println("Knight is bored"); …
Run Code Online (Sandbox Code Playgroud)

java oop inheritance static dynamic

3
推荐指数
1
解决办法
2623
查看次数

MIPS - 检查数组元素是否为空终止字符

所以我正在尝试编写一个函数来在 MIPS 中找到字符串的长度。

我沿着数组走/遍历,加载每个字符,我想将每个字符与空终止字符进行比较,以查看字符串是否已“结束”。在每次连续迭代中,我都会增加一个计数器,然后在字符串“结束”后将计数器存储在 $v0 中。但是,如何比较当前加载的字符是否为空终止字符“\0”?更具体地说,我如何表示这个空终止字符?正如我在下面所做的那样,它是零美元吗?如果是这样,那么我还做错了什么?获取地址错误。

.data
msg1:.asciiz "Please insert text (max 20 characters): "
msg2:.asciiz "\nThe length of the text is: "

newline: .asciiz "\n"

str1: .space 20
.text
.globl main
main:
addi $v0, $v0,4
la $a0,msg1
syscall #print msg1
li $v0,8
la $a0,str1
addi $a1,$zero,20
syscall   #get string 1

la $a0,str1  #pass address of str1
jal len

len: 
addi $t2, $zero, 0 # $t2 is what we want to return in the end -- the count of the …
Run Code Online (Sandbox Code Playgroud)

string mips

3
推荐指数
1
解决办法
3万
查看次数

Rails:约会调度功能?

所以我有一个User模型,以及所有我希望能够做的是有可用约定时隙的列表,并让用户自己注册过程中选择其中之一.我在脑海中想象我会有一些TimeSlotList,它会在两个DateTime实例之间以xxx分钟为增量生成TimeSlots,然后让User有一个TimeSlot作为属性?也许每个TimeSlot都有一个taken布尔值,表明用户是否已经使用它?

有什么标准方法可以做这样的事情吗?我无法想象这实现功能的情况并不常见.

ruby-on-rails

3
推荐指数
1
解决办法
3577
查看次数

将字节数组解码为unicode时检测空字符串?(Python)

我正在尝试逐个字符读取字节数组并将其解码为 un​​icode 字符串,如下所示:

current_character = byte_array[0:1].decode("utf-8")
Run Code Online (Sandbox Code Playgroud)

对于每个字符,我试图检查 .decode("utf-8") 的结果是否等于空字符串,但我似乎无法检测到这一点。当我打印出解码结果时,我得到空字符串。但是我如何将这个检测转换成代码呢?

我试过了:

if not current_character

if current_character is u""
Run Code Online (Sandbox Code Playgroud)

但它们都不起作用。有什么建议?

python unicode

3
推荐指数
1
解决办法
1万
查看次数

使用Node客户端向localhost发出请求?

我有一个非常简单的节点服务器在端口8080上运行,我正在尝试获得一个同样简单的节点客户端来命中这个服务器.

为什么这个代码工作:

var http = require('https');

http.get('http://localhost:8080/headers', function(response) {
    console.log('Status:', response.statusCode);
    console.log('Headers: ', response.headers);
    response.pipe(process.stdout);
});
Run Code Online (Sandbox Code Playgroud)

但这段代码确实有用吗?:

var http = require('http');
var client = http.createClient(8080, 'localhost');
var request = client.request('GET', '/headers');
request.end();
request.on("response", function (response) {
    console.log('Status:', response.statusCode);
    console.log('Headers: ', response.headers);
    response.pipe(process.stdout);
});
Run Code Online (Sandbox Code Playgroud)

node.js

3
推荐指数
1
解决办法
7579
查看次数

Java中的字符串总是从compareTo返回某个结果?

对于任何人来说String x,是否会在x.compareTo(y)调用时始终返回负数String y?同样,是否x总会返回正数?谢谢.

java compareto

2
推荐指数
1
解决办法
165
查看次数

在Java中实例化集合集?

我想实例化一组Set(of strings),然后将两个Set<String>放入其中,如下所示:

Set<String> setOne = retrieveSetOne();
Set<String> setTwo = retrieveSetTwo();
Set<Set<String>> myCollection = new HashSet<new HashSet<String<()>(); // not working
myCollection.add(setOne);
myCollection.add(setTwo);
Run Code Online (Sandbox Code Playgroud)

问题是,我对嵌套集的实例化不起作用.我该怎么做呢?

java

2
推荐指数
1
解决办法
998
查看次数

用节点重复询问用户输入?

这里是JS/Node的新手.我正在尝试编写一个非常简单的Node程序来反复要求用户键入用户名,直到他/她键入关键字done.我正在使用提示npm包(https://www.npmjs.com/package/prompt).

var prompt = require('prompt');

// Start the prompt
prompt.start();

var currentDinerName = "";
var done = false;

while (done !== true) {
    // Ask for name until user inputs "done"
    prompt.get(['name'], function(err, result) {
        console.log('Diner name: ' + result.name);
        currentDinerName = result.name;
        if (currentDinerName === 'done') {
            console.log('We are done.');
            done = true;
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

但是,我得到了内存泄漏 - 它不喜欢这个while循环.在Node/JS中执行此操作的正确方法是什么?

谢谢.

node.js

2
推荐指数
1
解决办法
1730
查看次数

CSS/HTML:仅当屏幕大小<xxx像素时才将换行符添加到HTML?

所以我设计了一个看起来像这样的页面:

在此输入图像描述

它是响应式的,但是当我缩小窗口大小时,它看起来像这样:

在此输入图像描述

我想修改间距,我希望在按钮和下面的图片之间有一个换行符或两行.但是,如果屏幕尺寸小于767像素,我只想要这些换行符.做这个的最好方式是什么?谢谢.

html css

1
推荐指数
1
解决办法
2338
查看次数

Python中用于访问可能不存在的ak/v对值的最佳实践?

function getParams(data) {
    return {
        id: data && data.uuid
    }
}
Run Code Online (Sandbox Code Playgroud)

因此,上面代表了Javascript中用于访问对象项的常见模式.

Python中用于访问dict项目的最常用的等效实践是什么?

会这样吗?

def getParams(data):
    params = {}
    if data is not None and hasattr(data, "id"):
        params["id"] = data["id"]
    return params
Run Code Online (Sandbox Code Playgroud)

如果没有,最佳做法是什么?谢谢.

javascript python

1
推荐指数
1
解决办法
142
查看次数

Golang接口和转换

我有以下代码:

   func returnTheMap() map[string][]string{
        myThing := getSomeValue()
    }
Run Code Online (Sandbox Code Playgroud)

getSomeValue() 返回类型的东西 map[string]interface{}

但它始终在内部map[string][]string.

什么是设置myThing相同的最佳方式getSomeValue(),但类型map[string][]string

我可以像这样制作一个新对象:

newMap := make(map[string][]string)

// cardTypeList is of type map[string]interface {}, must convert to map[string][]string
for k, v := range myThing {
    newMap[k] = v.([]string)
}
Run Code Online (Sandbox Code Playgroud)

但有没有办法在这里做到这一点,或者有任何首选的方法来做到这一点?

go

1
推荐指数
1
解决办法
150
查看次数

C - 基本结构问题

所以我现在正在努力学习C,并且我有一些基本的结构问题,我想清理一下:

基本上,一切都围绕着这段代码:

#include <stdio.h>
#include <stdlib.h>

#define MAX_NAME_LEN 127

typedef struct {
    char name[MAX_NAME_LEN + 1];
    unsigned long sid;
} Student;

/* return the name of student s */
const char* getName (const Student* s) { // the parameter 's' is a pointer to a Student struct
    return s->name; // returns the 'name' member of a Student struct
}

/* set the name of student s
If name is too long, cut off characters after the maximum number of characters …
Run Code Online (Sandbox Code Playgroud)

c struct

0
推荐指数
1
解决办法
179
查看次数

具有继承性的静态和动态类型 - Java中的细微之处?

所以我有这个相关的代码......

public class PokemonTrainer {
    private Pokemon p = new Squirtle();
    private String name;

    public PokemonTrainer(String name) {
        this.name = name;
    }

    public static void main(String[] args) {
        PokemonTrainer pt = new PokemonTrainer("Ash");
        try {pt.fightGary();}

        catch (Charmander c) {
            System.out.println("You were roasted by a Charmander.");
        }

        catch (Squirtle s) {
            System.out.println("You were drowned by a Squirtle.");
        }

        catch (Bulbasaur b) {
            System.out.println("You were strangled by a Bulbasaur.");
        }

        catch (Pokemon p) {
            System.out.println("You survived!");
        }
    }

    public void fightGary() throws …
Run Code Online (Sandbox Code Playgroud)

java inheritance

-3
推荐指数
1
解决办法
194
查看次数

标签 统计

java ×4

inheritance ×2

node.js ×2

python ×2

c ×1

compareto ×1

css ×1

dynamic ×1

go ×1

html ×1

javascript ×1

mips ×1

oop ×1

ruby-on-rails ×1

static ×1

string ×1

struct ×1

unicode ×1