简体   繁体   English

java非字符串类型的泛型类

[英]java generic classes of String non primitive type

I'm studying about java generic classes. 我正在研究Java泛型类。 Create one generic class with T as type parameter. 用T作为类型参数创建一个通用类。

public class Genericstring<T> {
    String name;

    public void set(String name){
        this.name = name;
    }

    public static void main(String[] args){
        Genericstring<String> o = new Genericstring<String>();
        o.set("Generic");

        System.out.println(o.name);
    }
}

In above example, the type of T could string, integer or any other type. 在上面的示例中,T的类型可以是字符串,整数或任何其他类型。 But is that possible I could create generic class with specific type argument. 但是有可能我可以创建具有特定类型参数的泛型类。 Like below example, but its showing error "Cannot make a static reference to the non-static type String" 类似于下面的示例,但显示错误“无法对非静态类型的String进行静态引用”

public class Genericstring<String> {
    String name;

    public static void main(String[] args){     
        //Here creating object always having string type attributes. No need to specify like did in above example.
    }

Can anyone tell me, Is that possible to create generic class with String as parameter Class generictype ? 谁能告诉我,是否可以用String作为参数创建类的通用类generictype If not, then why ? 如果没有,那为什么呢?

Genericstring<String> is declaring a type variable called String . Genericstring<String>声明一个名为String的类型变量。

This makes all other uses of String refer to that type variable, not java.lang.String . 这使得String所有其他用法都引用该类型变量,而不是java.lang.String Where you've got the argument to main declared as String[] , it thinks you mean the type variable. main的参数声明为String[] ,它认为您的意思是类型变量。 And because this is a static method, you can't refer to that type parameter there, because it belongs to instances of the class. 而且由于这是一个静态方法,因此您不能在那里引用该类型参数,因为它属于该类的实例。

But is that possible I could create generic class with specific type argument. 但是有可能我可以创建具有特定类型参数的泛型类。

What you're describing is a non-generic class. 您要描述的是一个非泛型类。 Remove the type parameter. 删除类型参数。

public class Genericstring {
  String name;

  // Etc
}

No, you can't. 不,你不能。

This code: Genericstring<String> is only naming the Generic Type Variable rather than setting as java.lang.String. 这段代码: Genericstring<String>仅命名Generic Type Variable,而不是设置为java.lang.String。

The most you can do is declaring a generic type that extends a specific Class: 您最多可以做的是声明一个扩展特定类的泛型类型:

public class Genericstring<T extends String>

Example: 例:

abstract class B<T extends String> {
    protected T value;

    public T getValue() {
        return value;   
    }

    public void handleValue() {
        System.out.println(this.getValue().substring(0, 5));
    }
}

class C extends B<String>{
    public C() {
        this.value = "Hello World";
    }
}

public class A {
    public static void main(String[] args) {
        C obj = new C();
        obj.handleValue(); // prints Hello
    }
}

UPDATED: According to the comments, my answer has generated a lot of confusion because the way I've implemented. 更新:根据评论,由于我的实施方式,我的回答引起了很多困惑。

@linuxman this is not the only way to accomplish your scenario and your right when you're mentioning Cant we mention like only B or B<T extends String> ? @linuxman这不是完成您的方案的唯一方法,当您提到Cant时,您的权利就是我们提到的,就像B or B<T extends String> :

class B<T extends String> {
    protected T value;

    public T getValue() {
        return value;
    }

    public void handleValue() {
        System.out.println(this.getValue().substring(0, 5));
    }
}

public class A {
    public static void main(String[] args) {
        B obj = new B<String>();
        obj.handleValue();
    }
}

@Pshemo is right: @Pshemo是正确的:

In this scenario only type available for is String itself, but if it is only one type then there is no point in having a generic type because it main purse is to allow us to use our class with many types. 在这种情况下,仅可用于字符串的类型是String本身,但是如果仅是一种类型,则没有通用类型是没有意义的,因为它的主要目的是允许我们将我们的类用于许多类型。

Also, about your doubt regarding final classes: 另外,关于您对期末课程的疑问:

Reference: https://stackoverflow.com/a/5181618/1715121 参考: https : //stackoverflow.com/a/5181618/1715121

A final class is simply a class that can't be extended . final课程就是不能扩展的课程。

(This does not mean that all references to objects of the class would act as if they were declared as final .) (这并不意味着所有对该类对象的引用都将像声明为final那样起作用。)

When it's useful to declare a class as final is covered in the answers of this question: 当将一个类声明为final有用时,请参见此问题的答案:

If Java is object oriented, and you declare a class final , doesn't it stop the idea of class having the characteristics of objects? 如果Java是面向对象的,并且您声明了一个final类,它是否会停止具有对象特征的类的想法?

In some sense yes. 从某种意义上说是的。

By marking a class as final you disable a powerful and flexible feature of the language for that part of the code. 通过将一个类标记为final,可以禁用该部分代码的强大而灵活的语言功能。 Some classes however, should not (and in certain cases can not) be designed to take subclassing into account in a good way. 有些类然而,不应该(在某些情况下不能 )设计充分继承考虑的一个好办法。 In these cases it makes sense to mark the class as final, even though it limits OOP. 在这些情况下,将类标记为最终类是有意义的,即使它限制了OOP。 (Remember however that a final class can still extend another non-final class.) (但是请记住,最后一堂课仍然可以扩展另一个非最后一课。)

Related article: Java: When to create a final class 相关文章: Java:何时创建最终类


@linuxman What you're trying to accomplish doesn't make sense because the purpose of a Generic Type is to declare contracts that could implement a behavior with different types. @linuxman您要完成的工作没有任何意义,因为通用类型的目的是声明可以实现不同类型行为的合同。

Hope it helps and clears your doubts. 希望它能帮助您消除疑虑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM