Java平台相关类继承
我开发了一个Java库,它将运行在两个不同的平台上。要打印消息,一个平台使用Java平台相关类继承,java,import,conditional,conditional-compilation,Java,Import,Conditional,Conditional Compilation,我开发了一个Java库,它将运行在两个不同的平台上。要打印消息,一个平台使用printA(str)方法,而另一个平台使用printB(str)方法。在C++中,我将创建一个静态方法: public static void print(string str) { #ifdef platformA printA(str); #else printB(str); #endif } 由于Java没有#ifdef,因此它成为一项棘手的任务。我开始考
printA(str)
方法,而另一个平台使用printB(str)
方法。在C++中,我将创建一个静态方法:
public static void print(string str)
{
#ifdef platformA
printA(str);
#else
printB(str);
#endif
}
由于Java没有#ifdef
,因此它成为一项棘手的任务。我开始考虑用静态方法重写抽象类,但不确定方向是否正确。最优雅的方式是什么
编辑:安迪·托马斯的回答(谢谢!)让我找到了适合我的解决方案。唯一的缺点是,它必须在启动时初始化。下面是代码。 公共图书馆:
//interface is only for internal use in Output class
public interface IPrintApi
{
public void Print(String message);
}
public abstract class Output
{
private static IPrintApi m_api;
public static void SetPrintAPI(IPrintApi api)
{
m_api=api;
}
public static void MyPrint(String message)
{
m_api.Print(message);
}
}
此函数的调用与通用库和平台特定代码中的调用相同:
public class CommonTest
{
public CommonTest()
{
Output.MyPrint("print from library");
}
}
每个平台的代码必须具有特定于平台的接口实现,例如platformA(对于B,它是相同的):
用法:
public class AppPlatformA
{
public static void main(String[] args)
{
// point the abstract Output.Print function to the available implementation
OutputA myPrintImpl = new OutputA();
Output.SetPrintAPI(myPrintImpl);
// and now you can use it!
Output.MyPrint("hello world!");
}
}
你可以用这个
定义一次接口,并在操作系统特定的类中实现它
public interface IPlatformAPI {
public void print( String str );
}
public class WindowsPlatformAPI implements IPlatformAPI {
public void print( String str ) { ... }
}
public class MacPlatformAPI implements IPlatformAPI {
public void print( String str ) { ... }
}
public class LinuxPlatformAPI implements IPlatformAPI {
public void print( String str ) { ... }
}
public class PlatformAPI {
public static IPlatformAPI getPlatformAPI() {
// return one of the platform APIs
// You can use System.getProperty("os.name") to choose the implementation
}
}
使用常量表达式:
private static final boolean PLATFORM_A = true;
public static void print(string str)
{
if(PLATFORM_A )
{
printA(str);
}
else
{
printB(str);
}
}
这个代码呢
public class Example {
public static void main(String[] args) {
print();
}
public static void print() {
String platform = System.getProperty("os.name");
switch (platform) {
case "Windows 7":
System.out.println("This is Windows 7");
break;
case "Windows XP":
System.out.println("This is Windows XP");
break;
}
}
}
[我认为这可以帮助你。这不是运行时检测的问题,而是避免编译时问题的代码设计:当我在platformB中使用库时,printA没有定义,反之亦然……持续使用不是很好。我在两个平台(即Windows和Android)上同时使用的库中使用它。我不想改变每次在公共代码中使用ange const…然后可以使用System.getEnv()并根据环境执行。谢谢,但问题更多的是不同的API调用,而不是如何检测平台。谢谢。这听起来很有趣。唯一不清楚的一点是如何在PlatformAPI类中引用三个不兼容的类,这些类由于不同的plantform依赖包含而无法一起编译。以及在这种情况下,print方法不能被污损为静态的…首先,验证您不能编译针对特定平台的所有Java代码。如果不能,一个选项是在运行时使用
class.forName()
和class.newInstance()加载适当的类顺便说一下,欢迎使用StAdvExcel。您可以通过点击左边的复选标记来接受您认为最好的答案。您可以对您认为有用的多个答案进行投票。我接受这个答案。我必须修改一下您的示例,但现在它按我的需要工作,看起来不错。稍后我会发布一个示例代码。
public class Example {
public static void main(String[] args) {
print();
}
public static void print() {
String platform = System.getProperty("os.name");
switch (platform) {
case "Windows 7":
System.out.println("This is Windows 7");
break;
case "Windows XP":
System.out.println("This is Windows XP");
break;
}
}
}