获取构造器和创建对象

复制代码
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);
    }
}
复制代码

 

posted @   爱的加勒比  阅读(26)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
点击右上角即可分享
微信分享提示