简体   繁体   English

Client.findById(id)是什么意思?

[英]What does `Client.findById(id)` mean?

Looking through the Play documentation for Java I noticed the following block of code: 浏览Java的Play文档时,我注意到以下代码块:

public static Result show(Long id) {
  Client client = Client.findById(id);
  return ok(views.html.Client.show(client));
}

Source: http://www.playframework.com/documentation/2.1.0/JavaRouting 来源: http//www.playframework.com/documentation/2.1.0/JavaRouting

I am having some trouble understanding the second line, my understanding of Java Object creation is a typical constructor looks like the following: 我在理解第二行时遇到了一些麻烦,我对Java对象创建的理解是一个典型的构造函数,如下所示:

Person john = new Person();

What is the second line doing? 第二行在做什么? Creating a Object called client from Class called Client, also what is Client? 从名为Client的类中创建一个名为client的对象,Client又是什么? It doesn't appear to be a part of the Play Framework, certainly I cannot find anything in JavaDocs. 它似乎不是Play框架的一部分,当然我在JavaDocs中找不到任何内容。

Thanks 谢谢

Edit: 编辑:

I found this to be a good point of reference for the answer ( http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html ) 我发现这是答案的好参考点( http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

Also I think the class Client comes from the following documentation ( http://www.playframework.com/documentation/1.1.1/controllers ) with Client being just a example model class, the new documentation probably needs updating to clear up this confusion. 另外,我认为Client类来自以下文档( http://www.playframework.com/documentation/1.1.1/controllers ),其中Client仅作为示例模型类,因此可能需要更新新文档以消除这种混淆。

There must be a static method called show(Client) on the views.html.Client class that returns some object. views.html.Client类上必须有一个名为show(Client)static方法,该方法返回一些对象。 That object is passed into an ok(whatever) method, and that ok method returns a Result object. 该对象被传递到ok(whatever)方法中,并且该ok方法返回一个Result对象。

Pretty clearly, the class Client has a static function of findById , which takes a Long and returns a Client . 很明显,类Client具有静态函数findById ,该函数采用Long并返回Client Static functions are functions that are defined without any access to object properties, and therefore can be accessed through the class name, rather than having to be accessed through an object. 静态函数是定义的函数,无需访问对象属性,因此可以通过类名进行访问,而不必通过对象进行访问。 Most likely, the class has a static property containing a collection of all clients in the system by index, and findById grabs an existing Client from that list. 该类最有可能具有静态属性,该属性包含按索引包含系统中所有客户端的集合,并且findById从该列表中获取现有的Client

I really have no idea where the class Client is defined, however. 但是,我真的不知道Client类的定义位置。 I've also made a quick look around for it, and couldn't find it in the obvious places. 我也快速浏览了一下,但在明显的地方找不到它。

You're missing some basic knowledge/experience. 您缺少一些基本知识/经验。 The sample you gave has nothing to do with routes and in this snippet only first line is important, second is just some hypothetical usage. 您提供的示例与routes无关,在此代码段中,第一行很重要,第二行只是一些假设的用法。 De facto it could be just... 事实上,可能只是...

public static Result show(Long id) {
  return ok("You want to display details of client with ID: " + id);
} 

Although @BenBarden explained what is that mean correctly , this static method isn't declared anywhere, it's (again) hyphotetical usage of some ORM. 尽管@BenBarden正确解释了这是什么意思,但此静态方法在任何地方声明,而是(再次)对某些ORM进行了表象化的用法。 For an example the real usage with Ebean's model will be: 例如,Ebean模型的实际用法是:

Client = Client.find.byId(id);

Of course you can also declare own method in your Client model and name it the same as in the sample, however it will be just only wrapper: 当然,您也可以在Client模型中声明自己的方法,并将其命名为与示例中的方法相同的名称,但是它仅是包装器:

public static Finder<Long, Client> find
        = new Finder<>(Long.class, Client.class);

public Client findById(Long id) {
    return find.byId(id);
}

Conclusions 结论

  • You need to examine some samples available with your Play sources to get familiar with some basic syntax, fortunately you'll find it easy. 您需要检查Play来源提供的一些示例,以熟悉一些基本语法,所幸您会发现它很简单。
  • DO NOT MIX documentation from Play 1.x with Play 2.x they are not compatible! 不要将 Play 1.x中的文档与Play 2.x中的文档兼容!

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

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