简体   繁体   English

我有包含多种方法的类文件,我可以从 Testcase 调用该类吗

[英]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.

相关问题 我可以在 class 中有多个运行方法吗? - Can I have multiple run methods in a class? 我可以在单个类文件中在 java 中定义多个自定义异常并通过方法调用它们吗? - Can I define multiple custom exceptions in java in a single class file and invoke them via methods? 如何测试调用父类受保护(不需要的)方法的方法? - How can I test a method which invoke protected (unwanted) methods of parent class? 我可以使一个类只有方法吗? - Can I make a Class just has methods? 我们可以创建一个不能从类中调用任何方法的类(超类或子类)的对象吗? - Can we create an object of a class (super or sub class) which cannot invoke any methods from the class? 无法访问我在其他班级上创建的方法 - Unable to access methods which i have created on other Class 如果我可以在创建者 class 中拥有多个工厂方法,为什么我还需要抽象工厂模式? - If I can have multiple factory methods in a creator class, why would I ever need the abstract factory pattern? 如何在 class 中使用另一个 class 的方法? - How can I use methods from another class in a class? 如何从另一个jar文件调用一个类? - How do I invoke a class from another jar file? 我可以将输入法从main方法调用到类中的另一个方法中吗 - Can I invoke input from main method into another method in the class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM