其他类构造函数中的Java调用对象

时间:2015-08-26 14:02:17

标签: java

我正在开发一个用于密码管理器的个人应用程序,并且在调用其他类构造函数中的对象时卡住了。我的问题是如何使用它。这是代码:

public class Account {
    public String username, email;
    public Password password;

    public Account(String username, String email, Password password){
        this.username = username;
        this.email = email;
        this.password = password;
    }

    public static void main(){
        Account a1 = new Account("test", "test@test", );
    }
}

这里是密码类的代码:

public class Password {
    public String password;
    public char[] passwordArray;
    public char[] alphabet = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
    public char[] symbols = {'!', '$', '&', '=', '?', '.', '-', '_', '@', '#', '+'};
    public char[] numbers = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
    public char[] miscellanous = merge(alphabet, symbols, numbers);

    public Password(String password){
        this.password = password;
    }

    public Password(int length){
        passwordArray = new char[length];
    }

    public void generateRandom(char how){
        switch(how){
            case 'a':
                for(int i = 0; i < passwordArray.length; i++){
                    passwordArray[i] = alphabet[rand(0, alphabet.length)];
                }
                break;
            case 's':
                for(int i = 0; i < passwordArray.length; i++){
                    passwordArray[i] = symbols[rand(0, symbols.length)];
                }
                break;
            case 'n':
                for(int i = 0; i < passwordArray.length; i++){
                    passwordArray[i] = numbers[rand(0, numbers.length)];
                }
                break;
            case 'm':
                for(int i = 0; i < passwordArray.length; i++){
                    passwordArray[i] = miscellanous[rand(0,miscellanous.length)];
                }
                break;
            default:
                System.out.println("Incorrect: choose one.");
                break;
        }
    }

    public void findChar(int first, int second){
        boolean bothCorrectIndex = false;

        if( (first > 0 && first <= password.length()) && (second > 0 && second <= password.length()) ){
            bothCorrectIndex = true;

            if(bothCorrectIndex){
                char firstChar = password.charAt(first - 1);
                char secondChar = password.charAt(second - 1);

                System.out.println("1: " + firstChar + "\n" + "2: " + secondChar);
            }
            else{
                System.out.println("Incorrect index.");
            }
        }
        else{
            System.out.println("Incorrect index.");
        }
    }


    public static char[] merge(char[] firstArray, char[] secondArray, char[] thirdArray){
        char[] mergedArray = new char[firstArray.length + secondArray.length + thirdArray.length];
        int firstArraylength = firstArray.length, counter = 0, arraysLength = firstArray.length + secondArray.length, counter2 = 0;

        for(int i = 0; i < firstArray.length; i++){
            mergedArray[i] = firstArray[i];
        }
        while(firstArraylength < (firstArray.length + secondArray.length) && counter < secondArray.length){
            mergedArray[firstArraylength] = secondArray[counter];
            firstArraylength++;
            counter++;
        }
        while(arraysLength < mergedArray.length && counter2 < thirdArray.length){
            mergedArray[arraysLength] = thirdArray[counter2];
            arraysLength++;
            counter2++;
        }

        return mergedArray;
    }
}

1 个答案:

答案 0 :(得分:1)

我希望您可能会遇到一些编译时错误。

在Account类中似乎有两个语法错误。

1:主方法签名需要String数组类型的形式参数。

2:Account构造函数需要第三个类型为Password的实际参数。

public static void main(String[] args){
    Account a1 = new Account("test", "test@test", new Password("Hello"));
}

至于Password类,我没有看到“rand”的方法声明索引到char数组。也许您可以声明类型为java.util.Random的实例变量,并稍微调整方法调用以使用Random.nextInt(int)。这将返回一个从0(含)到指定值(不包括)的随机整数。

 private Random rand = new java.util.Random();

 public void generateRandom(char how){
    switch(how){
        case 'a':
            for(int i = 0; i < passwordArray.length; i++){
                passwordArray[i] = alphabet[rand.nextInt(alphabet.length)];
            }
            .
            .
            .