永发信息网

急求一个Java程序设计(我给100分)

答案:6  悬赏:80  手机版
解决时间 2021-04-25 02:39

Java中的继承都是单继承的,即一个类只能有一个父类。但是在很多情况下单继承不能满足我们的需求。比如,飞机可以飞,鸟可以飞,超人也可以飞。但是这三者除了这个能飞这个方法以外,很难找到其他类似的方法或者属性。如果单就为了这三个类写一个父类,那么再想实现其他父类就不能了。因此,我们在这里要用接口。

Java中的接口是可以实现多个的。这就解决了单继承所不能的很多问题。

接口实验:

1. 定义一个接口CanFly,接口内定义一个方法canFly();

2. 定义一个飞机类Plane,实现CanFly接口,并重写canFly()方法;

3. 定义一个鸟类Bird,实现CanFly接口,并重写canFly()方法;

4. 定义一个超人类SuperMan,实现CanFly接口,并重写canFly()方法;

5. 定义一个机场管理员类Manager,用以调用各类的方法;

最佳答案
interface CanFly{
public void canFly();
}
class Plane implements CanFly{
public void canFly(){
System.out.print("Plane can fly");
}
}
class Bird implements CanFly{
public void canFly(){
System.out.print("Bird can fly");
}
}
class SuperMam implements CanFly{
public void canFly(){
System.out.print("SuperMan also can fly");
}
}
public class Manager{
static public void main(String a[]){
Plane plane=new Plane();
plane.canFly();
Bird bird=new Bird();
bird.canFly();
SuperMan superman=new SuperMan();
superman.canFly();
}
}
全部回答

public interface Fly{ public void canFly(){}; }

public class Plane implements Fly { public void canFly(){ System.out.println("Plane can fly"); } }

public class Bird implements Fly { public void canFly(){ System.out.println("Bird can fly"); } }

public class SuperMan implements Fly { public void canFly(){ System.out.println("SuperMan can fly"); } }

public class Manager{ public void fly(Fly f){ f.canFly(); } }

public class Test{ public static void main(String arg[]){ Plane plane = new Plane(); Bird bird = new Bird(); SuperMan superman = new SuperMan(); Manager manager = new Manager(); manager fly(Plane); manager fly(bird); manager fly(superman); } }

interface CanFly { void canFly(); }

class Plane implements CanFly { void canFly() { System.out.println("plane can fly"); } }

class Bird implenments CanFly { void canFly() { System.out.println("bird can fly"); } }

class SuperMan implenments CanFly { void canFly() { System.out.println("superMan can fly"); } }

public class Manager { public static void main(String[] args) { Plane plane=new Plane(); Bird bird=new Bird(); SuperMan superMan=new SuperMan(); plane.canFly(); bird.canFly(); superMan.canFly(); } }

java的275课程中的例子 可以去看看

public interface MyInterface { public void introduce(); public void NewMethod(); }

public class Pupil implements MyInterface { private String name; //姓名 private String stuNo; //学好 private double mark; //得分

public Pupil(){}

public Pupil(String name, String stuNo, double mark) { super(); this.name = name; this.stuNo = stuNo; this.mark = mark; }

public String getName() { return name; } public void setName(String name) { this.name = name; } public String getStuNo() { return stuNo; } public void setStuNo(String stuNo) { this.stuNo = stuNo; } public double getMark() { return mark; } public void setMark(double mark) { this.mark = mark; }

public void introduce(){ System.out.println("姓名:"+this.getName()+"\t"+"学号:"+this.getStuNo()+"分数"+this.getMark()); }

public void myMethod(){ System.out.println("小学生类"); }

public void NewMethod() { System.out.println("这是小学生类的新方法"); } }

public class MiddleStudent1 extends Pupil implements MyInterface{ private String mp3;

public MiddleStudent1() { super(); }

public MiddleStudent1(String name,String stuNo,double mark,String mp3) { super(name,stuNo,mark); //调用父类代餐构造 this.mp3 = mp3; }

public String getMp3() { return mp3; }

public void setMp3(String mp3) { this.mp3 = mp3; }

public void introduce (){ System.out.println("姓名:"+this.getName()+"\t"+"学号:"+this.getStuNo()+"分数"+this.getMark()+"Mp3:"+this.getMp3()); }

public void myMethod(){ System.out.println("MiddleStudent1类"); } }

public class MiddleStudent2 extends Pupil { private String mobile;

public MiddleStudent2() { super(); }

public MiddleStudent2(String mobile) { super(); this.mobile = mobile; }

public String getMobile() { return mobile; }

public void setMobile(String mobile) { this.mobile = mobile; }

public void introduce(){ System.out.println("姓名:"+this.getName()+"\t"+"学号:"+this.getStuNo()+"手机:"+ this.getMobile()); }

public void run(){ System.out.println("这是MiddleStudent2类自己的方法"); }

public void myMethod(){ System.out.println("MiddleStudent2类"); } }

//实现多态 public class PolymiorphismImp { public void getMyMethod(Pupil pupil){ pupil.myMethod(); pupil.introduce(); } }

public class Test { public static void main(String[] args) { Pupil pubil = new Pupil("zhangsan","1001",76); Pupil middleStudent1 = new MiddleStudent1("lishi","1003",98,"mp3"); Pupil middleStudent2 = new MiddleStudent2("sanxing"); PolymiorphismImp imp = new PolymiorphismImp(); ((MiddleStudent2)middleStudent2).run(); imp.getMyMethod(pubil); imp.getMyMethod(middleStudent1); imp.getMyMethod(middleStudent1); System.out.println(); } }

简单的来说

1.写个CanFly接口 里面有个方法 void canFly(); 不实现

2.Plane 实现 CanFly接口 方法void canFly(){}里面 实现方法 比如说 我是飞机 我会飞

3.Bird 实现 CanFly接口 方法void canFly(){}里面 实现方法 比如说 我是鸟 我会飞

4.SuperMan 实现 CanFly接口 方法void canFly(){}里面 实现方法 比如说 我是超人 我会飞

5.Manager 里面写个方法 然后实例化上面3个类 (不包含接口 接口不能实例化)

然后打.调用每个的方法就好 最后你还可以写个Test来实例化Manager类 然后调用Manager的方法

我给你说思路

希望你自己写 对你有好处的

我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
西兰溪村地址在什么地方,想过去办事
半包包括哪些项目,房屋装修时的硬装具体包括
怎么下DNF的台服补丁
怎么才能学会电脑
2006到2007年都听先什么歌
淘宝被举报的条件?
手机坏点怎么修复?
已知a的平方+3ab=-2,3b的平方+ab=1,求代数
我觉得一些关心爸爸妈妈的话肉麻,为何会这样
快男十二强中,最有希望进军冠军、季军、亚军
对QQ好友隐身
名侦探柯南不更新了吗,名侦探柯南国语版为什
请问战地之火怎么既活
谁有<是美男啊>里面歌曲的歌曲链接?空间用
百合香味诗句,描写蟹粉小笼包的句子
推荐资讯
DNF2000万多少人民币啊?
作为一名大学生
我想找个晚校
密云哪有薰衣草园?
现代爱情诗歌谁写的好,现代 女生写恋爱的诗句
怎样在CSOL运用超级跳
为什么我前面的头发都油油的?而后面都没有?
阴阳师姑获鸟台词翻译,手游阴阳师里,晴明在
李白诗歌内容及特点,李白和杜甫的诗歌各有什
红眼崩山学到了11怎么办。是不是废了啊
开通一年QQ会员QQ黄钻超级QQ,那个划算,那个便
电脑一到晚上就自动断电?
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?