有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java中的C++ Type?

在Java中是否使用与C++ Tyjava相类似的东西?在C++中,我会写

using LatLng = std::pair<double, double>;

共 (2) 个答案

  1. # 1 楼答案

    Java中没有类型别名

    也没有类似decltype的东西

  2. # 2 楼答案

    最接近decltype的可能是在java中使用泛型

    /**
     * @author OldCurmudgeon
     * @param <P> - The type of the first.
     * @param <Q> - The type of the second.
     */
    public class Pair<P extends Comparable<P>, Q extends Comparable<Q>> implements Comparable<Pair<P, Q>> {
      // Exposing p & q directly for simplicity. They are final so this is safe.
      public final P p;
      public final Q q;
    
      public Pair(P p, Q q) {
        this.p = p;
        this.q = q;
      }
    
      public P getP() {
        return p;
      }
    
      public Q getQ() {
        return q;
      }
    
      @Override
      public String toString() {
        return "<" + (p == null ? "" : p.toString()) + "," + (q == null ? "" : q.toString()) + ">";
      }
    
      @Override
      public boolean equals(Object o) {
        if (!(o instanceof Pair)) {
          return false;
        }
        Pair it = (Pair) o;
        return p == null ? it.p == null : p.equals(it.p) && q == null ? it.q == null : q.equals(it.q);
      }
    
      @Override
      public int hashCode() {
        int hash = 7;
        hash = 97 * hash + (this.p != null ? this.p.hashCode() : 0);
        hash = 97 * hash + (this.q != null ? this.q.hashCode() : 0);
        return hash;
      }
    
      @Override
      public int compareTo(Pair<P, Q> o) {
        int diff = p == null ? (o.p == null ? 0 : -1) : p.compareTo(o.p);
        if (diff == 0) {
          diff = q == null ? (o.q == null ? 0 : -1) : q.compareTo(o.q);
        }
        return diff;
      }
    
    }