编译不能通过,Eclipse里提示"The field test.str is ambiguous",即调用该同名变量时,提示该变量不能明确。
对于父类的变量,可以用super.同名变量名来明确,而接口的属性默认隐含为 public static final.所以可以通过C.同名变量名来明确。
要看你的变量的modifiers是什么样的了。
没问题啊 假如a,b,c中都有变量temp. 实例化对象的时候 1.a a = new a(); a.temp 是a本身的变量temp 2.b a = new a(); a.temp 是父类b的变量temp 3.c a = new a(); a.temp 是父接口c的变量temp
如果在A类或测试类中不调用这个同名变量,或者只调用B类中的这个同名变量(比如 new B().通明变量 ),或者B类中同名变量设为私有,那么可以通过编译。否则,编译不能通过,系统无法找到匹配的项,因为在A类中相当于有 两个重名变量。
public class a extends b implements c { /** * @param args */ public a() { super(); } public static void main(String[] args) { // TODO Auto-generated method stub a t=new a(); System.out.print(((b)t).n); System.out.print(((c)t).n); }} class b { int n=0; public b() { n=1; } } interface c{ int n=0; } 这样就不会有歧义了!类型转换
实例化对象的时候
1.a a = new a(); a.temp 是a本身的变量temp
2.b a = new a(); a.temp 是父类b的变量temp
3.c a = new a(); a.temp 是父接口c的变量temp
两个重名变量。
* @param args
*/
public a()
{
super();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
a t=new a();
System.out.print(((b)t).n);
System.out.print(((c)t).n);
}}
class b
{
int n=0;
public b()
{
n=1;
}
}
interface c{
int n=0;
}
这样就不会有歧义了!类型转换