手机类
java
package Phone20240711;
public abstract class Phone {
private String brand;
private String price;
public Phone(){}
public Phone(String brand, String price) {
this.brand = brand;
this.price = price;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public abstract void call();
public abstract void sendMessages();
}
旧手机类
java
package Phone20240711;
public class OldPhone extends Phone{
public OldPhone() {}
public OldPhone(String brand, String price) {
super(brand, price);
}
@Override
public void call() {
System.out.println("旧手机" + this.getBrand() + "在打电话");
}
@Override
public void sendMessages() {
System.out.println("新手机" + this.getBrand() + "在打电话");
}
}
新手机类
java
package Phone20240711;
public class NewPhone extends Phone implements PlayGame{
public NewPhone() {}
public NewPhone(String brand, String price) {
super(brand, price);
}
@Override
public void call() {
System.out.println("新手机" + this.getBrand() + "在打电话");
}
@Override
public void sendMessages() {
System.out.println("新手机" + this.getBrand() + "在发短信");
}
@Override
public void playGame() {
System.out.println("新手机" + this.getBrand() + "在玩游戏");
}
}
游戏接口
java
package Phone20240711;
public interface PlayGame {
public abstract void playGame();
}
测试
java
package Phone20240711;
import java.util.Scanner;
public class PhoneTest {
public static void main(String[] args) {
NewPhone np = creatNewPhone();
OldPhone op = creatOldPhone();
np.call();
np.sendMessages();
np.playGame();
op.call();
op.sendMessages();
}
public static NewPhone creatNewPhone() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入新手机的品牌");
String brand = sc.next();
System.out.println("请输入新手机的价格");
String price = sc.next();
return new NewPhone(brand, price);
}
public static OldPhone creatOldPhone() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入旧手机的品牌");
String brand = sc.next();
System.out.println("请输入旧手机的价格");
String price = sc.next();
return new OldPhone(brand, price);
}
}