【问题标题】:Cannot Find Symbol Error While Calling Method From Same Class Within Constructor在构造函数中从同一类调用方法时找不到符号错误
【发布时间】:2020-03-17 02:58:31
【问题描述】:

因此,我的老师要求我创建一个 Customer 类,该类具有一个带有 7 个参数的构造函数,其中一个是密码字符串变量,必须使用构造函数中的 encrypt() 方法对其进行加密,以确保所有密码在实例化新客户对象时加密。 encrypt() 方法必须使用一个简单的替换密码和 2 个静态数组来存储明文字母和包含大写、小写和数字 0-9 的密文字母。我得到了一个名为 Encryptable 的接口,它需要定义一个用于加密的公共 void encrypt() 方法和一个用于返回解密密码字符串的公共 String decrypt() 方法。在构造函数中调用 encrypt() 方法时,我收到“找不到符号 - 方法 encrypt()”错误。由于该方法定义明确,我不明白错误来自哪里。有任何想法吗?另外,由于我还不能编译,我不知道 encrypt 方法是否正确加密了密码,所以如果您在这方面发现任何问题,请随时告诉我。

public class Customer implements Encryptable
 {
  private String customerID;
  private String lastName;
  private String firstName;
  private String email;
  private String userName;
  private float balance;
  private String password;
  private boolean encrypted = false;

  String[] plaintext = {"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", "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", "1", "2", "3", 
  "4", "5", "6", "7", "8", "9", "0"};

  String[] ciphertext= {"p", "0", "o", "9", "i", "8", "u", "7", "y", "6", "t", "5", "r", "4", "e", 
  "3", "w", "2", "q", "1", "l", "k", "j", "h", "g", "f", "d", "s", "a", "m", "n", "b", "v", "c", "x", 
  "z", "P", "O", "I", "U", "Y", "T", "R", "E", "W", "Q", "L", "K", "J", "H", "G", "F", "D", "S", "A", 
  "M", "N", "B", "V", "C", "X", "Z"};


  public Customer(String customerID, String lastName, String firstName, String email, String 
  userName, float balance, String password)
  {
   this.customerID = customerID;
   this.lastName = lastName;
   this.firstName = firstName;
   this.email = email;
   this.userName = userName;
   if (balance > 0)
    {  this.balance = balance;  }
   this.password = password.encrypt();
  }

  public void encrypt()
   {  
    if (!encrypted)
    {
     String tempEncryptedPassword = "";
     for (int i = 0; i < password.length(); i++)
      {
       int j = 0;
       while (!(plaintext[j].contains(password.substring(j))))
        {
         j++;
         if (plaintext[j].contains(password.substring(j)))
          {
           tempEncryptedPassword += ciphertext[j];
          }
        }
      }
     this.password = tempEncryptedPassword;
     this.encrypted = true;
    }
   }

    final public String getCustomerID()
    {   return this.customerID;   }

    final public String getFirstName()
    {   return this.firstName;   }

    final public String getLastName()
    {   return this.lastName;   }

    final public String getPassword()
    {   return this.password;   }

    final public String decrypt()
    {
      String tempEncryptedPassword = "";
      if (encrypted)
      {
        for (int i = 0; i < password.length(); i++)
        {
         int j = 0;
         while (!(ciphertext[j].contains(password.substring(j))))
          {
           j++;
           if (ciphertext[j].contains(password.substring(j)))
            {
              tempEncryptedPassword += plaintext[j];
              break;
            }
          }
        }
       } 
      return tempEncryptedPassword;
     }

     final public String toString()
     {
       String info = "";
       info = ("\n============ Customer Information ============");
       info += ("\n\t* Customer Name: " + firstName + " " + lastName);
       info += ("\n\t* Customer ID Number: " + customerID);
       info += ("\n\t* Customer Email: " + email);
       info += ("\n\t* Customer Balance: " + balance);
       return info;
      }
    }       

【问题讨论】:

  • 错误发生在哪个方法和哪一行?
  • this.password = password.encrypt();
  • .encrypt();导致错误。这发生在构造函数中。
  • "password" 是 String,它没有 encrypt() 方法。像这样称呼它:this.password = encrypt( this.password );
  • 问题是我会将 this.password 作为参数传递给不需要参数并导致另一个错误的方法

标签: java encryption substitution


【解决方案1】:

想留下这个,但我解决了我的问题。需要自己调用加密方法,而不是像我一样关闭变量。另外,我认为加密方法无法正常工作。这是我用来使它工作的代码:

    public void encrypt()
    {  
     if (!encrypted)
      {
        String tempEncryptedPassword = "";
        for (int index = 0; index < password.length(); index++)
         {
          int j = 0;
          while (!(plaintext[j].contains(password.substring(index, index+1))))
           {
            j++;
            if (plaintext[j].contains(password.substring(index,index + 1)))
            {
              tempEncryptedPassword = tempEncryptedPassword + ciphertext[j];  
            }
           }
         }
         this.password = tempEncryptedPassword;
         this.encrypted = true;
        }
       }

【讨论】:

    猜你喜欢
    • 2015-01-09
    • 1970-01-01
    • 2019-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多