gpt4 book ai didi

java - Java中如何保留类型变量的原始类型?

转载 作者:行者123 更新时间:2023-12-01 18:42:41 26 4
gpt4 key购买 nike

我有以下示例:

class bounds
{
private StringBuilder str = new StringBuilder();

public <Type> void add (Type value)
{
add_specific (value);
str.append (String.format("%n"));
}

private <Type extends Number> void add_specific (Type value)
{
str.append (value);
}

public String toString () { return str.toString(); }

public static void main (String[] args)
{
bounds test = new bounds();
test.add (new Integer (42));
System.out.print (test.toString());
}
}

当我尝试编译它时,出现以下错误:

bounds.java:7: error: method add_specific in class bounds cannot be applied to given types;    add_specific (value);    ^  required: Type#1  found: Type#2  reason: inferred type does not conform to declared bound(s)    inferred: Type#2    bound(s): Number  where Type#1,Type#2 are type-variables:    Type#1 extends Number declared in method add_specific(Type#1)    Type#2 extends Object declared in method add(Type#2)1 error

This looks to me as if the original type of the argument passed to the add method gets lost in the body of add. How can I preserve the type so that the correct add_specific method can be chosen?

Update

I have simplified my example, because I thought it would be easier to understand. But it seems to me that most people do not understand the reason why it contains a generic and a specific function. So I paste a more advanced example. Maybe this makes the reasons more obvious:

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

class bounds
{
private StringBuilder str = new StringBuilder();

public <Type> void add (Type value)
{
add_specific (value);
str.append (String.format("%n"));
}

private <Type extends Number> void add_specific (Type value)
{
str.append (value);
}

private void add_specific (String value)
{
str.append ('"');
for (int i = 0; i < value.length(); i++) {
char ch = value.charAt(i);
switch (ch) {
case '\\': str.append ("\\\\"); break;
case '"': str.append ("\\\""); break;
default: str.append (ch);
}
}
str.append ('"');
}

private static DateFormat iso8601
= new SimpleDateFormat("'\"'yyyy-MM-dd'T'HH:mm:ssZ'\"'");

private void add_specific (Date date)
{
str.append (iso8601.format(date));
}

public String toString ()
{
return str.toString();
}

public static void main (String[] args)
{
bounds test = new bounds();

test.add (new Integer (42));
test.add ("42");
test.add (new Date());

System.out.print (test.toString());
}
}

我有一个名为add的通用函数。该通用函数执行通用操作并调用特定函数来执行特定操作。问题是,选择特定函数的类型在通用函数中丢失了。问题是如何解决这个问题?我必须如何编写通用函数,以便仍然可以在通用函数主体中选择正确的特定函数?

最佳答案

This looks to me as if the original type of the argument passed to the add method gets lost in the body of add.

嗯,类型删除意味着 Type在执行时不会知道,但这并不是您收到编译时错误的直接原因。您可以调用add具有任何类型 - 而您只能调用 add_specificNumber 兼容的类型。例如,考虑:

// TODO: Fix your naming to meet Java naming conventions
bounds b = new bounds();
b.<String>add("foo");

您希望如何调用 add_specificString不扩展Number .

(顺便说一句,将类型参数命名为 Type 也是一个非常坏主意 - 很容易将其与 java.lang.reflect.Type 混淆。)

选项:

  • 添加 extends Number绑定(bind)到Typeadd
  • 删除 Type 的绑定(bind)在add_specific

编辑:好的,听起来您只给了我们一半的图片 - 您期望在执行时基于 Type 执行重载解析。 。这是行不通的,原因有两个:

  • Java总是在编译时执行重载解析
  • 类型删除意味着在执行时不存在信息来作为重载决策的基础。如果您需要该信息,则需要另一个 Class<Type> 类型的参数。 .

关于java - Java中如何保留类型变量的原始类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19251576/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com