我对Java很陌生我想要做的事情可能看起来很奇怪但是我对Java的工作原理有点了解,而不是实际完成设定结果。
如果我有一个布尔方法,例如:
public class doThings
{
// imagine the intial value of this variable is set
// and or modified using getters and setters between the declaration
// and the following method
private boolean boolVar;
public boolean doSomething()
{
if(boolVar == true)
{
//does things and then returns true
return true;
}
else
{
return false;
}
}
}
我想在另一个类中调用此方法,就像这样......
public class testDoThings
{
doThings someObject = new doThings;
public static void main(String[] args)
{
someObject.doSomething()
}
}
如何检查(在类testDoThings
中)以查看方法是否返回true或返回false并相应地打印消息...
public class testDoThings
{
doThings someObject = new doThings;
public static void main(String[] args)
{
someObject.doSomething()
if (doSomething() returns true) // yes I am aware this is not
//valid
{
// print a statment
}
else if (doSomething() returns false) // yes once again not valid
{
// print a different statement
}
}
}
我知道您可以将这些消息放在包含该方法的类中,但是如果我想要根据调用方法的位置以及调用它的方式而需要不同的消息,那么将内容放入原始类方法并不总是如此工作。
如果我完全不在这里,请告诉我,如果有人能向我解释这一点,那就太棒了!
答案 0 :(得分:2)
您可以尝试这样:
# ===============================
# = DATA SOURCE
# ===============================
# Connection url for the database connection
spring.datasource.url = jdbc:mysql://localhost:27017/purchase_books
# Username and password
spring.datasource.username = root
spring.datasource.password = root
# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1
# ===============================
# = JPA / HIBERNATE
# ===============================
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update): with "update" the database
# schema will be automatically updated accordingly to java entities found in the project
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# Allows Hibernate to generate SQL optimized for a particular DBMS
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
答案 1 :(得分:1)
您可以尝试这样的事情:
if(someObject.doSomething()){
System.out.print("foo1");
}
else{
System.out.print("foo2");
}
答案 2 :(得分:0)
这是一种方式:
if(someObject.doSomething() == true){
System.out.print("foo1");
}
else{
System.out.print("foo2");
}
答案 3 :(得分:0)
通常,您使用==
运算符比较两件事:if (x == y) ...
所以我们有:
if ( someObject.doSomething() == true ) {
//statements
} else {
//statement for the case where result of method call is false
}
BTW代替if(x == true)
,您只需撰写if(x)
。
答案 4 :(得分:-1)
if,while,do ...等条件结构会收到一个布尔值,因此没有必要输入“boolVar == true”。只做“if(boolVar)”就足够了。至于doSomething方法中的示例,只需执行“return boolVar”;除非你假装在它上面做更多的事情,否则我会做这项工作而不需要任何ifs。
检查函数返回值的工作方式是否相同。我的意思是,变量也有一个值和函数,唯一的区别是变量在函数计算或生成值时保持一个值。所以,在你的代码中:
public class testDoThings {
public void check() {
doThings someObject = new doThings();
boolean flag = sameObject.doSomething();
if (flag) {
// print a statment
} else {
//If you want to check falsehood, !flag would do.
// Notice the "!" sign before
// the flag variable?
// It is a NOT sign, so
// NOT true = false
// NOT false = true
// The if will execute its code
// When the result value is true
// So when the function return val is
// false.
// print a different statement
}
}
}
我希望这个解释足够明确。