关于静态与非静态?感到迷惑!

helen823 2008-04-22 11:15:13
class A
{
int i=10;

static void show()
{
System.out.println("show() in A");
}

void show1()
{
System.out.println("show1() in A");
}
}
class B extends A
{
static int i=20;

static void show()
{
System.out.println("show() in B ");
}
}

class C extends A
{
int i=30;

void show1()
{
System.out.println("show1() in c");
}

}

class TestStatic
{
public static void main(String[] args)
{
A aa=new B();
A ab=new C();
aa.show();
aa.show1(); //1

ab.show();
ab.show1(); //2


}
}


我想问的是:1:为什么静态方法只能够覆盖静态方法,非静态只能覆盖非静态方法?而成员变量却可以用静态覆盖非静态,非静态覆盖静态的?
2:当两个类为is-a的关系的时候,调用静态方法的时候,为什么1处和2处的结果会不一样?
...全文
128 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
wanhao3g 2008-04-22
  • 打赏
  • 举报
回复
1:为什么静态方法只能够覆盖静态方法,非静态只能覆盖非静态方法?而成员变量却可以用静态覆盖非静态,非静态覆盖静态的?

静态方法不能够被覆盖,你可以看到你的aa.show();ab.show();调用的都是基类A的方法。对于static方法编译器可以准确地知道调用那个方法,这种调用方式称为静态绑定。

静态变量也不能够被非静态变量隐藏,反之一样,只不过是调用机制影响调用罢了。
2:当两个类为is-a的关系的时候,调用静态方法的时候,为什么1处和2处的结果会不一样?
事先说明一下你并没有调用静态方法。
支持一楼
aa.show1(); //1继承了A中的方法
ab.show1(); //2执行时动态绑定C中的覆盖了A中的show1()方法。
class A
{
int i=10;
static int j =10;
static int n=11;
static void show()
{
System.out.println("show() in A");
}

void show1()
{
System.out.println("show1() in A");
}
}
class B extends A
{
static int i=20;

static void show()
{
System.out.println("show() in B ");
}
}

class C extends A
{
int i=30;
int j=2;
static int n=23;
void show1()
{
System.out.println("show1() in c");
}

}

public class Test3
{
public static void main(String[] args)
{
A aa=new B();
A ab=new C();
C cc=new C();
aa.show(); //A类的方法
aa.show1(); //A类的方法

ab.show();//A类的方法
System.out.println(cc.n);//可以看出static变量n被被隐藏了
System.out.println(ab.n);//可以看出static变量j并没有被隐藏
System.out.println(ab.j);//可以看出static变量j并没有被隐藏
ab.show1(); //2执行时调用C类的方法


}
}

goosman 2008-04-22
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 gongyali2005 的回复:]
静态方法不能被重写的.

class A
{
int i=10;

static void show()
{
System.out.println("show() in A");
}

void show1()
{
System.out.println("show1() in A");
}
}


两个方法输出内容都一样.是不是这个在做怪.
[/Quote]
在楼主的例子里,静态方法都已经被重写了,只不过没有调用.
class TestStatic//把楼主的主类改成这样,你运行看看是不是被重写了.
{
public static void main(String[] args)
{
A aa=new B();
A ab=new C();
B.show();
aa.show1(); //1

C.show();
ab.show1(); //2


}
}
helen823 2008-04-22
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 gongyali2005 的回复:]
楼主看看多态吧.

重写,重载.

http://blog.csdn.net/faintbear/archive/2004/12/16/218088.aspx
[/Quote]
这些题是非静态的和静态的是有区别的,如果是静态的方法,那答案就不是那个了
gongyali2005 2008-04-22
  • 打赏
  • 举报
回复
楼主看看多态吧.

重写,重载.

http://blog.csdn.net/faintbear/archive/2004/12/16/218088.aspx
jiazhengjing 2008-04-22
  • 打赏
  • 举报
回复
静态方法是在编译的时候把静态方法和类的引用类型进行匹配,而不是在运行的时候和类引用进行匹配。所以在子类中创建的静态方法,它并不会覆盖父类中相同名字的静态方法。
oogamiyiqilou 2008-04-22
  • 打赏
  • 举报
回复
1楼说得对
helen823 2008-04-22
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 anqini 的回复:]
问题2:这就是多态,执行期辨别机制,虽然你上向转型至A,但是它没有丢掉自己原来的类型,执行的时候还是根据你原来类型来执行!
[/Quote]
那为什么如果是非静态的方法的时候,调用的是运行期对象的方法,而静态方法却调用的是编译期的?
helen823 2008-04-22
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 gongyali2005 的回复:]
静态方法不能被重写的.
[/Quote]
是可以的,不相信你试一下
gongyali2005 2008-04-22
  • 打赏
  • 举报
回复
静态方法不能被重写的.

class A
{
int i=10;

static void show()
{
System.out.println("show() in A");
}

void show1()
{
System.out.println("show1() in A");
}
}


两个方法输出内容都一样.是不是这个在做怪.
anqini 2008-04-22
  • 打赏
  • 举报
回复
问题2:这就是多态,执行期辨别机制,虽然你上向转型至A,但是它没有丢掉自己原来的类型,执行的时候还是根据你原来类型来执行!
helen823 2008-04-22
  • 打赏
  • 举报
回复
那就是说java中的成员变量都是在静态绑定的,方法只有static和final是静态绑定的. 那:

class A
{

static void show()
{
System.out.println("show() in A");
}

}
class B extends A
{

static void show()
{
System.out.println("show() in B ");
}
}

这究竟算不算覆盖!
anqini 2008-04-22
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 helen823 的回复:]
引用 1 楼 anqini 的回复:
问题2:这就是多态,执行期辨别机制,虽然你上向转型至A,但是它没有丢掉自己原来的类型,执行的时候还是根据你原来类型来执行!

那为什么如果是非静态的方法的时候,调用的是运行期对象的方法,而静态方法却调用的是编译期的?
[/Quote]

比如说
A a = new B(); a.test();//假如test是静态方法!
静态方法是类的信息不是对象的信息,简单明了说,静态方法是静态绑定,就是编译期判断它将执行哪个方法,非静态方法是动态绑定,执行期盼段执行哪个方法!

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧