我如何在泛型类中使用intValue()

时间:2017-05-16 15:17:19

标签: java string generics integer int

我当然面临着大学任务的问题:

public class Num<Integer> { 
    Integer x;
    Integer y;

    public Num(Integer x, Integer y) {
       this.x = x;
       this.y = y;
    }

 // now i have to write a method whitch returns (x, y) as a String
    public String intValue() {

    }

我不知道我是否有正确的基本方法或它是如何工作的。 我想写一下intValue方法:

int i = x.IntValue();

int j = y.IntValue();

return“(”i“,”j“)”;

编辑:我解决了这个问题。可能有人确认是否正确:

public class Zahl<T> {
public Integer x;
public Integer y;

public Zahl(Integer x, Integer y) {
    this.x = x;
    this.y = y;
}   

 public String intValue() {
   int i = this.x.intValue();
   int j = this.y.intValue();
   return "(" + i + "," + j + ")";
} 
}

编辑2:问题解决了!谢谢你帮忙解决这个问题:-)

这是正确的代码:

class Zahl<N extends Number> {
N x;
N y;

Zahl(N x, N y) {
    this.x = x;
    this.y = y;
}
 String intValue() {
   return "(" + x.intValue() + "," + y.intValue() + ")";
} 

public class Test {
public static void main(String[] args) {
    Zahl<Integer> z = new Zahl<>(7, 8);
    System.out.println(z.intValue());
   }
}

输出:(7,8)

2 个答案:

答案 0 :(得分:2)

使用Integer作为类型参数的名称是一个非常糟糕的主意...称之为N或T但不是整数......

// Skip exec. if the init already run once, do not overwrite ui config
final File status = new 
File("${System.getenv("JENKINS_HOME")}/init.groovy.d/uk-config.status")
if (status.exists()) {
  logger.info("First init already run")
  return
}
status.createNewFile()

现在我们对public class Num<N> { ... } 了解的唯一事情是它是N,而Object没有Object方法。因此,您可能希望确保intValueN

Number

现在你可以写你的课了:

public class Num<N extends Number> {
  ...
}

答案 1 :(得分:0)

您可以连接字段并返回该字段。

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:fillViewport="true">

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginTop="15px">

            <LinearLayout
                android:id="@+id/spinner"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <TextView
                        android:id="@+id/titleText"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:layout_margin="@dimen/large_margin"
                        android:layout_marginBottom="@dimen/normal_margin"
                        android:text="Address"
                        android:textSize="@dimen/text_content" />

                    <CheckBox
                        android:id="@+id/checkBox"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:layout_margin="@dimen/small_margin"
                        android:button="@drawable/check_box"
                        android:checked="true"
                        android:clickable="false" />
                </RelativeLayout>

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:layout_margin="@dimen/small_margin"
                    android:background="@color/gray_border" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="@dimen/large_margin"
                    android:orientation="vertical"
                    android:padding="@dimen/large_margin">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical">

                        <android.support.design.widget.TextInputLayout
                            android:id="@+id/doorNumberTxt"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="20dp"
                            android:theme="@style/TextLabel">

                            <EditText
                                android:id="@+id/txtDoorName"
                                android:layout_width="match_parent"
                                android:layout_height="30dp"
                                android:layout_marginBottom="@dimen/normal_margin"
                                android:layout_marginTop="@dimen/normal_margin"
                                android:background="@android:color/transparent"
                                android:hint="@string/door_number"
                                android:singleLine="true"
                                android:textColorHighlight="@color/gray_text"
                                android:textColorHint="@color/gray_text"
                                android:textSize="@dimen/text_medium" />
                        </android.support.design.widget.TextInputLayout>

                        <View
                            android:layout_width="match_parent"
                            android:layout_height="1dp"
                            android:layout_marginBottom="@dimen/normal_margin"
                            android:layout_marginTop="@dimen/normal_margin"
                            android:background="@color/gray_border" />

                        <android.support.design.widget.TextInputLayout
                            android:id="@+id/apartmentTxt"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="20dp"
                            android:theme="@style/TextLabel">

                            <EditText
                                android:id="@+id/txtApartmentName"
                                android:layout_width="match_parent"
                                android:layout_height="30dp"
                                android:layout_marginBottom="@dimen/normal_margin"
                                android:layout_marginTop="@dimen/normal_margin"
                                android:background="@android:color/transparent"
                                android:hint="@string/apartment_number"
                                android:singleLine="true"
                                android:textColorHighlight="@color/gray_text"
                                android:textColorHint="@color/gray_text"
                                android:textSize="@dimen/text_medium" />
                        </android.support.design.widget.TextInputLayout>

                        <View
                            android:layout_width="match_parent"
                            android:layout_height="1dp"
                            android:layout_marginBottom="@dimen/normal_margin"
                            android:layout_marginTop="@dimen/normal_margin"
                            android:background="@color/gray_border" />

                        <android.support.design.widget.TextInputLayout
                            android:id="@+id/location_txt"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="20dp"
                            android:theme="@style/TextLabel">

                            <EditText
                                android:id="@+id/txt_location"
                                android:layout_width="match_parent"
                                android:layout_height="30dp"
                                android:layout_marginBottom="@dimen/normal_margin"
                                android:layout_marginTop="@dimen/normal_margin"
                                android:background="@android:color/transparent"
                                android:clickable="false"
                                android:hint="@string/txt_location"
                                android:lines="2"
                                android:textColorHighlight="@color/gray_text"
                                android:textColorHint="@color/gray_text"
                                android:textSize="@dimen/text_medium" />
                        </android.support.design.widget.TextInputLayout>

                        <View
                            android:layout_width="match_parent"
                            android:layout_height="1dp"
                            android:layout_marginBottom="@dimen/normal_margin"
                            android:layout_marginTop="@dimen/normal_margin"
                            android:background="@color/gray_border" />

                        <android.support.design.widget.TextInputLayout
                            android:id="@+id/cityTxt"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="20dp"
                            android:theme="@style/TextLabel">

                            <EditText
                                android:id="@+id/txtCity"
                                android:layout_width="match_parent"
                                android:layout_height="30dp"
                                android:layout_marginBottom="@dimen/normal_margin"
                                android:layout_marginTop="@dimen/normal_margin"
                                android:background="@android:color/transparent"
                                android:clickable="false"
                                android:hint="@string/city"
                                android:lines="5"
                                android:textColorHighlight="@color/gray_text"
                                android:textColorHint="@color/gray_text"
                                android:textSize="@dimen/text_medium" />
                        </android.support.design.widget.TextInputLayout>

                        <View
                            android:layout_width="match_parent"
                            android:layout_height="1dp"
                            android:layout_marginBottom="@dimen/normal_margin"
                            android:layout_marginTop="@dimen/normal_margin"
                            android:background="@color/gray_border" />

                        <android.support.design.widget.TextInputLayout
                            android:id="@+id/pincodeTxt"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="20dp"
                            android:theme="@style/TextLabel">

                            <EditText
                                android:id="@+id/txtPincode"
                                android:layout_width="match_parent"
                                android:layout_height="30dp"
                                android:layout_marginBottom="@dimen/normal_margin"
                                android:layout_marginTop="@dimen/normal_margin"
                                android:background="@android:color/transparent"
                                android:digits="1234567890"
                                android:hint="@string/pincode"
                                android:inputType="textPostalAddress"
                                android:singleLine="true"
                                android:textColorHighlight="@color/gray_text"
                                android:textColorHint="@color/gray_text"
                                android:textSize="@dimen/text_medium" />
                        </android.support.design.widget.TextInputLayout>

                        <View
                            android:layout_width="match_parent"
                            android:layout_height="1dp"
                            android:layout_marginBottom="@dimen/normal_margin"
                            android:layout_marginTop="@dimen/normal_margin"
                            android:background="@color/gray_border" />
                    </LinearLayout>
                </LinearLayout>
            </LinearLayout>
        </RelativeLayout>
    </ScrollView>

    <RelativeLayout
        android:id="@+id/layoutFooter"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize"
        android:layout_alignParentBottom="true"
        android:background="@color/fab_button_color">

        <TextView
            android:id="@+id/txtLblInr"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_centerVertical="true"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="@dimen/x_large_margin"
            android:gravity="center_vertical"
            android:singleLine="true"
            android:text="@string/save_n_contine"
            android:textAllCaps="true"
            android:textColor="@color/white"
            android:textSize="@dimen/text_title" />

        <TextView
            android:id="@+id/txtTotalContinue"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="@dimen/x_large_margin"
            android:drawableRight="@drawable/right_arrow"
            android:gravity="center"
            android:singleLine="true"
            android:textColor="@color/white"
            android:textSize="@dimen/text_title" />
    </RelativeLayout>
</LinearLayout>