Java 传递对象子类类型

Java 传递对象子类类型,java,android,class,casting,Java,Android,Class,Casting,我正在使用一个以类类型为参数的函数: 我试图传递getSpans()对象“type”的特定子类 span; 对象类型; int启动; 内端; //假设上述变量已正确初始化。 .... //getSpans(int开始、int结束、类类型) ss.getSpans(开始、结束); 通过使用策略模式,您无需求助于一系列实例,即可实现此功能。下面是一个示例实现,它使用不同的提供者计算运输成本,而实际上不必知道所使用的提供者类型 public enum ShippingMethod { FIRS

我正在使用一个以类类型为参数的函数:

我试图传递getSpans()对象“type”的特定子类

span;
对象类型;
int启动;
内端;
//假设上述变量已正确初始化。
....
//getSpans(int开始、int结束、类类型)
ss.getSpans(开始、结束);

通过使用策略模式,您无需求助于一系列实例,即可实现此功能。下面是一个示例实现,它使用不同的提供者计算运输成本,而实际上不必知道所使用的提供者类型

public enum ShippingMethod {
    FIRST_CLASS {
        public double getShippingCost(double weightInPounds, double distanceInMiles) {
            // Calculate the shipping cost based on USPS First class mail table
        }
    },
    FED_EX {
        public double getShippingCost(double weightInPounds, double distanceInMiles) {
            // Calculate the shipping cost based on FedEx shipping
        }       
    },
    UPS {
        public double getShippingCost(double weightInPounds, double distanceInMiles) {
            // Calculate the shipping cost based on UPS table
        }           
    };

    public abstract double getShippingCost(double weightInPounds, double distanceInMiles);
};

public class ShippingInfo {

    private Address address;
    private ShippingMethod shippingMethod = ShippingMethod.FIRST_CLASS;

    public Address getAddress() {
        return this.address;
    }

    public double getShippingCost(double weightInPounds, double distanceInMiles) {
        return shippingMethod.getShippingCost(weightInPounds, distanceInMiles);
    }
}

有关和的详细信息。

是的,只需使用
type.class
。它将返回类型变量的类对象。还可以尝试
type.getClass().class


最好使用第二个示例。

type
是一个实例,因此
type.class
无效。另外,
type.getClass()
返回一个
Class
对象。
Class
类型没有名为
Class
的公共成员,因此
type.getClass().Class
也无效。你说得对。我编辑了我的问题,这样其他人就不会被我的困惑所困扰(因为我说getClass不起作用)。我尝试了getClass,但由于编码错误,它没有返回匹配项。顺便说一句,你不能得到一个实例的object.class,你唯一的选择是getClass()(我知道),这就是我认为mike要表达的观点。
public enum ShippingMethod {
    FIRST_CLASS {
        public double getShippingCost(double weightInPounds, double distanceInMiles) {
            // Calculate the shipping cost based on USPS First class mail table
        }
    },
    FED_EX {
        public double getShippingCost(double weightInPounds, double distanceInMiles) {
            // Calculate the shipping cost based on FedEx shipping
        }       
    },
    UPS {
        public double getShippingCost(double weightInPounds, double distanceInMiles) {
            // Calculate the shipping cost based on UPS table
        }           
    };

    public abstract double getShippingCost(double weightInPounds, double distanceInMiles);
};

public class ShippingInfo {

    private Address address;
    private ShippingMethod shippingMethod = ShippingMethod.FIRST_CLASS;

    public Address getAddress() {
        return this.address;
    }

    public double getShippingCost(double weightInPounds, double distanceInMiles) {
        return shippingMethod.getShippingCost(weightInPounds, distanceInMiles);
    }
}