《Java2实用教程(第三版)》第67页有下面一节代码,明明是书上例子,结果报错:
class 类人猿
{
private int n = 100;
void crySpeak(String s)
{
System.out.println(s);
}
}class People extends 类人猿
{
void computer(int a, int b)
{
int c = a * b;
System.out.println(c);
}
void crySpeak(String s)
{
System.out.println("**" + s + "**");
}
}class Example4_n
{
public static void main(String args[])
{
类人猿 monkey = new People(); //monkey是People对象的上转型对象
monkey.crySpeak("I love this game");
People people = People(monkey); //把上转型对象强制转换成子类的对象
people.computer(10, 10);
}
}People people = People(monkey)报警:没有为类型 Example4_n 定义方法 People(类人猿)
class 类人猿
{
private int n = 100;
void crySpeak(String s)
{
System.out.println(s);
}
}class People extends 类人猿
{
void computer(int a, int b)
{
int c = a * b;
System.out.println(c);
}
void crySpeak(String s)
{
System.out.println("**" + s + "**");
}
}class Example4_n
{
public static void main(String args[])
{
类人猿 monkey = new People(); //monkey是People对象的上转型对象
monkey.crySpeak("I love this game");
People people = People(monkey); //把上转型对象强制转换成子类的对象
people.computer(10, 10);
}
}People people = People(monkey)报警:没有为类型 Example4_n 定义方法 People(类人猿)
解决方案 »
- 生产者、消费者多线程《小鸟请教》
- webservice和Java 的 rmi 有什么分别 谁来提点一下。
- 自定义外观的Button怎么制作?
- 请问关于键盘输入回车问题。
- 有关JEditorPane. 急!!!
- 如何Java应用程序窗口浮在最上面
- 为什么我的窗口关了程序也不结束?
- 高分!有谁用过xindice&java,用过的进来看问题,拿分了!
- 在p4下安装了j2sdk-1_4_1_01-windows-i586怎么还是不能正常使用。
- 一个JDBC的问题,老是报错找不到驱动?
- 为什么会提示这个?
- 帮忙看下这个Java键入程序(The type keybord must implement the inherited abstract method )
你的括号反了,应该是
People people = (People)monkey;
改为: People people = (People)monkey;