首页 \ 问答 \ Tricky Java Generics:使用泛型方法实现非泛型接口的泛型类(Tricky Java Generics : generic class implementing non generic interface with generic Method)

Tricky Java Generics:使用泛型方法实现非泛型接口的泛型类(Tricky Java Generics : generic class implementing non generic interface with generic Method)

在ideone.com中看到以下代码:

import java.util.*;

class Test{
   interface Visitor{
        public <T> void visit(T Value);
   }

   class MyVisitor<T> implements Visitor{
        List<T> list = new  ArrayList<T>();

        public <T> void visit(T value){
           list.add(value);
        }
    }
}

编译时,此代码将产生以下+错误:

 Main.java:12: error: no suitable method found for add(T#1)
             list.add(value);
                 ^
     method List.add(int,T#2) is not applicable
       (actual and formal argument lists differ in length)
     method List.add(T#2) is not applicable
       (actual argument T#1 cannot be converted to T#2 by method invocation conversion)   where T#1,T#2 are type-variables:
     T#1 extends Object declared in method visit(T#1)
     T#2 extends Object declared in class Test.MyVisitor 1 error

问题是访问中的类型 T在列表中不被视为相同的T. 我该如何解决这个编译问题?


I have the following code as seen in ideone.com:

import java.util.*;

class Test{
   interface Visitor{
        public <T> void visit(T Value);
   }

   class MyVisitor<T> implements Visitor{
        List<T> list = new  ArrayList<T>();

        public <T> void visit(T value){
           list.add(value);
        }
    }
}

When compiled this code will produce the following+ error:

 Main.java:12: error: no suitable method found for add(T#1)
             list.add(value);
                 ^
     method List.add(int,T#2) is not applicable
       (actual and formal argument lists differ in length)
     method List.add(T#2) is not applicable
       (actual argument T#1 cannot be converted to T#2 by method invocation conversion)   where T#1,T#2 are type-variables:
     T#1 extends Object declared in method visit(T#1)
     T#2 extends Object declared in class Test.MyVisitor 1 error

The problem is that the type T in visit is not considered the same T in list . How can I fix this compilation problem?


原文:https://stackoverflow.com/questions/15947506
更新时间:2023-07-09 22:07

最满意答案

当我看到你用来从主题字段获取值作为.GetAttribute("Value") ,这里唯一的问题是将属性属性作为Value传递, Value应该是value表示v应该是小写的,所以你应该尝试如下: -

string actualSubject = driver.FindElement(By.Id("subject-field")).GetAttribute("value");

或者使用WebDriverWait等到DOM上的元素存在,如下所示:

var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement element = wait.Until(ExpectedConditions.ElementExists(By.Id("subject-field")));
string actualSubject = element.GetAttribute("value");

我测试了它,它适用于我。

希望能帮助到你...:)


As I seeing you are using to get value from subject field as .GetAttribute("Value"), here only problem is to passing attribute property as Value which should be value means v should be in lowercase, So you should try as below :-

string actualSubject = driver.FindElement(By.Id("subject-field")).GetAttribute("value");

Or using WebDriverWait to wait until element present on DOM as below :

var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement element = wait.Until(ExpectedConditions.ElementExists(By.Id("subject-field")));
string actualSubject = element.GetAttribute("value");

I have tested it and it works for me.

Hope it helps...:)

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。