package com.liu.test03;
/**
* @author : liu
* 日期:14:27:02
* 描述:IntelliJ IDEA
* 版本:1.0
*/
//子类
@MyAnnotation("abc")
public class Student extends Person implements MyInterface{
//属性
private int sno;//学号
double height;//身高
protected double weight;//体重
public double score;//成绩
@Override
@MyAnnotation("helloMyMthod")
public void myMethod() {
System.out.println("我重写了myMethod方法");
}
//方法
@MyAnnotation("三好学生")
public String showInfo(){
return "我是一名三好学生";
}
//重载方法
public String showInfo(int a,int b){
return "我是一名三好学生";
}
private void work(){
System.out.println("我以后会找工作---》码农 程序员 程序猿 程序媛");
}
void happy(){
System.out.println("做人最重要的就是开开心心");
}
protected int getSno(){
return sno;
}
//构造器
public Student(){
}
public Student(double weight,double height){
this.weight=weight;
this.height=height;
}
private Student(int sno){
this.sno=sno;
}
Student(int sno,double weight){
this.sno=sno;
this.weight=weight;
}
protected Student(int sno , double weight,double height){
this.weight=weight;
this.sno=sno;
this.height=height;
}
@Override
public String toString() {
return "Student{" +
"sno=" + sno +
", height=" + height +
", weight=" + weight +
", score=" + score +
'}';
}
}
package com.liu.test03;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
/**
* @author : liu
* 日期:09:30:25
* 描述:IntelliJ IDEA
* 版本:1.0
*/
public class Test01 {
//这是一个main方法:是程序的入口
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
//获取字节码信息
Class cls = Student.class;
//通过字节码信息可以获取构造器
//getConstructors()只能获取当前运行时类的被public修饰的构造器
Constructor[] c1 = cls.getConstructors();
for (Constructor c:c1
) {
System.out.println(c);
}
System.out.println("----------------------------");
//getDeclaredConstructors()获取运行时类的全部修饰符构造器
Constructor[] c2 = cls.getDeclaredConstructors();
for (Constructor c:c2
) {
System.out.println(c);
}
System.out.println("-----------------");
//获取指定构造器
//获取空构造器
Constructor con1 = cls.getConstructor();
System.out.println(con1);
System.out.println("-----------------");
//得到两个参数的有参构造器
Constructor con2 = cls.getConstructor(double.class, double.class);
System.out.println(con2);
System.out.println("-------------------");
//得到一个参数的有参构造器并且是private修饰的
Constructor con3 = cls.getDeclaredConstructor(int.class);
System.out.println(con3);
//有了构造器以后就可以创建对象了
Object o = con1.newInstance();
System.out.println(o);
Object o1 = con2.newInstance(180.5, 170.6);
System.out.println(o1);
}
}