Java以电话Phone为父类
发布网友
发布时间:2022-05-25 17:33
我来回答
共2个回答
热心网友
时间:2023-11-25 06:06
public class PhoneTest {
public PhoneTest() {
}
public static void main(String[] args) {
Phone[] phs = new Phone[] { new Phone("0214-6325417"),
new Phone("0214-6325418"), new Mobilephone("18888888888"),
new Fixedphone("0578-88996654"),
new Cordlessphone("0578-88996655"), };
for (Phone ph : phs) {
System.out.println(ph);
ph.call();
ph.answer();
}
}
}
class Phone {
private String number;
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public Phone() {
}
public Phone(String number) {
this.number = number;
}
public void call() {
System.out.println("Phone calling...");
}
public void answer() {
System.out.println("Phone answer...");
}
public String toString() {
return "I'm " + this.getClass().getSimpleName() + ", My number is : "
+ this.getNumber();
}
}
interface Moveable {
public void move();
}
class Mobilephone extends Phone implements Moveable {
public Mobilephone() {
}
public Mobilephone(String number) {
super(number);
}
public void answer() {
System.out.println("Mobilephone calling...");
}
public void call() {
System.out.println("Mobilephone answering...");
}
public void move() {
System.out.println("I'm Mobilephone, i can move.");
}
}
class Fixedphone extends Phone {
public Fixedphone() {
}
public Fixedphone(String number) {
super(number);
}
public void answer() {
System.out.println("Fixedphone calling...");
}
public void call() {
System.out.println("Fixedphone calling...");
}
}
class Cordlessphone extends Fixedphone {
public Cordlessphone() {
}
public Cordlessphone(String number) {
super(number);
}
public void call() {
System.out.println("Cordlessphone calling...");
}
public void answer() {
System.out.println("Cordlessphone canswering...");
}
}
热心网友
时间:2023-11-25 06:07
在这里我只给一个大概的类模型,类当中的内容需要你自己去处理了
Class Phone{
private string number;
public bool getPhone(){
~~~~~
}
public bool sendPhone(){
~~~
}
}
class Mobilephone extends Phone implement Moveable{
public bool move(){
}
}
public interface Moveable{
public bool move();
}
class Fixedphone extends Phone{
~~~
}
class Cordlessphone extends Fixedphone{
}