小编arn*_*bpl的帖子

在Python中创建列表对象类

这是一个基本问题.我正在尝试以下代码:

class SMS_store:

def __init__(self):
    self=[]    #probably something is wrong here

def add_new_arrival(self,from_number,time_arrived,text_of_SMS):
    self.append([False,from_number,time_arrived,text_of_SMS])    #append list to self list
    self[len(self)-1]=tuple(self[len(self)-1])

def message_count(self):
    return len(self)

my_inbox=SMS_store()
my_inbox.add_new_arrival('01234','9:37 AM','How are you?')
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:

>>> 
Traceback (most recent call last):
  File "C:\Users\Arnob\Desktop\New Text Document.py", line 15, in <module>
    my_inbox.add_new_arrival('01234','9:37 AM','How are you?')
  File "C:\Users\Arnob\Desktop\New Text Document.py", line 8, in add_new_arrival
    self.append([False,from_number,time_arrived,text_of_SMS])    #append list to self list
AttributeError: 'SMS_store' object has no attribute 'append'
>>>
Run Code Online (Sandbox Code Playgroud)

我的代码有什么问题?

python class list append

7
推荐指数
2
解决办法
5万
查看次数

Windows批处理中嵌套循环中的"continue"等效命令

我有一个批处理文件,其中包含带continue-like命令的嵌套循环:

for %%i in (1, 2, 4, 8, 16, 32, 64, 128, 256) do (
    for %%j in (1, 2, 4, 8, 16, 32, 64, 128, 256) do (
        if %%i gtr %%j goto CONTINUE
        test.exe 0 %%i %%j 100000 > "%%i_%%j".txt
        :CONTINUE
        rem
    )
)
Run Code Online (Sandbox Code Playgroud)

但是当if声明第一次成立时,它不会进一步迭代.我只获取文本文件1_256.txt.所以似乎goto CONTINUE有问题.我的批处理文件有什么问题?

windows for-loop cmd batch-file

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

在Java 2D API中将图像缩放为单个表面

有一个叫方法scale(double sx, double sy)Graphics2DJava中.但是这种方法似乎将图像缩放为单独的表面而不是单个表面.结果,如果原始图像没有额外的宽度和高度,则缩放图像具有尖角.以下屏幕截图演示了此问题:

在此输入图像描述

这是代码:

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class TestJava {
    static int scale = 10;

    public static class Test extends JPanel {
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.scale(scale, scale);
            g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

            BufferedImage img = null;
            try {
                img = ImageIO.read(new File("Sprite.png"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            g2.drawImage(img, null, 5, 5);
        } …
Run Code Online (Sandbox Code Playgroud)

java swing 2d image awt

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

WPF - 将静态资源分配到 XAML 数组中,无需代码隐藏

我正在研究 MS .NET WPF;在 XAML 中,我定义了一些静态资源。我想将它们分配到一个也在 XAML 中声明的数组中。

以下是静态资源:

<local:Person x:Key="PersonABC" Name="Hello" Age="29" />
<local:Person x:Key="PersonXYZ" Name="World" Age="55" />
Run Code Online (Sandbox Code Playgroud)

但是我无法通过类似的东西将它们分配到数组中{StaticResource PersonABC}。我必须重新创建资源并将它们分配到数组中:

<x:Array x:Key="PersonList" Type="{x:Type local:Person}">
    <local:Person Name="Hello" Age="29" />
    <local:Person Name="World" Age="55" />
</x:Array>
Run Code Online (Sandbox Code Playgroud)

但我想要这样的东西:

<x:Array x:Key="PersonList" Type="{x:Type local:Person}">
    "{StaticResource PersonABC}"
    "{StaticResource PersonXYZ}"
</x:Array>
Run Code Online (Sandbox Code Playgroud)

wpf xaml staticresource

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

在Java中编写文件时如何避免额外的头字节?

首先,我没有使用Java 的高级默认序列化来在文件中编写对象.我手动在文件中写一些原始类型变量.这是一个例子:

public class TestMain {
    public static void main(String[] args) {
        ObjectOutputStream out = null;
        try {
            out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("Test.bin")));

            out.writeInt(1024);
            out.writeInt(512);
            out.writeInt(256);
            out.writeInt(128);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我测试了创建的文件"Test.bin".filesize是22个字节.但是计算我实际写在文件中的内容,文件大小应该是16个字节.[ int每个是4个字节.4个int变量.所以4*4 = 16字节.]

那么额外的6个字节是多少?[22 - 16 = 6个字节]我用十六进制编辑器测试了该文件.这就是我发现的:

ac ed 20 …

java io file stream

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

如何在Python中返回相同的类对象

这是一个基本问题.我写了以下代码:

class Point:
    def __init__(self,x=0,y=0):
        self.x=x
        self.y=y
    def __str__(self):
        return '({0} , {1})'.format(self.x,self.y)
    def reflect_x(self):
        return Point(self.x,-self.y)

p1=Point(3,4)
p2=p1.reflect_x

print(str(p1),str(p2))
print(type(p1),type(p2))
Run Code Online (Sandbox Code Playgroud)

这里p1的类型和p2的类型是不同的.我只想将p2作为一个点,它是x轴的p1反射点.我该怎么做?

python methods class

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

如何在 std::fmt::Debug 中使用内部 #[derive(Debug)]

我想自定义的输出structFooOut,但我不想改变内场的调试输出FooIn这里面FooOut。考虑 Rust代码

#[derive(Default)]
struct FooOut {
    num1: u32,
    num2: u32,
    obj: FooIn,
}

#[derive(Debug, Default)]
struct FooIn {
    bval: bool,
    list: Vec<u32>,
}

impl std::fmt::Debug for FooOut {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "FooOut {{ sum: {}, num1: {}, num2: {}, obj: {} }}",
                   self.num1 + self.num2,
                   self.num1,
                   self.num2,
                   self.obj)  // self.obj is not working without implementing it too
    }
}
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,我试图自定义FooOut(通过添加sum)的调试输出,但我不想自定义/重新实现 …

rust

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

标签 统计

class ×2

java ×2

python ×2

2d ×1

append ×1

awt ×1

batch-file ×1

cmd ×1

file ×1

for-loop ×1

image ×1

io ×1

list ×1

methods ×1

rust ×1

staticresource ×1

stream ×1

swing ×1

windows ×1

wpf ×1

xaml ×1