interface sport
{
void run();
void jump();
}
class Athlete implements sport
{
public void run()
{
System.out.println("run");
}
/*public void jump()
{
System.out.println("jump");
}*/
public static void main(String [] args)
{
Athlete zhang = new Athlete();
zhang.run();
zhang.jump();
}
}
如果我把class Athlete中的
public void jump()
{
System.out.println("jump");
}注释掉,为什么不能编译?提示:Athlete不是抽象的,并且没有覆盖sport中的jump()
我不想调用sport中的jump方法,应该怎么做?
{
void run();
void jump();
}
class Athlete implements sport
{
public void run()
{
System.out.println("run");
}
/*public void jump()
{
System.out.println("jump");
}*/
public static void main(String [] args)
{
Athlete zhang = new Athlete();
zhang.run();
zhang.jump();
}
}
如果我把class Athlete中的
public void jump()
{
System.out.println("jump");
}注释掉,为什么不能编译?提示:Athlete不是抽象的,并且没有覆盖sport中的jump()
我不想调用sport中的jump方法,应该怎么做?
public void run()
{
System.out.println("run");
}
}
class SubAthlete extends Athlete { public static void main(String [] args)
{
Athlete zhang = new SubAthlete ();
zhang.run();
zhang.jump();
}}
void run();
}
interface Jumpable {
void jump();
}
interface sport extends Runnable, Jumpable {
}再让Athlete去实现Runnable,就可以了。但实际上,你常常无法改变接口的定义。这种情况下,如果有你不想实现的方法,解决方法是抛出UnsupportedOperationException异常,表示该操作不被支持,如:
class Athlete implements sport {
public void run() {
System.out.println("run");
}
public void jump() {
throw new UnsupportedOperationException();
}
public static void main(String [] args) {
Athlete zhang = new Athlete();
zhang.run();
zhang.jump(); // 运行时抛出异常
}
}
http://www.10zhizui.cn
{
abstract void run(); void jump()
{
}
}
class Athlete extends sport
{
public void run()
{
System.out.println("run");
}
/*public void jump()
{
System.out.println("jump");
}*/
public static void main(String [] args)
{
Athlete zhang = new Athlete();
zhang.run();
zhang.jump();
}
}
{
void run();
void jump();
}abstract class AbstractAthlete implements sport{{
abstract void run(); void jump()
{
}
}
class Athlete extends AbstractAthlete
{
public void run()
{
System.out.println("run");
}
public static void main(String [] args)
{
Athlete zhang = new Athlete();
zhang.run();
zhang.jump();
}
}