![](/img/trans.png)
[英]How to read and test a class from java extension file in junit? How can I access that class for testing?
[英]How can I read from a java class?
我真的剛開始使用Android開發人員和Objective編程,所以這個問題對您來說是如此愚蠢。.我知道,但我想問一下,我如何從Java類中讀取內容?
這是我的情況:我來自C和C ++,並且必須保留XML文件中的數據。 數據是關於地點的,因此項目為:“名稱”,“地址”,“電話號碼”等等。 現在,如果我使用C語言,也許我會做一個數組或一個列表,但是,我想學習Java和Objective編程,所以我用所有字段制作了一個名為“ PointOfInterest”的類,但是我不知道很好地寫在上面,我不知道該如何從類中讀取內容。例如,如果我使用C或C ++,並且標題為to,則可以水平移動水平,例如標題等於字段“ 0”。 但是使用Java類?
我能怎么做? 這是我的代碼:
package com.example.findmyclients;
public class PointOfInterest {
private String title;
private String address;
private String telephone;
private String email;
private String description;
private String facebook;
private String twitter;
private String website;
public void AnyPoint(String nome, String indirizzo, String telefono, String email, String description, String facebook, String twitter, String website){
//super();
this.title = nome;
this.address = indirizzo;
this.telephone = telefono;
this.email = email;
this.description = description;
this.facebook = facebook;
this.twitter = twitter;
this.website = website;
}
public String prova(String nome){
if(title.contains(nome)){
return this.address;
}
return nome;
}
}
這是我“嘗試”將xml中的數據保存到類中:
for (int i = 0; i < markers.getLength(); i++) {
//PointOfInterest p = point.get(i+1);
Element item = (Element) markers.item(i);
String name = item.getAttribute("name");
String address = item.getAttribute("address");
String stringLat = item.getAttribute("lat");
String stringLong = item.getAttribute("long");
String icon = item.getAttribute("icon"); //assigned variable for the XML icon attribute
String desc = item.getAttribute("descrizione");
String tel = item.getAttribute("tel");
String email = item.getAttribute("email");
String facebook = item.getAttribute("facebook");
String twitter = item.getAttribute("twitter");
String web = item.getAttribute("web");
Double lat = Double.valueOf(stringLat);
Double lon = Double.valueOf(stringLong);
PointOfInterest p = new PointOfInterest();
p.AnyPoint(name,address,tel,email,desc,facebook,twitter,web);
[...]
恐怕會誤解您想獲得的東西,正是我不明白“如何從課堂上讀書”的含義
但是,如果我正確理解它,您可能希望將Java Collections Framework .like用作HashMap或TreeMap。
如果要使用某些“鍵”獲取PointOfInterest實例,則可以按照以下步驟進行操作。
Map<String, PointOfInterest> nameMap = new HashMap<<String, PointOfInterest>();
nameMap.put(p.getName, p);
PointOfInterest anotherP = nameMap.get("foo");
if (anotherP != null) { // there is a instance with a name of "foo"
...
}
順便說一句,我強烈建議使用Immutable對象。 例如
public class PointOfInterest {
private final String title;
private final String address;
private final String telephone;
....
//constructure
public PointOfInterest(String title, String address, ... ) {
this.title = title;
....
}
....
}
如果這不是您想知道的,請讓我理解。 我希望有辦法可以為您提供幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.