簡體   English   中英

為什么構造函數不適用

[英]Why constructor cannot applicable

這是我的課

public class SecretName{
      private String str;
      private int i;
       private int j;
      Scanner cycle=new Scanner(System.in);

public SecretName(String str , int i)
{
    this.str = str;
    this.i = i;
}

public SecretName(String str,int i, int j )
{
    this.i = i;
    this.j = j;
    this.str = str;
}

public void cycleName(String str , int i)// method string and integer
{

        System.out.print("Enter string:");
        str=cycle.next()+" ";
        System.out.print("Enter value:");
        i=cycle.nextInt();
        int l,len,y;
        String str2="";
        char x;
        len=str.length();

        for(l=0;l<len;l++)
            {
                x=str.charAt(l);
                int c=Integer.valueOf(x);
                if(i>0)
                    {
                        if((c>=97)&&(c<=123-i))
                            {
                                c=c+i;
                                str2=str2+(char)(c);
                            }
                            else
                                {
                                    y=c+i;
                                    c=(y-123)+96;
                                    str2=str2+(char)(c);
                                }
                    }
            }System.out.println("New string:" +str2);
}


public void cycleName(String str, int i, int j)//method string and 2 integer value
{

String 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", "~", "!", "@", "#",}
};


int l=0;

for(i=0; i<6; i++) {//column
  for(j=0; j<5; j++)//row
    System.out.print(alphabet[i][j] + " ");
  System.out.println();
}

System.out.print("Enter string:");
str=cycle.next();
System.out.print("Enter value:");
int n=cycle.nextInt();
int y;
String str2="";
char x;
String len=alphabet[i][j];

for(i=0; i<6; i++) {//column
  for(j=0; j<5; j++)//row
    {
        //x=str.charAt(l);
        //int c=Integer.valueOf(x);
        if(n>0)

            {
                if((i>=0)&&(j<=6-n))

                    {
                        j=j+n;
                        str2=str2+(i);
                    }

                    else

                        {
                            y=i+n;
                            i=(y-90)+64;
                            str2=str2+(i);
                        }

            }

            }System.out.print("New string: " +str2);
        }
}

這是我的延伸班。

public class MySN extends SecretName
{
public static void main(String[] arg)
{
    SecretName cn1 = new SecretName();

    cn1.cycleName("abu", 3);
    System.out.println(cn1.cycleName());
}

問題是當我運行它時,我收到此錯誤:構造函數SecretName.SecretName(String,int,int)不適用(實際和正式參數列表的長度不同)構造函數SecretName.SecretName(String,int)不適用(實際和正式的論證清單的長度不同)有人可以幫我嗎?

您需要添加默認構造函數,因為您是通過main方法使用它的。

SecretName cn1 = new SecretName();

所以將其添加到SecretName

public SecretName() {}

您已經在類中定義了兩個構造函數:

public SecretName(String str , int i) {
    //...
}
public SecretName(String str, int i, int j) {
    //...
}

它們都需要自變量(分別為兩個或三個自變量)。 但是,您嘗試調用不帶參數的構造函數:

new SecretName()

您要么需要添加一個無參數的構造函數:

public SecretName() {
    // initialize fields to default values?
    // or maybe don't initialize anything?
}

或者您需要向構造函數提供參數:

new SecretName("", 0)
// or...
new SecretName("", 0, 0)
// though you can use any values you like

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM