[英]Extend a generic holder class, have it accept only specific objects?
I am trying to understand and accomplish the task of trying to create a class that extends a generic class that accepts all types of classes. 我试图理解并完成尝试创建一个类的任务,该类扩展了接受所有类型类的泛型类。 So far I have that working.
到目前为止,我已经在工作。 I am trying to create a class that extends a generic holder class and have this class accept only specific objects.
我正在尝试创建一个扩展通用持有人类的类,并让该类仅接受特定的对象。
Example, a class called "ComputerOrder" that will not accept an Apple or Orange object but only a ComputerPart or Peripheral object, such as Motherboard or Printer objects. 例如,一个名为“ ComputerOrder”的类将不接受Apple或Orange对象,而仅接受ComputerPart或Peripheral对象,例如Motherboard或Printer对象。 Been stuck on this for 2 weeks.
坚持了2周。 I can't for the life of me figure this concept out.
我无法终生想出这个概念。 Any help would be appreciated.
任何帮助,将不胜感激。
abstract class Product{
protected float price;
abstract float price();
public String toString() {
return "Price = " + String.valueOf(price) + " ";
}
}
class Apple extends Product{}
class Orange extends Product{}
class ComputerPart extends Product{
public ComputerPart(float p){
price = p;
}
public float price() {
return price;
}
}
class Motherboard extends ComputerPart{
protected String manufacturer;
public Motherboard(String mfg, float p) {
super(p);
manufacturer = mfg;
}
public String getManufacturer() {
return manufacturer;
}
}
class Peripheral extends Product{
public Peripheral(float p) {
price = p;
}
public float price() {
return price;
}
}
class Printer extends Peripheral{
protected String model;
public Printer(String model, float p) {
super(p);
this.model = model;
}
public String getModel() {
return model;
}
}
class Cheese extends Product{
public Cheese(float p) {
price = p;
}
public float price() {
return price;
}
}
class Cheddar extends Cheese{
public Cheddar(float p) {
super(p);
}
}
class GenericOrder<T>{
public ArrayList<T> storage = new ArrayList<T>();
private static int counter = 1;
public final int id;
public T obj;
public GenericOrder(){
id = counter;
counter++;
}
public void add(T item){
storage.add(item);
}
public T get(int in){
return obj;
}
public void getId(){
System.out.println(this.id);
}
public String toString(){
String ret = "";
Iterator<T> it = storage.iterator();
while (it.hasNext()) {
ret += it.next() + "\n";
}
return ret;
}
}
class ComputerOrder extends GenericOrder {
public void add(ComputerPart in){
if(in instanceof ComputerPart){
storage.add(in);
}
}
}
public class Tme2{
public static void main(String[] args){
ComputerOrder com = new ComputerOrder();
com.add(new Motherboard("bla", 3.33f))
}
}
You can do it like this: 您可以这样做:
class ComputerOrder<T extends ComputerProduct> extends GenericOrder<T> {
//...
}
Here, ComputerProduct
is a class that extends Product
and all your computer products like ComputerPart
or Peripheral
extend ComputerProduct
. 在这里,
ComputerProduct
是扩展Product
的类,而您的所有计算机产品(例如ComputerPart
或Peripheral
扩展了ComputerProduct
。 Similarly, you could create a class FoodProduct
derived from Product
, from which Apple
, Orange
and Cheese
are derived: 同样,您可以创建一个从
Product
派生的FoodProduct
类, FoodProduct
派生Apple
, Orange
和Cheese
:
class FoodOrder<T extends FoodProduct> extends GenericOrder<T> {
//...
}
The declaration <T extends ComputerProduct>
is a type restriction , which ensures that all types of T
are derived from ComputerPart
, otherwise you will get a compiler error. 声明
<T extends ComputerProduct>
是一个类型限制 ,它确保所有T
类型都从ComputerPart
派生,否则将出现编译器错误。
The ComputerOrder
class is still generic, so you could instance an order for all computer products: ComputerOrder
类仍然是通用类,因此您可以为所有计算机产品实例一个订单:
ComputerOrder order = new ComputerOrder<ComputerProduct>();
// Add peripherals, printers, motherboards...
// Apples, ... will throw compiler errors...
But you could also restrict it to peripherals only: 但您也可以将其仅限于外围设备:
ComputerOrder order = new ComputerOrder<Peripheral>();
// Add peripherals, printers,...
// Apples, motherboards (ComputerProduct, but NOT Peripheral) will fail...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.