public interface CPR {
double balance();
void debit(double amount);
void charge(double amount);
}
public class Facile implements CPR {
private int number;
Facile(int number){
this.number = number;
}
@Override
public double balance() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void debit(double amount) {
// TODO Auto-generated method stub
}
@Override
public void charge(double amount) {
// TODO Auto-generated method stub
}
}
public class Gold implements CPR {
private int number;
Gold(int number) {
this.number = number;
}
@Override
public double balance() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void debit(double amount) {
// TODO Auto-generated method stub
}
@Override
public void charge(double amount) {
// TODO Auto-generated method stub
}
}
public class Ecpr implements CPR{
private int number;
Ecpr(int number){
this.number = number;
}
@Override
public double balance() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void debit(double amount) {
// TODO Auto-generated method stub
}
@Override
public void charge(double amount) {
// TODO Auto-generated method stub
}
}
实现以下客户端。 私人商业和公共。
public abstract class Client {
private CPR card;
private int CF;
Client(int CF){
this.CF = CF;
}
abstract CPR makeCPR( int number);
public void getCPR(int number){
card = makeCPR(number);
}
public void chargeCPR(double amount){
card.charge(amount);
}
public void payCPR(double amount) throws Exception{
}
public void cancelCPR(int newnumber){
}
public void writeInfo(){
}
}
我很难实现扩展客户端的其他类。
例如,我认为私有类会实现它。
public class Private extends Client {
private CPR card;
Private(int CF) {
super(CF);
// TODO Auto-generated constructor stub
}
@Override
CPR makeCPR(int number) {
return new Facile(number);
}
public void payCPR(double amount) throws Exception{
if (amount > card.balance());
}
public void cancelCPR(int newnumber){
}
public void writeInfo(){
}
}
答案 0 :(得分:0)
如果我理解你,你需要Client
的继承人,这将返回CPR
的正确实例。要解决此问题,您可以使用Generics
public abstract class Client<T extends CPR> {
private CPR card;
private int CF;
Client(int CF){
this.CF = CF;
}
abstract T makeCPR( int number);
//....
}
public class GoldClient extends Client<Gold> {
Gold makeCPR( int number) {
return new Gold(number);
}
}