小编jah*_*roy的帖子

Java - 具有相同方法的不同对象的数组

我正在练习继承.

我有两个类似的类,我想要同化为一个数组,所以我想使用Object类作为超类,因为一切都是Object的一个子级.

所以,例如我将T类和CT类放入一个名为all的数组中:

 Object all[] = new Object[6];

    all[0] = T1;

    all[1] = CT2;

    all[2] =T3;

    all[3] = CT1;

    all[4] = T2;

    all[5] = CT3;
Run Code Online (Sandbox Code Playgroud)

我跳过了声明,因为那不是我的问题.

当我希望使用循环调用数组中的函数时,我的真正问题就变成了:

for (int i = 0; i < 6; i++) {

    all[i].beingShot(randomNum, randomNum, AK47.getAccuracy());
}
Run Code Online (Sandbox Code Playgroud)

涉及T和CT的类都分别具有isShot方法,该方法是公开的.

Eclipse建议将它们作为快速修复.我想知道除了创建我自己的持有beingShot方法的Object类,或者将其添加到Object类之外是否还有其他逻辑选择,尽管我觉得这些选择中的任何一个都会导致更长时间的问题.

谢谢!

java polymorphism inheritance casting interface

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

Java - 泛型与投射对象

我上课了 Data<T>

具有通用属性

private T value;
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法来做到以下几点?
即以不同的形式返回泛型类型?

public List<String> getValues() {
    if (value.getClass() != ArrayList.class)
        throw new Exception("Wrong Enum value '%s'", value);
    return (ArrayList<String>) value;
    //ugly
}


public String getStringValue() {
    if (value.getClass() != String.class)
        throw new Exception("Wrong value type '%s'", value);
    return (String) value;
    //ugly
}

public Float getFloatValue() {
    if (value.getClass() != Double.class)
        throw new Exception("Wrong value type '%s'", value);
    return (Float) value;
    //ugly
}

public Long getLongValue() {
    if (value.getClass() != Double.class)
        throw new Exception("Wrong value …
Run Code Online (Sandbox Code Playgroud)

java generics casting type-inference gson

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

如何在我的Java代码中使用Apache Ant的任务

我想从我的代码中使用Ant的任务(scp).有什么办法吗?

我应该简单地引用一个Ant的库并直接从我的代码中调用API吗?

java ant scp

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

方法修改方法超出范围

我今天有一个非常有趣的问题.我试图修改一个数组,但方法修改另一个甚至没有调用的数组.

a)导致麻烦的方法的一部分

public static void place2(String output, int position,furniture furniture_array[], char room_grid[][]) {

    int i=0;
    int j=0;

    furniture_array[position].setXY(i, j);
    char grid1[][] = room_grid;
    char grid2[][] = room_grid;
    char grid3[][] = room_grid;
    char grid4[][] = room_grid;
    pasteToArray(i, j, grid1, furniture_array[position]);
}
Run Code Online (Sandbox Code Playgroud)

B)pasteToArray

这是接收input_array的非常简单的方法,在这个数组中它将超过作为对象值的数组.

public static char[][] pasteToArray(
    int x, int y, char input_array[][], furniture furniture_to_be_placed) {

    char[][] result=input_array;

    for (int i = 0; i <= furniture_to_be_placed.getSize(); i++) {
        for (int j = 0; j <= furniture_to_be_placed.getSize(); j++) {
            result[x + i][y …
Run Code Online (Sandbox Code Playgroud)

java theory arrays

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

Java中的变量太多了?

我仍然是Java的新手,但我个人认为我的代码很草率,有太多的变量.我知道我可以将它们隐藏在方法或类中,但我听说将它们放在一个单独的类中是不好的做法,而且大多数都不是静态的.谁能给我一些提示?提前致谢!

public static float c = .2f; //cube dimensions
private float separation = 4.0f*c;
private float angle = 0f; //set to 0
private float rotate = 1.0f;
private float h_height = 1.0f;
private float h_width = 1.0f;
private float h_length = 1.0f;
private float b_height = 1.5f;
private float b_width = .5f;
private float b_length = 1.0f;
private float a_height = 1.5f;
private float a_width = .5f;
private float a_length = .5f;
private float l_height = 1.5f;
private float l_width …
Run Code Online (Sandbox Code Playgroud)

java oop

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

在页面上显示JavaScript控制台错误

假设我有以下代码:

<script>

    function billy() {
        alert('muahahahaha!');
    }

    function suzzy() {
        return;
    }

</script>
Run Code Online (Sandbox Code Playgroud)

和这样的按钮(带有未定义的onclick处理程序):

<input type='button' value='click me' onClick='FRANK()' />
Run Code Online (Sandbox Code Playgroud)

单击该按钮时,开发人员控制台中将显示以下内容:

函数'FRANK()'未定义.

如何将该消息存储在变量中并将其显示在页面上?

document.getElementById('prompt').innerHTML = log;
Run Code Online (Sandbox Code Playgroud)

所以它看起来像:

<div id='prompt'>
    Function 'FRANK()' has not been defined.
</div>
Run Code Online (Sandbox Code Playgroud)

javascript dom console.log

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

使用java.io.File时出现NullPointerException

我试图使用这个程序来计算D:\驱动器中的所有文件,但是当我运行它时它会抛出异常.

package lmh;

import java.io.File;

public class FileList {

    static int fileNum = 0;
    static int directoryNum = 0;
    static int cannotRead = 0;

    public static void main(String[] args) {
        File f = new File("e:/");
        printFileStructure(f);
        System.out.println("result:");
        System.out.println("file number?" + fileNum);
        System.out.println("directory number?" + directoryNum);
        System.out.println("cannot rend:" + cannotRead);
    }

    public static void printFileStructure(File f) {
        File[] files = f.listFiles();
        for (int i = 0; i < files.length; i++) {
            if (files[i].isFile()) {
                if (files[i].canRead()) {
                    fileNum++;
                    System.out.println(files[i].getName());
                } else …
Run Code Online (Sandbox Code Playgroud)

java java-io

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

使用JavaScript正则表达式的建议

我正在尝试写一个正则表达式,其中:

  1. 仅以字母开头
  2. 仅包含字母和数字
  3. 长度不超过32个字符

到目前为止我所拥有的是:

^[a-zA-Z][0,31]+$
Run Code Online (Sandbox Code Playgroud)

但我不确定这是否正确.

javascript regex

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

'循环'JavaScript函数

我有以下脚本.

function slideShow1(){
    document.getElementById('dynimg').src="Other/noctis.jpg";
    var timer1 = setTimeout(slideShow2(),5000);
}

function slideShow2(){
    document.getElementById('dynimg').src="Other/retriever.jpg";
    var timer2 = setTimeout(slideShow3(),5000);
}

function slideShow3(){
    document.getElementById('dynimg').src="Other/miningop2.jpg";
    var timer3 = setTimeout(slideShow1(),5000);
}
Run Code Online (Sandbox Code Playgroud)

这很粗糙,我知道......而且它也没有用.我们的想法是让每个函数在给定的时间段后触发下一个函数,从而创建幻灯片,其中img重复更改.我想使用body onload ="slideShow1()"

html javascript web

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

多个If条件在Java中使用Iterator

我有一个列表,其中包含元素1到10.我尝试从中删除素数2,3,5,7然后使用iterator打印列表的其余部分.但此代码抛出 NoSuchElementException.这是我的代码:

public static void editerate2(Collection<Integer> list3)
{
    Iterator<Integer> it=list3.iterator();
    while(it.hasNext())
    {
        if(it.next()==2 || it.next()==3 || it.next() ==5 || it.next()==7 ) 
        {
            it.remove();
        }
    }
    System.out.println("List 3:");
    System.out.println("After removing prime numbers  : " + list3);
}
Run Code Online (Sandbox Code Playgroud)

这样做的正确方法是什么?还有什么区别使用"|" 和"||" ???

java collections iterator

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