- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我第一次发帖,但我遇到了很多问题。我目前有一个带有标题的 AbstractDevice 类:
public abstract class AbstractDevice> implements Device
这个类有一个带有标题的嵌套构建器类:
public static abstract class Builder
我还有一个带有标题的 AbstractPeripheral 类:
public abstract class AbstractPeripheral> extends AbstractDevice
这个类也有自己的嵌套构建器类,其标题为:
public static abstract class Builder extends AbstractDevice.Builder.
我的目标是让 AbstractPeripheral 扩展 AbstractDevice 并且 AbstractPeripheral 的构建器扩展 AbstractDevice 的。但是,我在尝试编译时收到此错误:
type argument uxb.AbstractPeripheral.Builder is not within bounds of type-variable T.
感谢任何帮助。谢谢抽象设备:
package uxb;
import java.util.List;
import java.util.Optional;
import java.util.ArrayList;
import java.math.BigInteger;
public abstract class AbstractDevice>
implements Device{
public static abstract class Builder{
private Integer version;
private Optional productCode;
private Optional serialNumber;
private List connectors;
public Builder(Integer version){
this.version = version;
} //end constructor method
public T productCode(Integer productCode){
if(productCode != null){
this.productCode = Optional.of(productCode);
} //end if statement
else{
this.productCode = Optional.empty();
} //end else statement
return getThis();
} //end method productCode()
public T serialNumber(BigInteger serialNumber){
if(serialNumber != null){
this.serialNumber = Optional.of(serialNumber);
} //end if statement
else{
/*Class has a static field for ZERO value*/
this.serialNumber = Optional.empty();
} //end else statement
return getThis();
} //end method serialNumber()
public T connectors(List connectors){
this.connectors = connectors;
return getThis();
} //end method connectors
protected abstract T getThis();
protected List getConnectors(){
return connectors;
} //end method getConnectors()
protected void validate(){
if(version == null){
throw new NullPointerException("Cannot be validated");
}
} //end method validate()
} //end nested abstract class Builder
private final Integer version;
private final Optional productCode;
private final Optional serialNumber;
private final List connectors;
private final List connectorObjects;
protected AbstractDevice(Builder builder){
this.version = builder.version;
this.productCode = builder.productCode;
this.serialNumber = builder.serialNumber;
this.connectors = builder.connectors;
ArrayList temp = new ArrayList();
for(int i = 0; i < connectors.size(); i++){
temp.add(new Connector(this, i, connectors.get(i)));
} //end for loop
connectorObjects = temp;
} //end constructor method
public Optional getProductCode(){
return productCode;
} //end method getProductCode()
public Integer getConnectorCount(){
/*Not Implemented Yet*/
trả về 0;
} //end method getConnectorCount()
public Optional getSerialNumber(){
return serialNumber;
} //end method getSerialNumber()
public Integer getVersion(){
return version;
} //end method getVersion()
public List getConnectors(){
return new ArrayList(connectorObjects);
} //end method getConnectors()
public Connector getConnector(int index){
if(! getConnectors().isEmpty()){
return getConnectors().get(index);
} //end if statement
else{
trả về giá trị null;
} //end else statement
} //end method getConnector()
} //end abstract class AbstractDevice
抽象外设: 封装uxb;
import java.util.List;
public abstract class AbstractPeripheral
AbstractPeripheral.Builder> extends
AbstractDevice{
public static abstract class Builder extends
AbstractDevice.Builder{
protected void validate(){
super.validate();
if(getConnectors().equals(null)){
throw new IllegalStateException("Cannot be validated");
} //end if statement
if(checkTypes(getConnectors())){
throw new IllegalStateException("Cannot be validated");
} //end if statement
} //end method
private boolean checkTypes(List types){
for(Connector.Type type: types){
if(type != Connector.Type.PERIPHERAL){
trả về false;
} //end if statement
} //end for each loop
trả về giá trị đúng;
} //end method checkTypes
public Builder(Integer version){
super(version);
} //end constructor method
} //end nested class Builder
public AbstractPeripheral(Builder builder){
super(builder);
}
} //end class AbstractPeripheral
中心: 封装uxb;
public class Hub extends AbstractDevice{
public static class Builder extends AbstractDevice.Builder{
public Builder(Integer version){
super(version);
} //end constructor method
public Hub build(){
validate();
return new Hub(getThis());
} //end method build()
protected Builder getThis(){
return this;
} //end method getThis()
protected void validate(){
super.validate();
if(!(super.getConnectors().contains(Connector.Type.COMPUTER))){
throw new IllegalStateException("Cannot be validated");
} //end if statement\
if(!(super.getConnectors().contains(Connector.Type.PERIPHERAL))){
throw new IllegalStateException("Cannot be validated");
} //end if statement
} //end validate()
} //end nested class Builder
private Hub(Builder builder){
super(builder);
} //end constructor method
public DeviceClass getDeviceClass(){
return DeviceClass.HUB;
} //end method getDeviceClass()
} //end class Hub
1 Câu trả lời
您确实需要使用正在构建的类,Và构建器作为构建器的参数。然而,正在构建的对象不需要需要了解构建器,因此不需要它作为参数。
我在几个开源项目中使用了这种模式或略有不同,包括 Apache Brooklyn和jclouds。
因此,从您的示例中,更改父类如下:
public abstract class AbstractDevice implements Device {
public static abstract class Builder {
public abstract B self();
public abstract T build();
public B example(String value) {
// do something with value
return self();
}
}
}
请注意,我还添加了一个抽象的 build()
方法,它将返回构建的对象。 self()
方法是必需的,因为当构建器方法返回 cái này
时,它将具有错误的类型。相反,每个构建器方法必须以 return self();
结尾,如所示的 example(String value)
方法中所示。那么 child 就变成了:
public abstract class AbstractPeripheral extends AbstractDevice {
public static abstract class Builder extends AbstractDevice.Builder {
}
}
您可以看到,T
Và B
泛型参数分别用于指向类的类型和构建器的类型。因此,要创建一个使用这些的具体类,应该创建类似这样的内容:
public class Hub extends AbstractPeripheral {
public static class Builder extends AbstractPeripheral.Builder {
public static final Builder builder() {
return new Builder();
}
public Builder self() {
return this;
}
public Hub build() {
return new Hub();
}
}
}
这还有一个静态 builder()
方法,它返回正确的 Builder
类的实例,如果您愿意,您也可以直接调用构造函数。 build()
方法只是创建并返回具体类,self()
方法在此处实现以返回 cái này
将具有正确的类型。
将所有内容放在一起,我们可以使用它来创建一个 Hub
对象,如下所示:
Hub hub = Hub.Builder.builder()
.example("something")
.build();
Xin lưu ý,AbstractDevice.Builder#example(String)
方法返回正确的类型,因为它实际上调用 Hub#self()
Và build()
按预期返回 Hub
的具体实例。
要减轻一些痛苦并删除重复的样板,您还可以尝试使用 Google AutoValue项目,我们现在正在 jclouds 中切换到该项目。
关于Java 抽象、泛型和构建器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39560266/
Tôi đang gặp sự cố khi xây dựng một ứng dụng cụ thể bằng gradle. Ứng dụng này có thể được biên dịch và xây dựng bằng Eclipse và chạy tốt trên máy tính bảng. Khi tôi thử xây dựng nó bằng Gradle, "compileDebugJava"
Tôi có một chương trình C do một lập trình viên nghỉ việc để lại. Tôi cố gắng tìm hiểu chính xác anh ấy đang làm gì và sắp xếp lại phần mềm thành thứ gì đó hợp lý hơn để tôi có thể xây dựng nó dễ dàng hơn. Tôi sử dụng CMake để xây dựng, còn anh ấy sử dụng Make. có
Tôi vừa bắt đầu đọc "Pro Spring MVC with web flow" và sách có kèm theo một ví dụ mã mà tôi muốn làm theo. Những gì tôi muốn - Tôi muốn xây dựng ứng dụng như trong sách, sử dụng Gradle. Những vấn đề là gì - Tôi chưa bao giờ sử dụng Gradle
Tôi hy vọng ai đó đã làm điều này rồi. Tôi đang cố gắng thiết lập bản dựng liên tục trong teamcity cho một trong những dự án Angular 2 của mình. Sau khi nghiên cứu, tôi đã làm theo các bước sau: Bước 1: Cài đặt js cho teamcity
Tôi có một giải pháp trang web ASP.Net cũ trông như thế này: Khi tôi xây dựng giải pháp trong Visual Studio, tôi nhận được kết quả sau: ------ Build started: Project: C:\..
Tôi sử dụng gulp-usref, gulp-if, gulp-uglify, gulp-csso và gulp-file-include để xây dựng ứng dụng của mình. Mọi thứ trong bản dựng đều hoạt động tốt ngoại trừ HTML vẫn giữ nguyên
Tôi đang phát triển một ứng dụng di động nội bộ bằng ionic2. Tôi có thể xây dựng thành công cho ios thông qua: ionic build ios và ionic build ios --prod nhưng nó cứ bị lỗi khi tôi thực hiện
Tôi là một nhà phát triển .NET/C# giàu kinh nghiệm nhưng lại mới làm quen với hầu hết các công nghệ/thư viện ở đây (bao gồm cả công việc SQL/DB). Tôi đang phát triển một ứng dụng .NET với nền tảng Azure/Entity Framework và một ứng dụng di động
Tôi đang sử dụng VS 2008. Tôi có thể biên dịch thành công giải pháp của mình bằng IDE. Tuy nhiên, khi tôi thử xây dựng bằng devenv.com, nó không thành công với lỗi "Lỗi: Không tìm thấy đầu ra nào cho nhóm đầu ra của dự án '(không thể xác định tên)'". Nhóm, cấu hình của nó, hoặc
Phiên bản: ember.js 2.7, ember-data 2.7 ember-cli 2.9.1 // cũng hoạt động với ember-cli 2.7 node 6.9.1, npm 3.10.9 // cũng hoạt động với no
Đây là lần đầu tiên tôi mày mò AzureDevops và thiết lập một số tác vụ CI. Tôi có một kho lưu trữ công khai (mã nguồn mở) và một giải pháp (.sln) chứa 3 dự án F#. Giải pháp có sẵn trên Windows/Mac/Li
Có vẻ như VS2008 vẫn chưa được phiên bản 5.1.5 hiện tại hoặc kho lưu trữ CVS STLPort hỗ trợ. Nếu ai đó đã làm điều này rồi thì sẽ rất hữu ích nếu chia sẻ nếu có thể :) Ngoài ra, hãy tìm hiểu về bản dựng VS2005 hoặc 2008 x64
Tôi có một dự án Python 2.7 và cho đến nay đã sử dụng gfortran và MinGW để xây dựng các tiện ích mở rộng. Tôi sử dụng MinGW vì nó dường như hỗ trợ các câu lệnh ghi và mảng có thể phân bổ trong mã Fortran, trong khi MSVC
đóng cửa. Câu hỏi này không liên quan đến chủ đề. Hiện tại không chấp nhận câu trả lời. Bạn có muốn cải thiện câu hỏi này không? Cập nhật câu hỏi để phù hợp với chủ đề trên Stack Overflow. Đã đóng cửa 9 năm trước. Cải thiện hàng đợi này
Tôi tự hỏi tại sao Zimbra Wiki chỉ liệt kê các nền tảng cụ thể cho quá trình xây dựng. Điều này có nghĩa là không thể xây dựng Zimbra trên các bản phân phối Linux khác không? Cộng đồng Zimbra chọn một bản phân phối Linux cụ thể để xây dựng Zimbra
Tôi sẽ xây dựng một công cụ CLI trong Swift. Tôi đã tạo dự án bằng lệnh này swift package init --type executable Khi tôi xây dựng dự án của mình và phân tích các tham số trong Xcode, hãy đọc bí danh và
Tôi muốn thiết lập quyền cho các tệp được thêm vào ảnh docker. Tôi có Dockerfile đơn giản này: FROM ubuntu:utopic WORKDIR /app RUN groupadd -g 1000 b
Khi tôi sử dụng clBuildProgram trong mã OpenCl của mình, nó không thành công với mã lỗi -11 mà không có bất kỳ thông tin nhật ký nào. Đây là mã của tôi trông như thế nào: ret = clBuildProgram(program
Tôi có một thanh điều hướng ở phía dưới có trang danh sách sử dụng khối trạng thái. lớp _MainPageState mở rộng State { int _index = 0; @override Wi
Tôi đang sử dụng Jenkins trên máy cục bộ của mình (URL Jenkins không được hiển thị trên Internet, nhưng Internet được bật trên máy đó.) Tôi đã thực hiện các thay đổi cấu hình sau: Cài đặt plugin Git và Github trên công cụ Jenkins
Tôi là một lập trình viên xuất sắc, rất giỏi!