Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
cppg
toBeTopJavaer
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
toBeTopJavaer
/
docs
/
basics
/
java-basic
/
generics-problem.md
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
42 lines (28 loc) · 1.59 KB
master
Breadcrumbs
toBeTopJavaer
/
docs
/
basics
/
java-basic
/
generics-problem.md
Copy path
Top
File metadata and controls
Code
Blame
42 lines (28 loc) · 1.59 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
###
一
、
当泛型遇到重载
public
class
GenericTypes
{
public
static
void
method
(
List
<
String
>
list
) {
System
.
out
.
println
(
"invoke method(List<String> list)"
);
}
public
static
void
method
(
List
<
Integer
>
list
) {
System
.
out
.
println
(
"invoke method(List<Integer> list)"
);
}
}
上面这段代码
,
有两个重载的函数
,
因为他们的参数类型不同
,
一个是
`
List
<
String
>`
另一个是
`
List
<
Integer
>` ,
但是
,
这段代码是编译通不过的
。
因为我们前面讲过
,
参数
`
List
<
Integer
>`
和
`
List
<
String
>`
编译之后都被擦除了
,
变成了一样的原生类型List
<
e
>,
擦除动作导致这两个方法的特征签名变得一模一样
。</
e
>
###
二
、
当泛型遇到catch
如果我们自定义了一个泛型异常类GenericException
<
t
>,
那么
,
不要尝试用多个catch取匹配不同的异常类型
,
例如你想要分别捕获GenericException
<
string
>、
GenericException
<
integer
>,
这也是有问题的
。</
integer
></
string
></
t
>
###
三
、
当泛型内包含静态变量
public
class
StaticTest
{
public
static
void
main
(
String
[]
args
){
GT
<
Integer
>
gti
=
new
GT
<
Integer
>();
gti
.
var
=
1
;
GT
<
String
>
gts
=
new
GT
<
String
>();
gts
.
var
=
2
;
System
.
out
.
println
(
gti
.
var
);
}
}
class
GT
<
T
>{
public
static
int
var
=
0
;
public
void
nothing
(
T
x
){}
}
答案是
——
2
!
由于经过类型擦除
,
所有的泛型类实例都关联到同一份字节码上
,
泛型类的所有静态变量是共享的
。
You can’t perform that action at this time.