关于类继承中的重写,我写了下面一段代码。类MostCommonDiv中的f()方法求的是两个数的最小公倍数。
类SubMostCommonDiv继承自MostCommonDiv,并重写了其f()方法。 程序运行时报错:
Exception in thread "main" java.lang.ClassCastException: chapter4.MostCommonDiv
at chapter4.Exam4_18.main(Exam4_18.java:68)
类转换出错,求高手简答..感激不尽package chapter4;class MostCommonDiv{
private int m;
private int n;
public int f(int a, int b){
this.m = a;
this.n = b;
if(m<n){
int temp = 0;
temp = m;
m = n;
n = temp;
}
int start = n;
int div = 0;
for(int i=start; i>0; i--){
if((m%i == 0) && (n%i == 0)){
div = i;
break;
}
}
return div;
}
}class SubMostCommonDiv extends MostCommonDiv{
protected static int resultOfSupper = 0;
public int f(int a, int b){
int m = super.f(a, b);
this.resultOfSupper = (a*b)/m;
if(a<b){
int temp = 0;
temp = a;
a = b;
b = temp;
}
int start = a;
int subdiv = 0;
while(start > 0){
if((start%a == 0) && (start%b ==0)){
subdiv = start;
break;
}else{
start ++;
}
}
return subdiv;
}
}
public class Exam4_18 { public static void main(String[] args) {
MostCommonDiv test1 = new MostCommonDiv();
SubMostCommonDiv test2 = new SubMostCommonDiv();
int a = 32;
int b = 8;
System.out.println("f function in father:"+test1.f(a, b));
System.out.println("f function in son:"+((SubMostCommonDiv)test1).f(a, b));
System.out.println("add the result of (a*b)/m="+SubMostCommonDiv.resultOfSupper);
System.out.println("f function in son:"+test2.f(a, b));
System.out.println("add the result of (a*b)/m="+test2.resultOfSupper);
}}
类SubMostCommonDiv继承自MostCommonDiv,并重写了其f()方法。 程序运行时报错:
Exception in thread "main" java.lang.ClassCastException: chapter4.MostCommonDiv
at chapter4.Exam4_18.main(Exam4_18.java:68)
类转换出错,求高手简答..感激不尽package chapter4;class MostCommonDiv{
private int m;
private int n;
public int f(int a, int b){
this.m = a;
this.n = b;
if(m<n){
int temp = 0;
temp = m;
m = n;
n = temp;
}
int start = n;
int div = 0;
for(int i=start; i>0; i--){
if((m%i == 0) && (n%i == 0)){
div = i;
break;
}
}
return div;
}
}class SubMostCommonDiv extends MostCommonDiv{
protected static int resultOfSupper = 0;
public int f(int a, int b){
int m = super.f(a, b);
this.resultOfSupper = (a*b)/m;
if(a<b){
int temp = 0;
temp = a;
a = b;
b = temp;
}
int start = a;
int subdiv = 0;
while(start > 0){
if((start%a == 0) && (start%b ==0)){
subdiv = start;
break;
}else{
start ++;
}
}
return subdiv;
}
}
public class Exam4_18 { public static void main(String[] args) {
MostCommonDiv test1 = new MostCommonDiv();
SubMostCommonDiv test2 = new SubMostCommonDiv();
int a = 32;
int b = 8;
System.out.println("f function in father:"+test1.f(a, b));
System.out.println("f function in son:"+((SubMostCommonDiv)test1).f(a, b));
System.out.println("add the result of (a*b)/m="+SubMostCommonDiv.resultOfSupper);
System.out.println("f function in son:"+test2.f(a, b));
System.out.println("add the result of (a*b)/m="+test2.resultOfSupper);
}}
解决方案 »
- 如何计算MD5的值?
- 关于JAVA日期问题,求思路或源码。
- 这次csdn故障修复后感觉我的专家分、可用分和一些已发的帖子都产生了变化。
- ~~~~~看看这个applet?~~~~~
- java 是否提供listview的功能?
- 朋友们谁公司要人能帮忙推荐下,万分感谢。2年互联网开发经验,简历我写到内容里了。
- 应该有高手用过 ImageJ吧!!!!!!!(UP有分喽!)
- 如何將一個String 類型的變量轉換成 double類型.
- jdk1.4.1正式发布了!!!!!!!!!!!!!!!!!!
- 一个很简单的问题,清帮忙解答一下
- 帮我算个java的时间问题,保证给分
- 都说这是懒加载问题可谁能解决搞定?
public class Exam4_18 { public static void main(String[] args) {
MostCommonDiv test1 = new SubMostCommonDiv();
SubMostCommonDiv test2 = new SubMostCommonDiv();
int a = 32;
int b = 8;
System.out.println("f function in father:"+test1.f(a, b));
System.out.println("f function in son:"+((SubMostCommonDiv)test1).f(a, b));
System.out.println("add the result of (a*b)/m="+SubMostCommonDiv.resultOfSupper);
System.out.println("f function in son:"+test2.f(a, b));
System.out.println("add the result of (a*b)/m="+test2.resultOfSupper);
}}
MostCommonDiv test1 = new SubMostCommonDiv();
声明的时候要使用子类,后面才可以进行强制转换
test1 声明的是父类, 实现的是父类, 这样是不能强转成子类的.只有多态的方式下,才能向下转型呢.
比如:
MostCommonDiv test3=new SubMostCommonDiv();
这样可以用(SubMostCommonDiv)test3.(但多态的情况下,自然调用的方法是子类覆写了的父类方法. 也不需要转型了.
只有调用子类新加的方法,才需要用向下转型后去调用.)