不保真
Phone类
package com.neuedu.phone;
/**
* 编程实现、以电话Phone为父类
* (例、电话有本机号码、打电话、接电话等属性和功能,
* 当然还有一些其它的特性;
* @author Administrator
*
*/
public class Phone {
private String Number;
private String color;
private String brand;
public void call(){
System.out.println("打电话");
}
public void getCall(){
System.out.println("接电话");
}
public Phone(String number, String color, String brand) {
super();
this.Number = number;
this.color = color;
this.brand = brand;
}
public String getNumber() {
return Number;
}
public String getColor() {
return color;
}
public String getBrand() {
return brand;
}
}
Mobilephone类
package com.neuedu.phone;
/**
* 移动电话Mobilephone和固定电话Fixedphone为两个子类,
* 并使移动电话实现接口Moveable,接口里有移动信息功能;
* @author Administrator
*
*/
public class Mobilephone extends Phone implements Moveable {
private int screenSize;
public void moveMessage(){
System.out.println("发短信");
}
@Override
public void call() {
System.out.println("移动着打电话");
}
@Override
public void getCall() {
System.out.println("移动着接电话");
}
public Mobilephone(String number, String color, String brand, int screenSize) {
super(number, color, brand);
this.screenSize = screenSize;
}
public int getScreenSize() {
return screenSize;
}
}
Fixedphone类
package com.neuedu.phone;
public class Fixedphone extends Phone {
private String locNum;
@Override
public void call() {
System.out.println("固定地点打电话");
}
@Override
public void getCall() {
System.out.println("固定地点接电话");
}
public Fixedphone(String number, String color, String brand, String locNum) {
super(number, color, brand);
this.locNum = locNum;
}
public String getLocNum() {
return locNum;
}
}
Cordlessphone类
package com.neuedu.phone;
/**
* 固定电话又有子类、无绳电话Cordlessphone。
* @author Administrator
*
*/
public class Cordlessphone extends Fixedphone {
public Cordlessphone(String number, String color, String brand, String locNum) {
super(number, color, brand, locNum);
}
@Override
public void call() {
System.out.println("在一定范围内移动着打电话");
}
@Override
public void getCall() {
System.out.println("在一定范围内移动着接电话");
}
}
Moveable类
package com.neuedu.phone;
public interface Moveable {
public void moveMessage();
}
TestPhone类
package com.neuedu.phone;
/**
* 声明测试类、声明Phone类的数组(含5个元素),
* 生成五个对象存入数组、其中二个Phone类的对象、
* 一个Mobilephone类的对象、一个Fixedphone类的对象和一个Cordlessphone类的对象,
* 打印输出每个对象的某个成员变量。
* 将一个父类的引用指向一个子类对象,
* 用这个塑型后的对象来调用某个方法实现多态性。
* @author Administrator
*
*/
public class TestPhone {
public static void main(String[] args) {
Phone p[]= new Phone[5];
p[0] = new Phone("13998373955", "黑色", "samsung");
p[1] = new Phone("13998373966", "白色", "apple");
p[2] = new Mobilephone("13998373911", "蓝色", "XiaoMi", 6);
p[3] = new Fixedphone("83739223", "红色", "ChinaTelecom", "024");
p[4] = new Cordlessphone("73922346", "黄色", "ChinaUnicom","0414");
for (int i = 0; i < 5; i++) {
String s = p[i].getBrand();
System.out.println(s);
p[i].getCall();
}
Phone p1 = new Mobilephone("13998373933", "绿色", "oppo", 5);
p1.call();
}
网友评论