Created
August 24, 2011 03:22
-
-
Save brantYan/1167243 to your computer and use it in GitHub Desktop.
the abstract class can not be write in List[T]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I'm reading the IBM developer scala guide | |
http://www.ibm.com/developerworks/cn/java/j-scala05059.html | |
and write the weatherReport copy form example | |
WeatherReport.scala | |
import org.apache.http.impl.client.DefaultHttpClient | |
import org.apache.http.HttpResponse | |
import org.apache.http.util.EntityUtils | |
object WeatherTest { | |
def main(args: Array[String]) = { | |
new WeatherTest().callTwitterTest | |
} | |
} | |
class WeatherTest { | |
import org.apache.http.client._, methods._ | |
def callTwitterTest = { | |
val testURL = "http://www.google.com/ig/api?weather=Beijing" | |
// HttpClient API 4 | |
val client: HttpClient = new DefaultHttpClient() | |
val method: HttpUriRequest = new HttpGet(testURL) | |
val response: HttpResponse = client.execute(method) | |
val responseString: String = EntityUtils.toString(response.getEntity) | |
val c = xml.XML.loadString(responseString) | |
val forecastConditionsList = new List[forecastConditions] | |
for (n <- (c \\ "forecast_conditions").elements) | |
forecastConditionsList += (forecastConditions.fromXml(n)) | |
forecastConditionsList.forall(x -> println(x.toString())) | |
} | |
} | |
WeatherType.scala | |
abstract class forecastConditions { | |
val dayOfWeek: String | |
val lowTemperature: String | |
val highTemperature: String | |
val iconUrl: String | |
val condition: String | |
} | |
object forecastConditions { | |
/* | |
<forecast_conditions> | |
<day_of_week data="Fri"/> | |
<low data="64"/> | |
<high data="82"/> | |
<icon data="/ig/images/weather/mostly_sunny.gif"/> | |
<condition data="Mostly Sunny"/> | |
</forecast_conditions> | |
*/ | |
def formXml(node: scala.xml.Node): forecastConditions = { | |
new forecastConditions { | |
val dayOfWeek = (node \\ "day_of_week").text | |
val lowTemperature = (node \\ "low").text | |
val highTemperature = (node \\ "high").text | |
val iconUrl = (node \\ "icon").text | |
val condition = (node \\ "condition").text | |
override def toString():String = { | |
dayOfWeek+" "+ lowTemperature+" "+highTemperature+" "+iconUrl+" "+ condition | |
} | |
} | |
} | |
} | |
the compile show | |
error: class List is abstract; cannot be instantiated | |
val forecastConditionsList =new List[forecastConditions] |
Author
brantYan
commented
Aug 24, 2011
我建议你看看program in scala先。
val forecastConditionsList:List[forecastConditions] = List.empty
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment