How to create a Java class, similar to a C++ template class?

后端 未结 4 1736
一向
一向 2020-12-29 05:28

How do I write an equivalent of this in Java?

// C++ Code

template< class T >
class SomeClass
{
private:
  T data;

public:
  SomeClass()
  {
  }
  vo         


        
4条回答
  •  执念已碎
    2020-12-29 06:28

    class SomeClass {
      private T data;
    
      public SomeClass() {
      }
    
      public void set(T data_) {
        data = data_;
      }
    }
    

    You probably also want to make the class itself public, but that's pretty much the literal translation into Java.

    There are other differences between C++ templates and Java generics, but none of those are issues for your example.

提交回复
热议问题