本帖最后由 chn_qmn 于 2012-11-23 11:39:09 编辑
package fish4_1;
public class Fish
{
abstract class Car
{
public abstract void startUp();
}
abstract class Mazda extends Car
{
public abstract void turbo();
}
public class Mazda6 extends Car
{
public void starup()
{
System.out.println("调用了Mazda6的启动功能!!!");
}
public void turbo()
{
System.out.println("调用了Mazda6的加速功能!!!");
}
}
public class Mazda3 extends Car
{
public void starup()
{
System.out.println("调用了Mazda3的启动功能!!!");
}
public void turbo()
{
System.out.println("调用了Mazda3的加速功能!!!");
}
}
public static void main(String[] args)
{
Mazda6 m6 = new Mazda6();
m6.startUp();
m6.turbo();
Mazda3 m3 = new Mazda3();
m3.startUp();
m3.turbo();
}
}
总是出现错误,不知道怎么改,学识浅薄见笑~我的开发环境是eclipse
------最佳解决方案--------------------------------------------------------
public class Fish
{
public static void main(String[] args)
{
Mazda6 m6 = new Mazda6();
m6.startUp();
m6.turbo();
Mazda3 m3 = new Mazda3();
m3.startUp();
m3.turbo();
}
}
abstract class Car
{
public abstract void startUp();
}
abstract class Mazda extends Car
{
public abstract void turbo();
}
class Mazda6 extends Car
{
public void turbo()
{
System.out.println("调用了Mazda6的加速功能!!!");
}
@Override
public void startUp() {
// TODO Auto-generated method stub
System.out.println("调用了Mazda6的启动功能!!!");
}
}
class Mazda3 extends Car
{
public void turbo()
{
System.out.println("调用了Mazda3的加速功能!!!");
}
@Override
public void startUp() {
// TODO Auto-generated method stub
System.out.println("调用了Mazda3的启动功能!!!");
}
}
这样写就不会出错了
------其他解决方案--------------------------------------------------------
首先你的Mazda6 和Mazda3里面的方法名写错了。还有既然把两个抽象类和两个子类放到Fish类里面,那么就不能这样建立对象了,要么像GJ20107924回答的那样写,要么
Fish f=new Fish();
f.new Mazda6().startUp();
这样调用。
------其他解决方案--------------------------------------------------------