永发信息网

用JAVA编程 类与对象的基础题

答案:3  悬赏:60  手机版
解决时间 2021-05-20 00:48

1、 类与对象的基础题:

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

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

最佳答案

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 void phoneType(){
System.out.println("这是移动电话");
}
}



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


public Fixedphone() {
super();
}


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



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



public class Cordlessphone extends Fixedphone {
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++){
System.out.println("这是电话的一个变量值:"+phones[i].getCall());
phones[i].phoneType();
}
}
}

全部回答
//电话基类 public class Phone { protected String myNumber; Phone(){ this.myNumber = "zongTaiNumber0000"; } public void call() { System.out.println(myNumber + " is calling....."); } public void answer() { System.out.println(myNumber + " is answer......"); } } //移动电话 class Mobilephone extends Phone implements Moveable{ Mobilephone() { this.myNumber = "MobilephoneNumber"; } public void move() { System.out.println("i am moving....."); } } //固定电话 class Fixedphone extends Phone { Fixedphone() { this.myNumber = "FixedphoneNumber"; } } //无绳电话 class Cordlessphone extends Fixedphone { Cordlessphone() { this.myNumber = "CordlessphoneNumber"; } } //可移动接口 interface Moveable { public void move(); } //测试类 class Test { public static void main(String[] args) { Phone[] p = new Phone[5]; Phone p1 = new Phone(); Phone p2 = new Phone(); Mobilephone p3 = new Mobilephone(); Fixedphone p4 = new Fixedphone(); Cordlessphone p5 = new Cordlessphone(); p[0] = p1; p[1] = p2; p[2] = p3; p[3] = p4; p[4] = p5; for (int i = 0; i < p.length; i++) { System.out.println("this phone number is : " + p[i].myNumber); p[i].call(); } } }

class Phone{ private String phonenumber; public void setPhonenumber(String phonenumber){ this.phonenumber=phonenumber; } public String getPhonenumber(){ return phonenumber; } public void recCall(){ System.out.println("接到一个电话"); } public void telCall(){ System.out.println("拨出一个电话"); } }

class Fixedphone extends Phone{ private String phonenumber;//号码是私有,设置为private,不可继承 public void recCall(){ System.out.println("以"+this.phonenumber+"呼出了一个电话"); //重载了父类的recCall } }

class Cordlessphone extends Fixedphone{ private String phonenumber; public void info(){ System.out.println("这是无绳电话的信息"); } }

interface Moveable{ public void moveinfo(); }

class Mobilephone extends Phone implements Moveable{ private String phonenumber; public void moveinfo(){ System.out.println("我实现了可移动性"); } }

public class PhoneTest{ public static void main(String a[]){ Phone[] p=new Phone[5]; Phone p1=new Phone(); p1.setPhonenumber("123456789"); p[0]=p1; Phone p2=new Phone(); p2.setPhonenumber("987654321"); p[1]=p2; Mobilephone mp=new Mobilephone(); mp.setPhonenumber("

想用C#

我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
苏州长岛花园在哪?坐什么公交可以到?
举个例子,到底是什么样的话啊?
如何详细清洗电脑机箱
每天对他的的关注与关心代表了什么???是习
()比20吨少20%
有个关于RS485的问题
LG法伤里的伤害的1/3是人物的伤害还是武器的
很爱她、为什么想离开她?
农历十二月初六的塔罗牌是什么
喜欢的快男是谁?
为什么不通过?
什么叫本地磁盘?
彩虹是谁唱的
茄子多吃点对身体有害还是有益?
有谁知道这动漫的名字!
推荐资讯
生物学未来的发展的评价.
只能对已解决的问题发表评论对还是错
IBM笔记本店面销售技巧..
Windows7下ATI和N卡混交有意义吗?
转氨酶过高怎么办. 急等
如何解除支付宝商户签约协议
服装打板和跟单哪个好?
我手机卡的月租是18元的,但我想用10月租的卡
郭沫若诗词适合小学生,硬笔书法作品欣赏楷书
在哪点亮呢问问
二人英语情景对话:关于在面试时怎么样给人留
粽叶是什么意思,打粽子是什么意思
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?