简体   繁体   English

如何从Java类读取?

[英]How can I read from a java class?

I'm at really beginning with Android dev and Objective programming so this question will be so stupid for you.. I know, but I wanna ask, how can I read from a java class? 我真的刚开始使用Android开发人员和Objective编程,所以这个问题对您来说是如此愚蠢。.我知道,但我想问一下,我如何从Java类中读取内容?

This is my case: I came from C and C++ and I have to keep datas from an XML file. 这是我的情况:我来自C和C ++,并且必须保留XML文件中的数据。 The datas are about places, so the items are: "Name", "Address", "Telephone number" and so on. 数据是关于地点的,因此项目为:“名称”,“地址”,“电话号码”等等。 Now, if I was in C maybe I will do an Array or a List, but, I wanna learn Java and the Objective programming so I made a class named "PointOfInterest" with all field, but I don't know if I'm writing well onto it and I don't know how to read from the class.. If I was in C or C++ with to for cicles I could go horizontally when the title is equal to the field "0" for example. 现在,如果我使用C语言,也许我会做一个数组或一个列表,但是,我想学习Java和Objective编程,所以我用所有字段制作了一个名为“ PointOfInterest”的类,但是我不知道很好地写在上面,我不知道该如何从类中读取内容。例如,如果我使用C或C ++,并且标题为to,则可以水平移动水平,例如标题等于字段“ 0”。 But with Java classes?? 但是使用Java类?

How can I do? 我能怎么做? This is my code: 这是我的代码:

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;
    }
}

and this is my "try" to keep into the class datas from xml: 这是我“尝试”将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);

[...] [...]

I'm afraid to misunderstand what you want to get, precisely I don't get to the meaning of 'how to read from the class' 恐怕会误解您想获得的东西,正是我不明白“如何从课堂上读书”的含义

Nevertheless if I understand it correctly, you may want to use Java Collections Framework .like as HashMap or TreeMap. 但是,如果我正确理解它,您可能希望将Java Collections Framework .like用作HashMap或TreeMap。

If you want to get PointOfInterest instance with some 'key', you can do it as followings. 如果要使用某些“键”获取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"
   ...
} 

By the way, I strongly want to suggest to use Immutable object. 顺便说一句,我强烈建议使用Immutable对象。 For example 例如

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;
        ....
    }
  ....
 }

If this is not what you want to know, please let me understand it. 如果这不是您想知道的,请让我理解。 I hope there is a way that I can help you. 我希望有办法可以为您提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM