简体   繁体   English

Java:在这种情况下如何解析字符串?

[英]Java: how to parse a string in this situation?

So I have created a simple class in Java like this: 所以我在Java中创建了一个简单的类,如下所示:

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.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 : 如果您确实想以String访问许多属性,建议您使用Map<String, String>这样:

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. 您已将Book创建为一个对象。
So, treat it like an object and add getters and setters. 因此,将其视为对象并添加getter和setter。

In this case, that would be a method, getTitle() and a separate method getAuthor() . 在这种情况下,这将是方法getTitle()和单独的方法getAuthor()
For more information on getters and setters, see the responses to this previous StackOverflow post 有关getter和setter的更多信息,请参见对此之前StackOverflow帖子的回复

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. 首先,因为title是私有的,所以您的代码将无法工作。 Second, I have no idea why you set Book class as static. 其次,我不知道为什么将Book类设置为静态。 Last, this (Java) is object oriented programming, so treat it like an object. 最后,此(Java)是面向对象的编程,因此应将其视为对象。

When you create a class you also add Getters & Setters to access the information inside. 创建课程时,您还需要添加Getters和Setters来访问其中的信息。 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 . 这里authortitle都是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. 因此,您需要添加可用于访问属性的公共getterssetters

you should change you Object class.. add getter and setter method.. here is example : 你应该改变你的对象类..添加getter和setter方法..这是例子:

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。

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

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