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

2024年11月22日 11:30
有3个网友回答
网友(1):

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("11111");
p[2]=mp;
Fixedphone fp=new Fixedphone();
fp.setPhonenumber("22222");
p[3]=fp;
Cordlessphone cp=new Cordlessphone();
cp.setPhonenumber("33333");
p[4]=cp;

for(int i=0;i System.out.println(p[i].getPhonenumber());
} p[4]=p[1];
System.out.println(p[4].getPhonenumber());

}} 写的不是很好,希望对你有帮助噶

网友(2):

//电话基类
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();
}
}
}

网友(3):

想用C#