日期:2014-05-20 浏览次数:20888 次
import static mytools.Print.*; class Sports{ void play(){ print("super play"); } } class Football extends Sports{ void play(){ print("足球是用脚踢的!"); } } class Bascketball extends Sports{ void play(){ print("篮球是用手打的!"); } } public class Test2{ static void howToPlay(Sports sp){ sp.play(); } public static void main(String[] args){ howToPlay(new Football()); howToPlay(new Bascketball()); } }
------解决方案--------------------
基本正确,但最后将超类改成抽象类,如:
public class Sports{
public abstract void play();
}
------解决方案--------------------
没大懂你的意图
public class TestSports { public void howToPlay(Sports sp){ sp.play(); } public static void main(String[] args){ Sports football=new FootBall(); Sports basketball=new Basketball(); TestSports test=new TestSports(); test.howToPlay(football); test.howToPlay(basketball); } } class Sports { public void play(){ System.out.println("sports!"); } } class FootBall extends Sports{ public void play(){ System.out.println("kicking a goal!"); } } class Basketball extends Sports{ public void play(){ System.out.println("give a 3-p shot!"); } }
------解决方案--------------------
嘿嘿,一天登录一次(或者100次)就可以增加10分可用分的。