在农场系统例子中,如何调用Apple,Grape类中的SET/GET方法?是不是作者错了?
Apple和Grape类实现了Fruit接口,按照作者的意思,应该如下调用:
public class a
{
public static void main(String[] args)
{
FruitGardener a=new AppleGardener();
a.factory().“这里如何调用SET/GET方法?返回的是Fruit类型呀,难道要强制转换?”;
}
}
Apple和Grape类实现了Fruit接口,按照作者的意思,应该如下调用:
public class a
{
public static void main(String[] args)
{
FruitGardener a=new AppleGardener();
a.factory().“这里如何调用SET/GET方法?返回的是Fruit类型呀,难道要强制转换?”;
}
}
解决方案 »
- 关于留言板中怎样制作表情?
- 各位有经验,很有经验,极其特别非常有经验的达人,给小弟支个招吧
- jsp web编程时遇到<A href='a.xml'>下载xml</a>时,直接在ie中打开了,请问如何能点击后下载而不是打开,谢谢!
- 用数据库链接池的程序怎么改写成用javabean的呢?
- 想试着用Java在lotus Notes里面写代理,谁能给几个简单的例子
- 请帮我一下看一下,为什么有这个问题,奇怪
- 如何整合tomcat和apache
- 怎样知道用户输入得数据是英文还是中文?
- 数据库连接池中多个连接池和单个连接池之间的区别
- 为何二次结果不同??
- jsp中缓冲区的问题
- 在应用CachedRowSetImpl结束时,是不是一定要显式调用release和close方法?
{
public Fruit factory();
}public interface Fruit
{
void grow(); void harvest(); void plant();
}public class AppleGardener implements FruitGardener
{
public Fruit factory()
{
return new Apple();
}
}public class Apple implements Fruit
{
private int treeAge; public void grow()
{
System.out.println("Apple is growing...");
} public void harvest()
{
System.out.println("Apple has been harvested.");
} public void plant()
{
System.out.println("Apple has been planted.");
} public int getTreeAge()
{
return treeAge;
} public void setTreeAge(int treeAge)
{
this.treeAge = treeAge;
}}
a.factory()new AppleGardener()返回一個AppleGardener類型的對象,賦值給FruitGardener(傳第的是一個 引用),這裡是"上傳".
對象類型自動變化成基類的類型,但此時在a的實際對象是個AppleGardener
類型的.a.factory(),調用的是AppleGardener類的方法,返回值為Apple類型的對象.