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#