如何从另一个类扩展类(Java)

时间:2022-11-28 16:00:02

I have two questions:

我有两个问题:

1) Is there a way to extend of class A from external file ? how?

1)有没有办法从外部文件扩展A类?怎么样?

2) I am building one class of my custom methods ( to use globally, in all my projects). Here is phseudo-code:

2)我正在构建一类自定义方法(在我的所有项目中全局使用)。这是伪代码:

package MyFunctions;
import Twitter.profile;

public class MyFuncs{
    public String externalProfile1()  { return Twitter.profile.TwitterUrl(); }
}

I want Is there a way to include that file in all my projects, and avoid IDE errors, as I should be able to use any when one of the above functions in my projects... The problem is that the Twitter.Profile classes are not included in all my projects, and whenever happens so - it see error in IDE("cannot find symbol method")...

我想有没有办法在我的所有项目中包含该文件,并避免IDE错误,因为我应该能够在我的项目中使用任何上述函数之一...问题是Twitter.Profile类是不包括在我的所有项目中,并且无论何时发生 - 它在IDE中看到错误(“找不到符号方法”)...

how to solve the problem?

怎么解决这个问题?

1 个解决方案

#1


1  

Question 2:

Just make sure all of your functions in your library are written statically:

只需确保库中的所有功能都是静态编写的:

public class MyFuncs{

    public static String externalProfile1(String link)     { return TwittUrl() + "/profile"; }
    public static String externalProfile2(String link)     { return YahooUrl() + "/profile"; }
}

And then import that class in your project files that you'll be using your library in. Then you can easily call the functions in your library. Alternatively, you can avoid importing the library in every other file and instead call the functions in a static way:

然后在您将使用库的项目文件中导入该类。然后,您可以轻松调用库中的函数。或者,您可以避免在每个其他文件中导入库,而是以静态方式调用函数:

MyFuncs.externalProfile1("link");

As for TwittUrl(), if it doesn't require to be in a separate Class, then refactor it and put it in MyFuncs class; otherwise you can make TwittUrl() and YahooUrl() methods static in their own class.

至于TwittUrl(),如果它不需要在一个单独的类中,那么重构它并将它放在MyFuncs类中;否则你可以在自己的类中使TwittUrl()和YahooUrl()方法静态化。

#1


1  

Question 2:

Just make sure all of your functions in your library are written statically:

只需确保库中的所有功能都是静态编写的:

public class MyFuncs{

    public static String externalProfile1(String link)     { return TwittUrl() + "/profile"; }
    public static String externalProfile2(String link)     { return YahooUrl() + "/profile"; }
}

And then import that class in your project files that you'll be using your library in. Then you can easily call the functions in your library. Alternatively, you can avoid importing the library in every other file and instead call the functions in a static way:

然后在您将使用库的项目文件中导入该类。然后,您可以轻松调用库中的函数。或者,您可以避免在每个其他文件中导入库,而是以静态方式调用函数:

MyFuncs.externalProfile1("link");

As for TwittUrl(), if it doesn't require to be in a separate Class, then refactor it and put it in MyFuncs class; otherwise you can make TwittUrl() and YahooUrl() methods static in their own class.

至于TwittUrl(),如果它不需要在一个单独的类中,那么重构它并将它放在MyFuncs类中;否则你可以在自己的类中使TwittUrl()和YahooUrl()方法静态化。