永发信息网

求java程序

答案:3  悬赏:70  手机版
解决时间 2021-07-26 08:25
  • 提问者网友:鼻尖触碰
  • 2021-07-25 08:05

1)编程实现:以电话Phone为父类(例:电话有本机号码、打电话、接电话等属性和功能,当然还有一些其它的特性),移动电话Mobilephone和固定电话Fixedphone为两个子类,并使移动电话实现接口:可移动Moveable。固定电话又有子类:无绳电话Cordlessphone。设计并定义这几个类,明确它们的继承关系,定义子类时给出子类有别于父类的新特性。

2)声明测试类:声明Phone类的数组(含5个元素),生成五个对象存入数组:其中二个Phone类的对象、一个Mobilephone类的对象、一个Fixedphone类的对象和一个Cordlessphone类的对象,打印输出每个对象的某个成员变量。将一个父类的引用指向一个子类对象,用这个塑型后的对象来调用某个方法实现多态性。

最佳答案
  • 五星知识达人网友:由着我着迷
  • 2021-07-25 09:43


public class Phone {
private String phoneCode; //电话号码
private String answerPhone; //接电话
private String call; //打电话


public Phone() {
super();
}

public Phone(String phoneCode, String answerPhone, String call) {
super();
this.phoneCode = phoneCode;
this.answerPhone = answerPhone;
this.call = call;
}

public String getPhoneCode() {
return phoneCode;
}
public void setPhoneCode(String phoneCode) {
this.phoneCode = phoneCode;
}
public String getAnswerPhone() {
return answerPhone;
}
public void setAnswerPhone(String answerPhone) {
this.answerPhone = answerPhone;
}
public String getCall() {
return call;
}
public void setCall(String call) {
this.call = call;
}


public void phoneType(){
System.out.println("这是电话");
}
}



public interface Moveable {
public void way(); //移动方式
}



public class Mobilephone extends Phone implements Moveable {
private String mp3; //带mp3


public Mobilephone() {
super();
}


public Mobilephone(String phoneCode, String answerPhone, String call,String mp3) {
super(phoneCode,answerPhone,call); //调用父类代参的构造方法
this.mp3 = mp3;
}


public String getMp3() {
return mp3;
}


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



public void way() {
System.out.println("这是可移动的Mobilephone");
}
}



public class Fixedphone extends Phone {
private String leaveWord; //留言


public String getLeaveWord() {
return leaveWord;
}


public void setLeaveWord(String leaveWord) {
this.leaveWord = leaveWord;
}


public Fixedphone() {
super();
}


public Fixedphone(String leaveWord) {
super(); //调用父类无参的构造方法
this.leaveWord = leaveWord;
}



public void phoneType(){
System.out.println("这是固定电话");
}



}



public class Cordlessphone extends Phone {
private String dect; //数字无绳电话


public Cordlessphone() {
super();
}


public Cordlessphone(String dect) {
super();
this.dect = dect;
}


public String getDect() {
return dect;
}


public void setDect(String dect) {
this.dect = dect;
}



public void phoneType(){
System.out.println("这是无绳电话");
}



}



public class TestPhone {


public static void main(String[] args) {
Phone[] phones = new Phone[5];
Phone phone1 = new Phone("4213264","使用phone1接电话","使用phone1打电话");
Phone phone2 = new Phone("3413436","使用phone1接电话","使用phone1打电话");
Phone mobilephone = new Mobilephone("13254742215","使用mobilephone接电话","使用mobilephone打电话","收听mp3");
Phone fixedphone = new Fixedphone("这是固定电话fixedphone");
Phone cordlessphone = new Cordlessphone("这是无绳电话cordlessphone");
phones[0] = phone1;
phones[1] = phone2;
phones[2] = mobilephone;
phones[3] = fixedphone;
phones[4] = cordlessphone;
for(int i=0;i<phones.length; i++){
if(i!=3||i!=4)
System.out.println("这是电话的一个变量值:"+phones[i].getCall());
if(i==3)
System.out.println("这是电话的一个变量值:"+((Fixedphone)phones[i]).getLeaveWord());
if(i==4)
System.out.println("这是电话的一个变量值:"+((Cordlessphone)phones[i]).getDect());
phones[i].phoneType();
}
}
}

全部回答
  • 1楼网友:蕴藏春秋
  • 2021-07-25 11:28
继承 接口 运用!基础的东西自己练练手吧!
  • 2楼网友:西风乍起
  • 2021-07-25 10:04
哥们是学java的吗?要努力,这些尽量自己写个试试
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯