简体   繁体   中英

Java: how to parse a string in this situation?

So I have created a simple class in Java like this:

public class Book {
    private String author;
    private String title;

    public Book (String author, String title) {
        this.author = author;
        this.title = title;
    }
}

public void checkInfo     

Is there a way to parse a string (property) in order to get Book properties like this, instead of doing bookA.title ?

Book bookA = new Book("George Orwell","Animal Farm")

String property = "title";
System.out.print(bookA.property);

Thanks in adance!

If you really want to access many properties as String , I suggest you using a Map<String, String> like this :

public class Book
{
    private Map<String, String> properties = new HashMap();

    public void setProperty(String name, String value)
    {
        properties.set(name,string);
    }

    public String getProperty(String name)
    {
        return properties.get(name);
    }
}

Now you can use like this :

Book book = new Book();

book.setProperty("title","Animal Farm");
book.setProperty("author","George Orwell");

System.out.println("Book: " + book.getProperty("title") + " by " + book.getProperty("author"))

You've created your Book as an object.
So, treat it like an object and add getters and setters.

In this case, that would be a method, getTitle() and a separate method getAuthor() .
For more information on getters and setters, see the responses to this previous StackOverflow post

You can use reflection:

 Field f = bookA.getClass().getDeclaredField("title");
 f.setAccessible(true);
 String title = (String) f.get(bookA);
 System.out.println(title);

First of all, your code won't work because title is private. Second, I have no idea why you set Book class as static. Last, this (Java) is object oriented programming, so treat it like an object.

When you create a class you also add Getters & Setters to access the information inside. The code would look like this:

Class :

public class Book {
    private String author;
    private String title;

    public Book (String author, String title) {
        this.author = author;
        this.title = title;
    }
}

public String getTitle(){
    return this.title;
}

public String getAuthor(){
    return this.author;
}

Accessing the data :

Book bookA = new Book("George Orwell","Animal Farm")

System.out.print("Book: " + bookA.getTitle() + " by " + bookA.getAuthor());

This would return :

Book: Animal Farm by George Orwell

If you see these few lines from your code:

private String author;  // both are private variables
private String title;  

Here author and title both are private String . So you can't access these properties outside of the class.

So, you'll need to add public getters and setters that can be used to access the properties.

you should change you Object class.. add getter and setter method.. here is example :

public class Book{ 
  String myauthor;
  String mytitle;
public Book (String author, String title){
  myauthor=author;
  mytitle=title;
}
public void setAuthor(String Autor){
myauthor=author;
}
public String getAuthor(){
return myauthor;
}
}

and create setter and getter for 'title' too.. if you want to get the title / author, just simply call

Book.getAuthor();

如果您不想在类中使用getter / setter方法,则可以将访问修饰符定义为受static关键字保护的示例,例如:在com.test包下-有两个类,一个是Book类,另一个是BookInSamePackage在Book类中;如果您将属性标题定义为受保护的静态String标题,则在BookInSamePackage类中;您可以这样访问:'Book.title'。如果要在另一个包的类中使用此title属性,则该类需要扩展Book类并可以这样访问:另一个包的子类中的Book.title。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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