Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
御承扬
java1
提交
fac08aa0
J
java1
项目概览
御承扬
/
java1
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
java1
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
fac08aa0
编写于
6月 28, 2019
作者:
御
御承扬
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Update
上级
fcca8b45
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
115 addition
and
0 deletion
+115
-0
src/反射/GetConstructorMain.java
src/反射/GetConstructorMain.java
+65
-0
src/反射/GetConstructorTest.java
src/反射/GetConstructorTest.java
+50
-0
未找到文件。
src/反射/GetConstructorMain.java
0 → 100644
浏览文件 @
fac08aa0
package
反射;
//
!/
usr
/
bin
/
env
jdk1
.
8
// encoding:utf-8
//@software:IntelliJ IDEA
//@pack:反射
//@user:彭友聪
//@date:2019/6/28
//@time:19:35
//@project:IDEA_JAVA
//@file:GetConstuctorMain.java
//Author:御承扬
//email:2923616405@qq.com
import
java.lang.reflect.Constructor
;
public
class
GetConstructorMain
{
private
GetConstructorMain
()
{
}
public
static
void
main
(
String
[]
args
)
{
GetConstructorTest
example
=
new
GetConstructorTest
(
"10"
,
"20"
,
"30"
);
Class
<?
extends
GetConstructorTest
>
exampleC
=
example
.
getClass
();
// 获得所有构造方法
Constructor
[]
declaredConstructors
=
exampleC
.
getDeclaredConstructors
();
int
len
=
declaredConstructors
.
length
;
for
(
int
i
=
0
;
i
<
len
;
i
++)
{
Constructor
<?>
constructor
=
declaredConstructors
[
i
];
System
.
out
.
println
(
"查看是否允许带有可变数量的参数:"
+
constructor
.
isVarArgs
()
);
System
.
out
.
println
(
"该构造方法的入口参数类型依次为:"
);
// 获得所有参数类型
Class
[]
parameterTypes
=
constructor
.
getParameterTypes
();
for
(
Class
parameterType
:
parameterTypes
)
{
System
.
out
.
println
(
" "
+
parameterType
);
}
System
.
out
.
println
(
"该构造方法可能抛出的异常类型:"
);
// 获得所有可能抛出的异常信息类型
Class
[]
exceptionTypes
=
constructor
.
getExceptionTypes
();
for
(
Class
exceptionType
:
exceptionTypes
)
{
System
.
out
.
println
(
" "
+
exceptionType
);
}
GetConstructorTest
example2
=
null
;
while
(
example2
==
null
)
{
try
{
if
(
i
==
2
)
example2
=
(
GetConstructorTest
)
constructor
.
newInstance
();
else
if
(
i
==
1
)
example2
=
(
GetConstructorTest
)
constructor
.
newInstance
(
"7"
,
5
);
else
{
Object
[]
parameters
=
new
Object
[]{
new
String
[]{
"100"
,
"200"
,
"300"
}};
example2
=
(
GetConstructorTest
)
constructor
.
newInstance
(
parameters
);
}
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
"在创建对象时抛出异常,下面执行 setAccessible()"
);
// 设置允许访问
constructor
.
setAccessible
(
true
);
}
}
example2
.
print
();
System
.
out
.
println
();
}
}
}
src/反射/GetConstructorTest.java
0 → 100644
浏览文件 @
fac08aa0
package
反射;
//
!/
usr
/
bin
/
env
jdk1
.
8
// encoding:utf-8
//@software:IntelliJ IDEA
//@pack:反射
//@user:彭友聪
//@date:2019/6/28
//@time:19:17
//@project:IDEA_JAVA
//@file:GetConstructorTest.java
//Author:御承扬
//email:2923616405@qq.com
import
org.jetbrains.annotations.Contract
;
public
class
GetConstructorTest
{
String
s
;
// 定义一个字符串变量
int
i
,
i2
,
i3
;
// 定义 3 个 int 变量
/**
* 默认构造方法
*/
@Contract
(
pure
=
true
)
private
GetConstructorTest
()
{
// 无参构造函数
}
protected
GetConstructorTest
(
String
s
,
int
i
)
{
// 有参构造函数,用来为字符串变量和 int 变量初始化值
this
.
s
=
s
;
this
.
i
=
i
;
}
public
GetConstructorTest
(
String
...
strings
)
throws
NumberFormatException
{
if
(
strings
.
length
>
0
)
i
=
Integer
.
valueOf
(
strings
[
0
]
);
if
(
strings
.
length
>
1
)
i2
=
Integer
.
valueOf
(
strings
[
1
]
);
if
(
strings
.
length
>
2
)
i3
=
Integer
.
valueOf
(
strings
[
2
]
);
}
public
void
print
()
{
System
.
out
.
println
(
"s="
+
s
);
System
.
out
.
println
(
"i="
+
i
);
System
.
out
.
println
(
"i2="
+
i2
);
System
.
out
.
println
(
"i3="
+
i3
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录