- class Base
- {
- int count=2;
- public void display()
- {
- System.out.println(this.count);
- }
- }
- class Derived extends Base
- {
- int count=30;
- public void display()
- {
- System.out.println(this.count);
- }
- }
- public class FieldAndMethodTest {
- public static void main(String s[])
- {
- Base db= new Derived();
- Derived d=new Derived();
- System.out.println(db.count);
- db.display();
- Base dc=d;
- System.out.println(dc.count);
- //有结果可以看出java继承在处理成员变量和成员方法时是有区别的。
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/281120137568.html
来源: http://www.codesnippet.cn/detail/281120137568.html