Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0cdd340

Browse files
java generics
1 parent06bd86e commit0cdd340

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
packagesporadic.java_generics;
2+
3+
/**
4+
* This is a generic class that operates on objects of only T
5+
* type.
6+
* @param <T>
7+
* the type of the value
8+
*/
9+
publicclassGenericClass<T>extendsParentClass {
10+
// T stands for "Type"
11+
privateTt;
12+
13+
publicvoidset(Tt) {
14+
this.t =t;
15+
}
16+
17+
publicTget() {
18+
returnt;
19+
}
20+
21+
publicstaticvoidmain(String[]args) {
22+
GenericClass<Integer>integerBox =newGenericClass<Integer>();
23+
GenericClass<String>stringBox =newGenericClass<String>();
24+
25+
integerBox.add(newInteger(10));
26+
stringBox.add(newString("Hello World"));
27+
28+
System.out.printf("Integer Value :%d\n\n",integerBox.get());
29+
System.out.printf("String Value :%s\n",stringBox.get());
30+
}
31+
32+
privatevoidadd(Tt) {
33+
this.t =t;
34+
}
35+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
packagesporadic.java_generics;
2+
3+
/**
4+
* This is a non-generic Box class that operates on objects of any type. */
5+
publicclassNonGenericClass {
6+
privateObjectobject;
7+
8+
publicvoidset(Objectobject) {this.object =object; }
9+
publicObjectget() {returnobject; }
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
packagesporadic.java_generics;
2+
3+
publicclassParentClass {
4+
5+
@Override
6+
publicStringtoString() {
7+
return"This class is serving as the parent class for other classes to extend to demo java genric types for my own understanding.\nIt's calling the parent class: SteveSunParentClass [getClass()=" +getClass()
8+
+", hashCode()=" +hashCode() +", toString()="
9+
+super.toString() +"]";
10+
}
11+
12+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp