如何用泛型实现接口方法

时间:2018-01-19 03:51:28

标签: java generics inheritance interface override

public interface A {
  public void setStar(Star star)
}

public class A-Class implements A {
 public void setStar(StarImpl star)
}

public interface Star {
}

public class StarImpl implements Star {
}

基本上,我想将接口A应用于A-Class。但它给了我错误:A-Class is not abstract and does not override abstract method setStar(Star)

我知道原因是A级中的setStar()与界面A中的public class A-Class implements A { public void setStar(<? extends Star> star) } public class A-Class implements A { public <T extends Star> void setStar(T star) } 不完全相同但是我不知道如何使用泛型来修复它?有任何想法吗?非常感谢你!

我尝试了什么:

function getYouTubeId($url) {
// Format all domains to http://domain for easier URL parsing
str_replace('https://', 'http://', $url);
if (!stristr($url, 'http://') && (strlen($url) != 11)) {
    $url = 'http://' . $url;
}
$url = str_replace('http://www.', 'http://', $url);

if (strlen($url) == 11) {
    $code = $url;
} else if (preg_match('/http:\/\/youtu.be/', $url)) {
    $url = parse_url($url, PHP_URL_PATH);
    $code = substr($url, 1, 11);
} else if (preg_match('/watch/', $url)) {
    $arr = parse_url($url);
    parse_str($url);
    $code = isset($v) ? substr($v, 0, 11) : false;
} else if (preg_match('/http:\/\/youtube.com\/v/', $url)) {
    $url = parse_url($url, PHP_URL_PATH);
    $code = substr($url, 3, 11);
} else if (preg_match('/http:\/\/youtube.com\/embed/', $url, $matches)) {
    $url = parse_url($url, PHP_URL_PATH);
    $code = substr($url, 7, 11);
} else if (preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=[0-9]/)[^&\n]+|(?<=v=)[^&\n]+#", $url, $matches) ) {
    $code = substr($matches[0], 0, 11);
} else {
    $code = false;
}

if ($code && (strlen($code) < 11)) {
    $code = false;
}

return $code;
}

他们两个都不起作用。再次,在此先感谢!

1 个答案:

答案 0 :(得分:0)

public interface A<T extends Star> {
  public void setStar(T star)
}

经过一番尝试,我自己解决了这个问题。基本上,它是将通用添加到接口A.谢谢,伙计们!