java新手,跪求解答!!!!

来源:百度知道 编辑:UC知道 时间:2024/06/02 01:00:12
public class Human {
private int n=100;
void crySpeak(String s){
System.out.println(s);
}}
class People extends Human{
void computer (int a,int b){
int c=a*b;
System.out.println(c);
}
void crySpeak(String s){
System.out.println("**"+s+"**");
}
}
class Exaple{
public static void main(String[] args) {
Human monkey;
monkey=new People();
monkey.crySpeak("I LOVE THIS GAME");
People people=(People)monkey;
people.computer(10,10);

}

}
为什么不能执行?

可以执行啊 执行结果:

**I LOVE THIS GAME**
100

public class Human {
private int n = 100;

void crySpeak(String s) {
System.out.println(s);
}
public static void main(String[] args) {
Human monkey;
monkey = new People();
monkey.crySpeak("I LOVE THIS GAME");
People people = (People) monkey;
people.computer(10, 10);

}
}

class People extends Human {
void computer(int a, int b) {
int c = a * b;
System.out.println(c);
}

void crySpeak(String s) {
System.out.println("**" + s + "**");
}
}

可以执行啊,
执行结果:

**I LOVE THIS GAME**
100

把public class Human 中的public去掉,并且文件名为Exaple.java就可以。
建议class Exaple前面加上public

因为对象monkey并不是People类的实例,而是people超类Humen的实例。因此不能强转,即使能转化成功他不能调用其中的方法和属性。