我很奇怪的是,如何为 j瓦的任意部分的les子写一个课? 这些组成部分都是通用的。 是否有一个语言特征,或以某种方式这样做?
感谢!
我很奇怪的是,如何为 j瓦的任意部分的les子写一个课? 这些组成部分都是通用的。 是否有一个语言特征,或以某种方式这样做?
感谢!
If the components of the tuple are all of data type T
, then you can simply use a List<T>
.
如果图形各组成部分都有不同的数据类型,就没有简单的方法。 事实上,Scala (坐在联合考试委员会的顶端)通过单独开设一门(正统)级,以达到任何数值。 例如,这里是一部两维教学法:
public class TwoTuple<T1, T2> {
private T1 e1;
private T2 e2;
public TwoTuple(T1 e1, T2 e2) {
this.e1 = e1;
this.e2 = e2;
}
public T1 getE1() {
return this.e1;
}
public T2 getE2() {
return this.e2;
}
}
同样,你也可以执行三读、四读等。
不幸的是,如果你想要避免 cast,就没有执行这一规定的一般办法。 当然,如果你不 mind,你可以简单地使用<代码>List<Object>。
这是安全的类型。 我提出的解决办法取决于了解“植被”时的物体类型:制造或“制造”:制造。 如果这种情形是错误的,就会提出有意义的时间例外,解释你的行为是错误的。
这一解决办法在汇编过程中并不安全。 如果你犯错误,它不会产生编辑错误。 但是,在运行期间,这种保险是安全的类型,因为它没有允许有类型的错误。 您宣布了建筑商的教职员工类型,这些类型在物体的完整寿命期间得到了加强。
课程:
/**
* A utility class for run time type safe mixed collections.
*
* @author davogotland
*/
public class Tuple {
private Class<?>[] m_types;
private Object[] m_objects;
/**
* Constructor, initializes members.
*
* @param objects
* An array of class objects, representing the
* objects of this tuple, followed by the
* objects of this tuple. The order these
* objects are passed to the constructor will
* decide what index they will be referred to
* with as they are being fetched or updated.
*/
public Tuple(Object... objects) {
if(objects.length == 0) {
m_types = new Class<?>[] {};
m_objects = new Object[] {};
} else {
try {
m_types = (Class<?>[])objects[0];
} catch(ClassCastException cce) {
throw new RuntimeException("the first parameter of Tuplet constructor must be an array of class objects.");
}
m_objects = new Object[m_types.length];
if(objects.length != 1) {
if(m_types.length != (objects.length - 1)) {
throw new RuntimeException("the first parameter of Tuplet constructor must be an array with the same length as the number of following arguments.");
}
System.arraycopy(objects, 1, m_objects, 0, m_types.length);
for(int i = 0; i < m_types.length; i++) {
try {
m_types[i].cast(m_objects[i]);
} catch(ClassCastException cce) {
throw new RuntimeException("the class objects of the first parameter to Tuple constructor must match the types of the following parameters. error at parameter " + i + ", type of " + m_objects[i] + " declared as " + m_types[i].getName() + " but was " + m_objects[i].getClass().getName() + ".");
}
}
}
}
}
/**
* Gets an element from the tuple.
*
* @param <T>
* The type of the element to fetch.
* @param c
* A class object representing the
* elements type.
* @param i
* The index of the element to fetch. This
* index is decided by the order the elements
* are added during construction of the
* tuple.
* @return
* The element at the given index, cast to
* the given type.
*/
public <T> T get(Class<T> c, int i) {
if(c != m_types[i]) {
throw new RuntimeException("the get method for index " + i + " must return a " + m_types[i].getName() + ". (attempted was " + c.getName() + ")");
}
return c.cast(m_objects[i]);
}
/**
* Sets an element in the tuple.
*
* @param i
* The index where the object should be set.
* @param object
* The object to set.
*/
public void set(int i, Object object) {
if(m_types[i] != object.getClass()) {
throw new RuntimeException("the set method for index " + i + " must take a " + m_types[i].getName() + ". (attempted was " + object.getClass().getName() + ")");
}
m_objects[i] = object;
}
}
a 测试班(优异):
/**
* A representation of a fraction.
*
* @author davogotland
*/
public class Fraction {
private int m_numerator;
private int m_denominator;
/**
* Constructor, initializes members.
*
* @param numerator
* @param denominator
*/
public Fraction(int numerator, int denominator) {
m_numerator = numerator;
m_denominator = denominator;
}
/**
* Calculates the value of this fraction as a double.
*
* @return
* The value of this fraction as a double.
*/
public double getDoubleValue() {
return (double)m_numerator / (double)m_denominator;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append(m_numerator);
builder.append("/");
builder.append(m_denominator);
return builder.toString();
}
}
和预期使用:
/**
* Proving that the class Tuple works.
*
* @author davogotland
*/
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
Tuple t = new Tuple(new Class<?>[] {Float.class, Fraction.class, Integer.class}, 2.2f, new Fraction(1, 3), 4);
System.out.println("first: " + t.get(Float.class, 0));
//expected output: first: 2.2
System.out.println("second: " + t.get(Fraction.class, 1));
//expected output: second: 1/3
System.out.println("third: " + t.get(Integer.class, 2));
//expected output: third: 4
t.set(0, 3.5f);
double sum = t.get(Float.class, 0);
sum += t.get(Fraction.class, 1).getDoubleValue();
sum += t.get(Integer.class, 2);
System.out.println("sum: " + sum);
//expected output: sum: 7.833333333333334
t = new Tuple(new Object[] {new Class<?>[] {Float.class, Fraction.class, Integer.class}});
t.set(0, 3.5f);
System.out.println("first: " + t.get(Float.class, 0));
//expected output: first: 3.5
System.out.println("second: " + t.get(Fraction.class, 1));
//expected output: second: null
System.out.println("third: " + t.get(Integer.class, 2));
//expected output: third: null
sum = t.get(Float.class, 0);
sum += t.get(Fraction.class, 1).getDoubleValue();
//expected null pointer exception on the above line
sum += t.get(Integer.class, 2);
System.out.println("sum: " + sum);
}
}
模范形形形形形形色色体的简单方法是:
SomeType[]
if a tuple s size doesn t changeList<SomeType>
if a tuple s size must be able to change, Object[]
or List<Object>
if the tuples need to be able to hold values of arbitrary (reference) type (with runtime type safety via casts), orTuple2<T1, T2>
, Triple3<T1, T2, T3>
, etc ... i.e. a different generic class for each value of N
.Java没有通用的方法来使用单一等级的固定型安全一般N-tuples。
(我认为你真的想要静态的类型安全;即,你想要每个在校的位置和固定点有适当的静态返回类型,以便你不需要一种预测。) 不幸的是,这是困难的。
Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...
Check this, List<String> list = new ArrayList<String>(); for (int i = 0; i < 10000; i++) { String value = (""+UUID.randomUUID().getLeastSignificantBits()).substring(3, ...
I am in the middle of solving a problem where I think it s best suited for a decorator and a state pattern. The high level setting is something like a sandwich maker and dispenser, where I have a set ...
I have been trying to execute a MS SQL Server stored procedure via JDBC today and have been unsuccessful thus far. The stored procedure has 1 input and 1 output parameter. With every combination I ...
I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...
If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...
I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....
I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...