俺昨天考试遇到一题!面向对象概念不是很清晰,发上来大家帮我看看!定义一个体育活动类(Sports)作为基类,它有一个进行活动的方法play(方法名命名规范:J2EE方向:小写字母开头 . NET方向:大写字母开头)。
足球(Football)和篮球(Bascketball)都是体育活动类的衍生类。
请在测试类(Test)中编写一个方法howToPlay(Sports sp),该方法要求传递一个Sports类型的参数。该方法的作用是:
1. 当传入的实例类型为Football时,控制台上应打印:足球是用脚踢的!
2. 当传入的实例类型为Bascketball时,控制台上应打印:篮球是用手打的!
在main方法中调用howToPlay()验证你的代码是对的。
本题目要求严格使用面向对象中的继承、多态概念。俺的答案
//运动类
public class Sports{
public void play(){
System.out.println ("运动");
}
}
class Football extends Sports{
public void play(){
System.out.println ("足球是用脚踢的");
}}
class Bascketball extends Sports{
public void play(){
System.out.println ("篮球是用手打的");
}
}
//测试类
class Test{
public void howToPlay(Sports sp){
sp.play();
}
public static void main(String[]args){
Test t=new Test();
t.howToPlay(new Bascketball());
}
}
足球(Football)和篮球(Bascketball)都是体育活动类的衍生类。
请在测试类(Test)中编写一个方法howToPlay(Sports sp),该方法要求传递一个Sports类型的参数。该方法的作用是:
1. 当传入的实例类型为Football时,控制台上应打印:足球是用脚踢的!
2. 当传入的实例类型为Bascketball时,控制台上应打印:篮球是用手打的!
在main方法中调用howToPlay()验证你的代码是对的。
本题目要求严格使用面向对象中的继承、多态概念。俺的答案
//运动类
public class Sports{
public void play(){
System.out.println ("运动");
}
}
class Football extends Sports{
public void play(){
System.out.println ("足球是用脚踢的");
}}
class Bascketball extends Sports{
public void play(){
System.out.println ("篮球是用手打的");
}
}
//测试类
class Test{
public void howToPlay(Sports sp){
sp.play();
}
public static void main(String[]args){
Test t=new Test();
t.howToPlay(new Bascketball());
}
}
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 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!");
}
}
private abstract class Sports{
public abstract void Play();
}
private class Football extends Sports{
public void Play(){
System.out.println("It is played with the foot that the football is!");
}
}
private class Basketball extends Sports{
public void Play(){
System.out.println("It is played with the hand that the basketball is!");
}
}
public static void HowToPlay(Sports sp){
sp.Play();
}
public static void main(String[] args){
dels pp = new dels();
HowToPlay(pp.new Football());
HowToPlay(pp.new Basketball());
}
}
{public void play(){};}
class Football extends Sports
{public void play()
{System.out.println("足球是用脚踢的!");}
}
class Bascketball extends Sports
{public void play()
{System.out.println("篮球是用手打的!");}
}
public class Tesy02{
public static void howToPlay(Sports sp)
{sp.play();
}
public static void main(String[] args)
{
howToPlay(new Football());
howToPlay(new Bascketball());
}
}