java继承类相互引用加上静态属性的困扰
2008-05-23 16:14
267 查看
在网上看到这样一段代码
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class father
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public String var="father";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static String staticvar="staticfather";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void method()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("father method");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
static void staticmethod()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("father staticmethod");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class son extends father
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public String var="son";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static String staticvar="staticson";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void method()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("son method");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
static void staticmethod()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("son staticmethod");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class goodClass
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static void test()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
father f=new son();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("f.var="+f.var);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("f.staticvar="+f.staticvar);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
f.method();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
f.staticmethod();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Inherit ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param args
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static void main(String[] args) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// TODO Auto-generated method stub
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
goodClass.test();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
结果是:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
f.var=father
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
f.staticvar=staticfather
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
son method
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
father staticmethod
如果去掉属性的static, 结果是不变的
如果给method加上static属性,结果如下
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
f.var=father
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
f.staticvar=staticfather
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
father method ;;已经改变
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
father staticmethod
如果测试时用
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
son f = (son)new father();
编译没问题,但执行时会报cast exception
单步测试结果, 在
father f=new son();
下断点
启动eclipse debug模式, 注意按F5, 即step into
看看整个流程
执行如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
father : public static String staticvar="staticfather";
son: public static String staticvar="staticson";
son: 构造函数 //这里没有
father: 构造函数
构建this指针: 此时debug 的 variables窗口 this下两个var, 此时为null
father: public String var="father"; // 赋值给this第一个var
son : public String var="son"; // 复制给this第二个var
this 指针 赋给 f
完成此句
两个var, 调用f.var的时候取得是第一个, "father"
static属性是全局的,这个不受 f 控制
method不是static就会根据new 后面的对象来指向函数.
再往下有待继续学习.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class father
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public String var="father";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static String staticvar="staticfather";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void method()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("father method");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
static void staticmethod()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("father staticmethod");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class son extends father
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public String var="son";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static String staticvar="staticson";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void method()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("son method");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
static void staticmethod()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("son staticmethod");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class goodClass
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static void test()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
father f=new son();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("f.var="+f.var);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("f.staticvar="+f.staticvar);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
f.method();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
f.staticmethod();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Inherit ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param args
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static void main(String[] args) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// TODO Auto-generated method stub
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
goodClass.test();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
结果是:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
f.var=father
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
f.staticvar=staticfather
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
son method
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
father staticmethod
如果去掉属性的static, 结果是不变的
如果给method加上static属性,结果如下
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
f.var=father
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
f.staticvar=staticfather
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
father method ;;已经改变
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
father staticmethod
如果测试时用
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
son f = (son)new father();
编译没问题,但执行时会报cast exception
单步测试结果, 在
father f=new son();
下断点
启动eclipse debug模式, 注意按F5, 即step into
看看整个流程
执行如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
father : public static String staticvar="staticfather";
son: public static String staticvar="staticson";
son: 构造函数 //这里没有
father: 构造函数
构建this指针: 此时debug 的 variables窗口 this下两个var, 此时为null
father: public String var="father"; // 赋值给this第一个var
son : public String var="son"; // 复制给this第二个var
this 指针 赋给 f
完成此句
两个var, 调用f.var的时候取得是第一个, "father"
static属性是全局的,这个不受 f 控制
method不是static就会根据new 后面的对象来指向函数.
再往下有待继续学习.
相关文章推荐
- 从JVM内存管理的角度谈谈静态方法和静态属性 和 java对象引用与JVM自动内存管理
- java中静态属性和和静态方法的继承问题以及多态的实质
- java继承 父类子类方法调用问题 属性和静态方法是没有多态的
- Java静态属性和方法的继承问题
- Java静态属性、静态方法、非静态属性、非静态方法的继承问题简单了解
- 静态类继承 父类引用子类的属性
- 从JVM内存管理的角度谈谈静态方法和静态属性 和 java对象引用与JVM自动内存管理
- java中静态属性和和静态方法的继承问题 以及多态的实质
- Java静态属性和方法的继承问题
- 从JVM内存管理的角度谈谈静态方法和静态属性 和 java对象引用与JVM自动内存管理
- 从JVM内存管理的角度谈谈静态方法和静态属性 和 java对象引用与JVM自动内存管理
- 从JVM内存管理的角度谈谈静态方法和静态属性 和 java对象引用与JVM自动内存管理
- java中静态属性和和静态方法的继承问题 以及多态的实质
- Java静态属性与静态方法能否被继承的问题
- Java基础-静态属性,继承
- java中静态属性和和静态方法的继承问题 以及多态的实质
- java中静态属性和和静态方法的继承问题 以及多态的实质
- 从JVM内存管理的角度谈谈静态方法和静态属性 和 java对象引用与JVM自动内存管理
- 2018/01/08JAVA 基础 / 接口与继承/Java关键词:变量引用 super 调用父类对象方法/构造方法【或者访问父类对象属性】的方式归纳
- 关于java中静态属性、静态方法的继承问题