如何从抽象类中实现通用字段?

时间:2016-02-29 20:07:02

标签: java generics

我有一个抽象类,它有一个字段,其类型可以在其子类之间变化。例如:

public abstract class ABCModel {
    protected String foo;
    protected String bar;
    protected ? baz;
}

在我的实现中,baz可能是Int或Float。例如:

public class ModelFoo {
     protected Int baz;
}

public class Modelbar {
     protected Float baz;
}  

首先,让我问一下这是否是Java中有效/可接受的设计模式?我选择了这种模式,因为我想抽象出共享方法中大部分繁琐的样板。

根据我的实现方式,我得到了这个错误的变体:

incompatible types: CAP#1 cannot be converted to BidAsk
where CAP#1 is a fresh type-variable:
CAP#1 extends Object from capture of ?

这让我相信我做错了什么。

我发布的示例与我的实际代码相比有点微不足道,其中这个泛型隐藏在嵌套的哈希表中。我试图在投入过多之前决定我在做什么是Java中的智能设计。

我已经尝试过搜索,但可能没有正确地阐述术语。

感谢。

3 个答案:

答案 0 :(得分:3)

您要问的是泛型的基本用法:

public abstract class ABCModel<T> {
    private T baz;

    public T getBaz() { return baz; }

    public void setBaz(T baz) { this.baz = baz; }
}

public class IntModel extends ABCModel<Integer> { // baz is of type Integer   
}

public class FloatModel extends ABCModel<Float> { // baz is of type Float   
}

IntModel m1 = new IntModel();
Integer i = m1.getBaz();

FloatModel m2 = new FloatModel();
Float f = m2.getBaz();

答案 1 :(得分:1)

无法在Java中覆盖字段。但是,您可以使用泛型方法。

/*****************Nested Accordion************************/
input:focus { outline: none; }
a {
	color: #000000;
	outline: none !important;
}

ul{
margin-top:20px;
 	list-style-type: none;
	 margin: 0;
	 padding: 0;
}
.singleColumn li h2{padding-left:20px;}
.docsAccordion {
  background-color: #fbf5db;
  font-family: 'Helvetica';
  font-weight: bold;
  font-size: 1em;
	color: #000000;
}
ul.docsAccordion > li{
	border-bottom: thick solid #ffffff;
	text-align:left;
}

 /***turn off links inside the accordion h3 h2 and links***/
  ul.docsAccordion  > li a:link{
	text-decoration:none !important;
			color:#000000 !important;
}
  ul.docsAccordion > li a:hover{
		text-decoration:none;
			color:#000000;
			padding:0px;
			margin:0px;
}
  ul.docsAccordion  > li a:visited{
	text-decoration:none !important;
			color:#000000 !important;
			
}
 ul.docsAccordion  > li h2 a:hover{
	text-decoration:none !important;
			color:#000000 !important;
}
 ul.docsAccordion  > li h2 a:visited{
	text-decoration:none !important;
			color:#000000 !important;
}

.dataTable a { /***turn on links inside the datatable***/
	text-decoration:underline !important;
	}
li.nestedElems{  /*list inside accordion*/
padding-left:5px !important;
margin-left:0px !important;
	border-bottom: none;
	background-color:#ffffff;
	text-align:left;
	border: 1px solid #eee6b5;
}
ul.insideAccor > li{
	padding: 10px 0 10px 0;
	background-color: #ffffff;
	margin-bottom: 2px;
	margin-left: 0px;
	text-align: left;
	font-weight: normal;
	font-size: 1.2em;
}

table#docListTable > tbody > tr > td a:visited{
text-decoration:none !important;
color:#800080;
}
ul.insideAccorList > li{
	padding: 5px 0 5px 60px;
	background-color: #ffffff;
	margin-bottom: 2px;
	text-align: left;
	font-weight: normal;
	font-size: 1.2em;
	border-top:1px solid #eee6b5;
}
/*style links inside accordion list*/
ul.insideAccorList li a:link{
color:#0000ff !important;
text-decoration:underline !important;
}
ul.insideAccorList li a:hover{
color:#0000ff !important;
text-decoration:underline !important;
}
ul.insideAccorList  li a:visited{
text-decoration:underline !important;
color:#800080 !important;
}
ul.indentList > li{
	padding-left:20px;
}
.noPadTop{
	padding-top: 0 !important;
}
li.nestedElemAccor{
	margin-top: -20px;
}


h2.ui-accordion-header.ui-state-default { /*add arrowhead*/
font-size:1.2em;
background-image: url("/site-resources/resource-files/img/arrow-lg.png");
background-repeat: no-repeat;
background-position:0% 50%;
width:100%;
}
h2.ui-accordion-header.ui-state-active {  /*add arrowhead*/
font-size:1.2em;
background-image: url("/site-resources/resource-files/img/arrow-lg-dn.png");
background-repeat: no-repeat;
background-position:0% 50%;
width:100%;
}
h3.ui-accordion-header.ui-state-default {  /*add arrowhead*/
font-size:1.2em;
padding-left:15px;
background-image: url("/site-resources/resource-files/img/arrow-sm.png");
background-repeat: no-repeat;
background-position:5px 3px;
width:95%;
}
h3.ui-accordion-header.ui-state-active {  /*add arrowhead*/
font-size:1.2em;
padding-left:15px;
background-image: url("/site-resources/resource-files/img/arrow-sm-dn.png");
background-repeat: no-repeat;
background-position:5px 3px;
width:95%;
}
li.NestElemTitle{
background-color: #ffffff;
padding: 3px 0px 3px 0px;
text-align:left;
}
.singleColumn h2{
font-size:1.2em;
margin-left:5px;
}
.singleColumn h3{
font-size:1.2em;
margin-left:10px;
}

然后

<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.0/jquery-ui.min.js"></script>
<script src="//cdn.datatables.net/1.10.11/js/jquery.dataTables.min.js"></script>
<link href="//cdn.datatables.net/1.10.11/css/jquery.dataTables.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<ul class = "docsAccordion">
<li>
	<h2><a href="#">(February 19, 2013 - May 12)</a></h2>
	
	</li>
	<li>
	<h2><a href="#">(February 19, 2012 - May 11)</a></h2>
		
</li>
 <!-- element which hold Individual class in accordian -->
<li>
	<h2><a href="#">September 10, 2007</a></h2>
		
	<li>
	<h2><a href="#">(February 19, 2011 - May 10)</a></h2>
		<ul class="docsAccordion nestedElemAccor">	
				<li class = "nestedElems"> 
				<h3><a href="#">December 2001 (4 Meeting days)</a></h3>
					<ul class="insideAccor">
						<li>
						<p>gsdrfg</p>
						</li>
					</ul>
				</li>
				<li class = "nestedElems">
					<h3><a href="#">February 2002 (7 Meeting days)</a></h3>	
					
				</li>
			<li class="nestedElems">
				<h3><a href="#">November 2001 (6 meetings days)</a></h3>
					<ul class="insideAccor">
						<li>
						<ul class="insideAccorList">
							<li><a href="http://www.google.ca">Monday February 3</a></li>
							<li><a href="http://www.bing.ca">Tuesday February 4</a></li>
							<li><a href="http://www.yahoo.ca">Wednesday Februaary 5</a></li>
							<li><a href="http://www.apple.ca">Thursday February 6</a></li>
							<li><a href="http://www.sony.ca">Tuesday February 11</a></li>
						</ul>
						</li>
					</ul>
			</li>
			
		</ul>
	</li>
<!-- END element which hold Individual class in accordian -->
	<li>
	<h2><a href="#">(February 19, 2010 - May 09)</a></h2>
		
	</li>
	<li>
	<h2><a href="#"> (February 19, 2009 - May 08)</a></h2>
		
	</li>
</ul>

答案 2 :(得分:1)

这是一种可接受的模式,但您应该更加具体地了解您的泛型:

public abstract class ABCModel<T extends Number> {
    protected String foo;
    protected String bar;
    protected T baz;

    public T getBaz() {
      return baz;
    }
}

之后你可以扩展你的模型:

public class ModelFoo extends ABCModel<Integer> {
     // No need to specify again baz.
}