mač*_*ček 17 java methods overloading
例如,我想创建一个可以返回任何数字(负数,零或正数)的函数.
但是,基于某些例外情况,我希望函数返回 Boolean FALSE
有没有办法写一个可以返回一个int 或一个Boolean?的函数?
好的,所以这收到了很多回复.我理解我只是错误地解决了这个问题而且我应该throw在方法中使用某种异常.为了得到更好的答案,我将提供一些示例代码.请不要取笑:)
public class Quad {
public static void main (String[] args) {
double a, b, c;
a=1; b=-7; c=12;
System.out.println("x = " + quadratic(a, b, c, 1)); // x = 4.0
System.out.println("x = " + quadratic(a, b, c, -1)); // x = 3.0
// "invalid" coefficients. Let's throw an exception here. How do we handle the exception?
a=4; b=4; c=16;
System.out.println("x = " + quadratic(a, b, c, 1)); // x = NaN
System.out.println("x = " + quadratic(a, b, c, -1)); // x = NaN
}
public static double quadratic(double a, double b, double c, int polarity) {
double x = b*b - 4*a*c;
// When x < 0, Math.sqrt(x) retruns NaN
if (x < 0) {
/*
throw exception!
I understand this code can be adjusted to accommodate
imaginary numbers, but for the sake of this example,
let's just have this function throw an exception and
say the coefficients are invalid
*/
}
return (-b + Math.sqrt(x) * polarity) / (2*a);
}
}
Run Code Online (Sandbox Code Playgroud)
Pab*_*ruz 13
不,你不能用Java做到这一点.
你可以回来一个Object.通过返回一个对象,你可以在技术上返回派生类,如java.lang.Integer或java.lang.Boolean.但是,我不认为这是最好的主意.
his*_*ess 11
你可以在技术上这样做:
public <T> T doWork()
{
if(codition)
{
return (T) new Integer(1);
}
else
{
return (T) Boolean.FALSE;
}
}
Run Code Online (Sandbox Code Playgroud)
然后这段代码将编译:
int x = doWork(); // the condition evaluates to true
boolean test = doWork();
Run Code Online (Sandbox Code Playgroud)
但是,如果方法返回错误的类型,您肯定会遇到运行时异常.您还必须返回对象而不是基元,因为T被擦除为java.lang.Object,这意味着返回的类型必须扩展Object(即作为对象).上面的示例使用自动装箱来实现原始返回类型.
我当然不会推荐这种方法,因为IMO需要评估您对异常处理的使用.如果您可以对该异常执行某些操作(即恢复,保留,重试等),则会在特殊情况下捕获异常.例外是预期工作流程的例外,而不是其中的一部分.
没有.您可以做的最好的事情是返回一个类的实例,该类处理您可能想要返回的所有内容.
就像是
public class ReturnObj {
public bool yesno; // yes or no
public int val; // for int values
public String mode; // mode describing what you returned, which the caller will need to understand.
}
Run Code Online (Sandbox Code Playgroud)
显然,你需要玩名字....
此外,这似乎是一种代码味道.您可以通过在函数之外限定所需的路径来删除执行此类操作的需要,然后调用特定函数以获取布尔值或特定函数以获取int,具体取决于限定条件.
大多数语言将处理没有异常或联合类型的特定示例,因为 IEEE 浮点包括 NaN 的表示。在 Java 中,使用 Double.NaN:
public static double quadratic(double a, double b, double c, int polarity) {
double x = b*b - 4*a*c;
// When x < 0, Math.sqrt(x) retruns NaN
if (x < 0) {
return Double.NaN;
}
return (-b + Math.sqrt(x) * polarity) / (2*a);
}
Run Code Online (Sandbox Code Playgroud)
这会产生您想要的确切输出:
x = 4.0
x = 3.0
x = NaN
x = NaN
Run Code Online (Sandbox Code Playgroud)
例外是解决类似问题的旧 Java 方法:
public static double quadratic(double a, double b, double c, int polarity) {
double x = b*b - 4*a*c;
// When x < 0, Math.sqrt(x) returns NaN
if (x < 0) {
throw new Exception("NaN")
}
return (-b + Math.sqrt(x) * polarity) / (2*a);
}
Run Code Online (Sandbox Code Playgroud)
这是您的异常客户端代码。
a=1; b=-7; c=12;
// x = 4.0
try {
System.out.println("x = " + quadratic(a, b, c, 1));
} catch (Exception iae) {
System.out.println("Oopsie: " + iae.getMessage());
}
// x = 3.0
try {
System.out.println("x = " + quadratic(a, b, c, -1));
} catch (Exception iae) {
System.out.println("Oopsie: " + iae.getMessage());
}
// "invalid" coefficients.
a=4; b=4; c=16;
// Oopsie: NaN
try {
System.out.println("x = " + quadratic(a, b, c, 1));
} catch (Exception iae) {
System.out.println("Oopsie: " + iae.getMessage());
}
// Oopsie: NaN
try {
System.out.println("x = " + quadratic(a, b, c, -1));
} catch (Exception iae) {
System.out.println("Oopsie: " + iae.getMessage());
}
Run Code Online (Sandbox Code Playgroud)
要真正向方法传递或从方法返回无关类型,您需要 Java 并不真正支持的联合类型。但是 Paguro 提供了可以在 Java 中使用的联合类型(使用 Or):
public static Or<Double,String> quadratic(double a, double b,
double c, int polarity) {
double x = b*b - 4*a*c;
// When x < 0, Math.sqrt(x) retruns NaN
if (x < 0) {
return Or.bad("NaN");
}
return Or.good((-b + Math.sqrt(x) * polarity) / (2*a));
}
@Test public void testQuadradic() {
double a, b, c;
a=1; b=-7; c=12;
// x = Good(4.0)
System.out.println("x = " + quadratic(a, b, c, 1));
// x = 3.0
System.out.println(
(String) quadratic(a, b, c, -1)
.match(good -> "x = " + good,
bad -> "Oopsie: " + bad));
// "invalid" coefficients.
a=4; b=4; c=16;
// x = Bad("NaN")
System.out.println("x = " + quadratic(a, b, c, 1));
// Oopsie: NaN
System.out.println(
(String) quadratic(a, b, c, -1)
.match(good -> "x = " + good,
bad -> "Oopsie: " + bad));
}
Run Code Online (Sandbox Code Playgroud)
对于您的具体示例,只需使用浮点。对于更通用的解决方案,我发现联合类型比异常更有用。您可以使用联合类型作为方法的参数,该方法可能采用没有公共接口或祖先的两个不同输入。它们对函数式编程也更友好。
| 归档时间: |
|
| 查看次数: |
66668 次 |
| 最近记录: |