[英]I have Class file which has multiple methods, Can i invoke the class from Testcase
I have a class called TestingClass
and I have a one more class called TestCase
.我有一个名为
TestingClass
的类,我还有一个名为TestCase
类。
In TestingClass
I have 4 methods which are dependent on each other, Can i call the this class file from my TestCase
file and invoke all the methods at once?在
TestingClass
我有 4 个相互依赖的方法,我可以从我的TestCase
文件中调用这个类文件并一次调用所有方法吗?
You can try with extend the TestingClass
and overwriting those methods as like,您可以尝试扩展
TestingClass
并像这样覆盖这些方法,
public class TestCase extends TestingClass {
@Override
public void method1(){
super.method1();
}
@Override
public void method2(){
super.method2();
}
@Override
public void method3(){
super.method3();
}
@Override
public void method4(){
super.method4();
}
}
Yes you can if they have visibility.是的,如果他们有可见性,你可以。
In your TestCase
you have to declarate a variable of TestingClass
and instantiate it.在您的
TestCase
您必须声明一个TestingClass
变量并实例化它。 Then, if your methods are public
you can use them.然后,如果您的方法是
public
您就可以使用它们。
Here is an example:下面是一个例子:
public class TestCase {
private TestingClass testingClass = new TestingClass();
@Test
public void test1(){
testingClass.methodFromTestCase();
}
}
There is another way to do it, refer below example:还有另一种方法可以做到这一点,请参阅以下示例:
package overridefunction;
public class SuperClass
{
public void method1()
{
System.out.println("superclass method1");
this.method2();
this.method3();
this.method4();
this.method5();
}
public void method2()
{
System.out.println("superclass method2");
}
private void method3()
{
System.out.println("superclass method3");
}
protected void method4()
{
System.out.println("superclass method4");
}
void method5()
{
System.out.println("superclass method5");
}
}
package overridefunction1;
public class SubClass extends SuperClass
{
@Override
public void method1()
{
System.out.println("subclass method1");
super.method1();
}
}
package overridefunction1;
public class Demo
{
public static void main(String[] args)
{
SubClass mSubClass = new SubClass();
mSubClass.method1();
}
}
subclass method1
superclass method1
superclass method2
superclass method3
superclass method4
superclass method5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.