JAVA导入不同包的同名类调用原则小结
使用不同包的同名类系统默认顺序:这里将在com.testpackage包中尝试导入com.a.Test和com.B.Test,分析调用Test的具体包。Test作为此次测试不同导入包的相同类名。·将要导入的包程序com.a包编写以下程序:package com.a;public class Test {public int num1=1;}com.b包编写以下程序:pac...
使用不同包的同名类系统默认顺序:
这里将在com.testpackage包中尝试导入com.a.Test和com.B.Test,分析调用Test的具体包。
Test作为此次测试不同导入包的相同类名。
·将要导入的包程序
com.a包编写以下程序:
package com.a;
public class Test {
public int num1=1;
}
com.b包编写以下程序:
package com.b;
public class Test {
public int num2=2;
}
·com.text中情况分类
1.一般情况——使用声明更具体的包中的类
package com.testpackage;
import com.a.*;
import com.b.Test;
public static void main(String[] args){
Test a=new Test();
Test b=new Test();
System.out.println(b.num2);
//a和b两个对象只能调用com.b包中的属性
System.out.println(a.num2);
}
/*
*此程序输出:
*2
*2
*/
a和b两个对象都调用的是com.b包中的Test方法,即采用完整具体类名声明包的类。
2.导入包声明程度一样具体——按顺序使用第一个
package com.testpackage;
import com.a.Test;
import com.b.Test;
public static void main(String[] args){
Test a=new Test();
Test b=new Test();
System.out.println(b.num1);
//a和b两个对象只能调用com.a包中的属性
System.out.println(a.num1);
}
/*
*此程序输出:
*1
*1
*/
a和b两个对象都调用的是com.a包中的Test方法,即具体声明程度相同,使用排序第一导入包中的类。
此程序,系统还会提示:The import com.b.Test collides with another import.
3.不同导入包都采用通配符声明——必须进行选择才可正常
package com.testpackage;
import com.a.*;
import com.b.*;
public static void main(String[] args){
Test a=new Test();
Test b=new Test();
}
系统报错:The type Test is ambiguous.
系统无法确定使用哪一个类,此时如果继续使用其中一个对象的属性,可选择num1或num2的属性,选定后系统自动增加具体声明语句,此时两个对象均使用这个具体声明包的方法。
比如选择a使用com.b.Test方法中num2属性:
package com.testpackage;
import com.a.*;
import com.b.*;
import com.b.Test;
public class TestPackage {
public static void main(String[] args) {
Test a=new Test();
Test b=new Test();
System.out.println(a.num2);
//选择后即自动增加import com.b.Test;此时b对象只能选择com.b.Test方法
System.out.println(b.num2);
}
}
/*
*此程序输出:
*2
*2
*/
4.不同导入包都采用通配符声明,主程序内采用完整包名新建一个对象——必须选择且只能一个导入包使用
package com.testpackage;
import com.a.*;
import com.b.*;
public class TestPackage {
public static void main(String[] args) {
Test a=new Test();
com.b.Test b=new Test();
}
}
依旧提示:The type Test is ambiguous.
与上述情况3一样,需选择,此时必须都选择有完整类名的num2属性,即只能导入con.b.Test;否则com.b.Test b=new Test();将报错:Type mismatch: cannot convert from com.a.Test to com.b.Test.
package com.testpackage;
import com.a.*;
import com.b.*;
public class TestPackage {
public static void main(String[] args) {
Test a=new Test();
com.b.Test b=new Test();
System.out.println(a.num2);
System.out.println(b.num2);
}
}
/*
*此程序输出:
*2
*2
*/
4.第一个导入包声明具体,第二个导入包加通配符声明,主程序内完整包名导入——报错
package com.testpackage;
import com.a.Test;
import com.b.*;
public static void main(String[] args){
Test a=new Test();
com.b.Test b=new Test();
//此句系统报错:Type mismatch: cannot convert from com.a.Test to com.b.Test
}
此种情况不存在。
5.两个具体声明的导入包,主程序内第二个包用完整类名新建对象——报错
package com.testpackage;
import com.a.Test;
import com.b.Test;
public class TestPackage {
public static void main(String[] args) {
Test a=new Test();
com.b.Test b=new Test();
//此句系统报错:Type mismatch: cannot convert from com.a.Test to com.b.Test
}
}
此种情况也不存在。
6.主程序用两个完整包名新建对象——报错
- 不导入包
package com.testpackage;
public class TestPackage {
public static void main(String[] args) {
com.a.Test a=new Test();
//系统报错:Test cannot be resolved to a type
com.b.Test b=new Test();
//Multiple markers at this line
// - Type mismatch: cannot convert from com.a.Test to com.b.Test
}
}
- 导入包含完整类名的包
package com.testpackage;
import com.a.Test;
import com.b.Test;
//系统报错:he import com.b.Test collides with another import statement
public class TestPackage {
public static void main(String[] args) {
com.a.Test a=new Test();
com.b.Test b=new Test();
//系统报错:Type mismatch: cannot convert from com.a.Test to com.b.Test
}
}
- 导入包均包含通配符
package com.testpackage;
import com.a.*;
import com.b.*;
public class TestPackage {
public static void main(String[] args) {
com.a.Test a=new Test();
//系统报错:The type Test is ambiguous
com.b.Test b=new Test();
//系统报错:Multiple markers at this line
// - The type Test is ambiguous
// - Type mismatch: cannot convert from com.a.Test to
}
}
小结
在com.testpackage中只能使用特定的一个导入包中的类名Test:只能使用com.a.Test或者只能使用com.b.Test。
一个类的方法中不可使用多个不同导入包中的相同类名。
更多推荐

所有评论(0)