你能不能告诉我,通常的做法是编写一个名为JUnit Test的方法,例如抛出异常
class A {
public String f(int param) throws Exception {
if (param == 100500)
throw new Exception();
return "";
}
}
private A object = new A();
@Test
public void testSomething() throws Exception {
String expected = "";
assertEquals(object.f(5), expected);
}
Run Code Online (Sandbox Code Playgroud)
实际上,方法f()不会为该参数抛出异常(5)但是我必须声明该异常.
假设我有2个字段的类:x和y,类型double.是否可以定义2个构造函数,因此constructor1将创建对象,将其x属性设置为构造函数中的哪个参数告诉y默认值,构造函数2反之亦然?
public class Test {
private int x;
private int y;
public Test(int x) {
this.x = x;
}
public Test(int y) {
this.y = y;
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试这样的东西,我知道它因为超载规则而无法工作
不知道怎么更正确地问,所以会尽我所能.
我们正在搜索一个示例字符串,另一个模式字符串:
我需要一些方法来弄清楚示例是否包含模式(我想到的是像example.indexOf(pattern)> - 1)
Pattern = "b*b"
Example = "gjsdng" - false;
Example = "bob" - true;
Example = "bab" - true;
Example = "gdfgbUbfg" - true;
Run Code Online (Sandbox Code Playgroud) 有一种情况:
包 pak1 包含一些类
package pak1;
public class A {
public void g() {}
}
Run Code Online (Sandbox Code Playgroud)
和另一个包 pak2
package pak2;
public class B {
public void f() {
// here I want to call method g() from class A
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法在不导入类(然后)的情况下调用类的A方法?g()Anew A().g()
如果方法 g() 是静态的,我可以写
public void f() {
pak1.A.g();
}
Run Code Online (Sandbox Code Playgroud) java ×4
constructor ×1
exception ×1
junit ×1
overloading ×1
package ×1
regex ×1
string ×1
visibility ×1