成都创新互联网站制作重庆分公司

方法名称与泛型相同怎么在Java中使用

方法名称与泛型相同怎么在Java中使用?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

成都创新互联公司是一家专注于网站设计、成都网站制作与策划设计,隆安网站建设哪家好?成都创新互联公司做网站,专注于网站建设十多年,网设计领域的专业建站公司;建站业务涵盖:隆安等地区。隆安做网站价格咨询:18980820575

Java中,方法的名称可以用泛型替代。

1 代码

public class SupGent {
  public class A {
    E t;
    public A( E t ) {
      this.t = t;
    }
    public E E() {  //采用了泛型E,碰巧方法名称也是E,只不过不要弄混淆,有点像宏替换
      return t;
    }
  }
  public class B extends A {
    public B( E t ) {
      super(t);
    }
  }
  public static void main( String[] args ) {
    B b = (new SupGent()).new B("test");
    System.out.println(b.E());
  }
}

2 运行

test

3 说明

和下面代码等价

public class SupGent {
  public class A {
    E t;
    public A( E t ) {
      this.t = t;
    }
    public E String() {
      return t;
    }
  }
  public class B extends A {
    public B( E t ) {
      super(t);
    }
  }
  public static void main( String[] args ) {
    B b = (new SupGent()).new B("test");
    System.out.println(b.String());
  }
}

看完上述内容,你们掌握方法名称与泛型相同怎么在Java中使用的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


新闻标题:方法名称与泛型相同怎么在Java中使用
分享网址:http://cxhlcq.com/article/gsephh.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部