gpt4 book ai didi

salesforce - 如何在apex中将一个类的方法调用到另一个类中

转载 作者:行者123 更新时间:2023-12-04 00:52:24 25 4
gpt4 key购买 nike

我想将一个类的方法用到另一个类上。

     eg: public class controller 1{
public void method 1(){}
}


public class controller 2{
public void method 2() { }
}

我想在 Controller 2 中使用方法 1。请帮我找到解决办法

最佳答案

您可以使用两种方法:

1.使用静态方法

你不能在这里使用 controller2 实例方法

public class controller2 
{
public static string method2(string parameter1, string parameter2) {
// put your static code in here
return parameter1+parameter2;
}
...
}

在单独的类文件中调用method2()

// this is your page controller
public class controller1
{
...
public void method1() {
string returnValue = controller2.method2('Hi ','there');
}
}

2.创建另一个类的实例

public class controller2 
{
private int count;
public controller2(integer c)
{
count = c;
}

public string method2(string parameter1, string parameter2) {
// put your static code in here
return parameter1+parameter2+count;
}
...
}

public class controller1
{
...
public void method1()
{
controller2 con2 = new controller2(0);
string returnValue = con2.method2('Hi ','there');
}
}

如果您的方法在具有命名空间的包中

string returnValue = mynamespace.controller2.method2();

关于salesforce - 如何在apex中将一个类的方法调用到另一个类中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12139737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com