【问题标题】:Java pass variable to class return different variableJava将变量传递给类返回不同的变量
【发布时间】:2019-05-15 20:22:31
【问题描述】:

所以我已经看了几天关于类、引用和方法的大量帖子,设置和获取。我似乎无法理解这一切是如何运作的。

免责声明是的,这是针对课堂作业的,下面这个糟糕的例子是为了说明。非常感谢您的帮助。

我已经有一个有效的程序(尽管程序非常初级且效率低下)。然而,我所拥有的一切都在我的 Primary 类的 main 方法中,我需要将它的一部分放在一个单独的类中并让它仍然可以工作。

使用 main 方法,用户输入明文密码 (clearText),其中提供的 sn-p 代码将密码散列为 (hashText)。然后将其用于其他事情。

我希望完成的是将密码的sn-p从我的主类的主要方法中分离出来,进入一个单独的辅助类。

我不知道该怎么做。如何将clearText导入二级类,然后将hashText输出回一级类中的main方法。

谢谢。

import java.util.*;
import java.io.*;

public class Primary {
   public static void main(String[] args) throws Exception {
      String clearText = ("");

      System.out.print("Type Text");
      clearText = scnr.next();

   //Somehow export clearText to the secondary class
   //Somehow import hashText into the main method for further use

      System.out.println(hashText);
   }
}

public class Secondary {
   String hashText = ("");

   //Somehow import clearText value inputted but the user

   //here is where clearText gets hashed(I have that)

   //Somehow export hashText for use in the main method
}

【问题讨论】:

  • 你真的需要使用“Secondary”类吗?如果没有,为什么不使用一个简单的方法接收 clearText 并返回其散列内容。

标签: java string class methods


【解决方案1】:

欢迎来到编程。这里要考虑的一件事是,您似乎认为您的 Secondary 类是一个不可变的实体,并且与您的程序一起运行。它不是。您正在创建一个对象,其中包含要在程序的其他地方使用的数据和功能。

您的辅助对象可以被实例化,然后用于执行任务。您也可以通过在基类内部创建的另一个方法来执行此操作,但这也必须是静态的。

我在包含哈希实例的辅助类中看不到太多意义,并且该问题已经得到解答。我建议您考虑将其创建为服务。

import java.util.*;
import java.io.*;

public class Primary {
   public static void main(String[] args) throws Exception {
      String clearText = ("");

      // Collect user input
      System.out.print("Type Text");
      clearText = scnr.next();

      // Create hash and display
      String hashText = HashService.generateHash(clearText);
      System.out.println(hashText);
   }
}

public class HashService {
  public static String generateHash(String fromText){
    // implementation here
    return null;
  }
}

编辑:看起来有人删除了对象答案。如果您出于某种原因想要将散列密码作为一个对象维护,您可以这样做

import java.util.*;
import java.io.*;

public class Primary {
   public static void main(String[] args) throws Exception {
      String clearText = ("");

      // Collect user input
      System.out.print("Type Text");
      clearText = scnr.next();

      // Create hash and display
      HashedPassword hash = new HashedPassword(clearText);
      String hashText = hash.getHashedPW();
      System.out.println(hashText);
   }
}

public class HashedPassword {
  private String hashedPW = ""
  public HashedPassword(String fromText){
    // Assign hashedPW with some logic
    hashedPW = fromText;
  }

  public getHashedPW(){
    return hashedPW;
  }
}

【讨论】:

  • 谢谢,您的回复的上半部分让我可以做我想做的事情。然后我可以修改它并在我的更大项目中实现它。
【解决方案2】:

简单地在第二个类中创建一个方法,它接受你的明文并返回哈希值,然后从主方法中调用第二个类的对象上的这个方法

【讨论】:

    【解决方案3】:
    public static void main(String[] args) throws Exception {
        String clearText = ("");
    
        System.out.print("Type Text");
        clearText = scnr.next();
    
        // Somehow export clearText to the secondary class
        // Somehow import hashText into the main method for further use
    
        System.out.println(Secondary.stringToHash(clearText));
    }
    
    publicclass Secondary {
    
        public static long stringToHash(String input) {
            // some transformation
        }
    
        // Somehow import clearText value inputted but the user
    
        // here is where clearText gets hashed(I have that)
    
        // Somehow export hashText for use in the main method
    }
    

    这应该可以,但你并不需要另一个类,你可以创建一个静态方法。

    【讨论】:

      【解决方案4】:

      您可以执行以下操作:

      import java.util.*;
      import java.io.*;
      
      public class Primary {
          public static void main(String[] args) throws Exception {
              String clearText = ("");
      
              System.out.print("Type Text");
              clearText = scnr.next();
      
              String hashText = Secondary.hashText(clearText);
      
              System.out.println(hashText);
          }
      }
      
      public class Secondary {
      
          public static String hashText(String clearText) {
              // TODO return the hash
              return null;
          }
      
      }
      

      或者,如果你的辅助类中有一些非静态的东西,那么你可以创建它的一个实例(通过Secondary secondary = new Secondary()),然后调用secondary.hashText()(也使 hashText 非静态):)

      【讨论】:

      • 谢谢您,我尝试应用您的建议未成功。
      • 如果你愿意,你可以给我更多关于你的代码的信息,这样我可以提供更好的帮助:)
      【解决方案5】:

      因此,如果您需要两个单独的类,请记住每个类必须位于单独的文件中。你需要像这样在第一个类中导入第二个类:

      import YourPackageName.YourClassName;
      

      然后在主类中你可以这样做:

      public class Primary {
        public static void main(String[] args) throws Exception {
            String clearText = ("");
      
            System.out.print("Type Text");
            clearText = scnr.next();
      
         Secondary secondClass = new Secondary();
         String hashText = secondClass.hash(clearText);
      
            System.out.println(hashText);
         }
      }
      

      同时在中学课程:

      public class Secondary {
      
         public String hash (String clearText){
            //doHash
         }
      
      }
      

      【讨论】:

      • 类绝对没有必须在单独的文件中。他们甚至不需要在其他班级之外。
      • 我误读了这个问题,我以为他希望它们位于单独的文件中。对不起
      • 没问题只是不想让他糊涂。
      • 谢谢,我确实需要它在单独的文件中。但是,我确实尝试应用您的建议未成功。这更多地反映了我应用您的回复的能力,而不是回复本身。再次感谢。
      猜你喜欢
      • 2012-11-19
      • 2017-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-26
      • 2012-09-30
      相关资源
      最近更新 更多