如何在 Java 中调用类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4018236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call a class within Java
提问by Hammer
have two strings, String1 = hello String2 = world, I want to call a class Hello and send to the two strings. The class should return a boolean value and a string. If the boolean is true it should do the followig:
有两个字符串,String1 = hello String2 = world,我想调用一个类Hello并发送给两个字符串。该类应该返回一个布尔值和一个字符串。如果布尔值为真,则应执行以下操作:
System.out.println("Hello to you too!");
Can someone help me out with this code?
有人可以帮我解决这个代码吗?
回答by VoteyDisciple
First, a terminology problem: you cannot "call a class." You can call a methodon a class, such as:
首先,一个术语问题:你不能“调用一个类”。您可以在类上调用方法,例如:
someObject.someMethod(string1, string2);
More to the point, you can't return two different values from a method. You could certainly store two different values in the object and return them from different methods, though. Perhaps a class like:
更重要的是,您不能从一个方法返回两个不同的值。不过,您当然可以在对象中存储两个不同的值并从不同的方法返回它们。也许像这样的类:
public class Foo {
protected boolean booleanThing;
protected String stringThing;
public void yourMethod(String string1, String string2) {
// Do processing
this.booleanThing = true;
this.stringThing = "Bar";
}
public String getString() {
return this.stringThing;
}
public boolean getBoolean() {
return this.booleanThing;
}
}
Which would be used as:
将用作:
someObject.yourMethod(string1, string2);
boolean b = someObject.getBoolean();
String s = someObject.getString();
Having said all that, though, this may not at all be the best way to solve your actual problem. Perhaps you can explain better what you're trying to accomplish. Perhaps throwing an Exception
is better than trying to return a boolean, or perhaps there's another solution entirely.
尽管如此,这可能根本不是解决实际问题的最佳方法。也许您可以更好地解释您要实现的目标。也许抛出一个Exception
比尝试返回一个布尔值更好,或者可能还有另一个解决方案。
The more detail we have, the better.
我们拥有的细节越多越好。
回答by Valchris
You should review your definition of classes but for now I'll assume this is what you meant, comment if this isn't what your looking for:
您应该查看您对类的定义,但现在我假设这就是您的意思,如果这不是您想要的,请发表评论:
public class Hello {
private final String first;
private final String second;
public static void main(String[] args) {
String s1 = "Hello";
String s2 = "World";
Hello h = new Hello(s1,s2);
if(h.isHelloWorld()) {
System.out.println("Hello to you too!");
}
}
private Hello(String first, String second) {
this.first = first;
this.second = second;
}
private boolean isHelloWorld() {
return (first.equals("Hello") && second.equals("World"));
//If that scares you then do this instead:
/**
if(first.equals("Hello") && second.equals("World") {
return true;
} else { return false; }
**/
}
}
When you run this program it will always print "Hello to you too!", if you change s1 or s2 it won't print anything.
当你运行这个程序时,它总是会打印“Hello to you too!”,如果你改变 s1 或 s2 它不会打印任何东西。
回答by Orbit
public class Hello{
boolean val = false;
String str = "";
public Hello(String a, String b){
if(a == "hello" && b == "world"){
this.val = true;
this.str = "hello to you too";
}
}
public static void main(String args[]){
String a = "hello";
String b = "world";
Hello hello = new Hello(a,b);
if(hello.val == true)
System.out.println(hello.str);
}
}