search

Home  >  Q&A  >  body text

java - public <T> T findOne(T record) what does this mean

means the return type can be <T> or T

女神的闺蜜爱上我女神的闺蜜爱上我2735 days ago1045

reply all(2)I'll reply

  • 大家讲道理

    大家讲道理2017-07-03 11:45:17

    The answer above has been very professional... I will answer it in a vernacular way, haha
    For example, when I see a method, it might look like this

    public BigCar find(Long carId)

    The premise that there is no problem in compiling this method is that BigCar is an existing class. I want to query the big car BigCar

    based on the ID of a car.

    At this time, you still want to write a query method for the small car SmallCar. You should probably create a SmallCar first, and then write a query method

    public SmallCar find(Long carId)

    No problem, at this time, if you want to write a way of querying based on ID whether it is a big car or a small car, the way to write it is

    public XXX find(Long carId)

    Then the compiler will definitely be worried. What the hell is your XXX? There is no such class
    Knock on the blackboard! ! ! Here comes the point
    There is a grammar at this time. If you want to indicate that this XXX is just a referent, but you don’t know what it is specifically, then use <> to express it. You can write n referents in it Things separated by commas, this is written as

    public <XXX> XXX find(Long carId)

    So, <> just represents a definer, which defines a bunch of variables for subsequent use. If you define it in a method, then the scope of application of this variable is in this method. If you define it in a class Defined in, hey, then this variable can be used in the entire class, for example:

    public class Car<X, XX>{
        X x;
        XX xx;
        Long id;
    
        public X findX(){
            return x;
        }
    }

    reply
    0
  • typecho

    typecho2017-07-03 11:45:17

    The way to write Java's generic method, the syntax stipulates
    The return type is T
    <T> indicates that this is a generic method (I guess it is related to compilation, after all, Java generics are erased

    reply
    0
  • Cancelreply