2020.8.6课堂笔记(反射,虚拟机安装mysql,单例模式)

获取类对象的三种方式:
1、类型.class
2、class.forname(“类型的全路径名”)
3、对象.getClass()
反射机制:
优点:可以越权,运行期注入
缺点:代码麻烦,不安全

1、上传安装包
2、安装依赖包perl,new-tools,autoconf
yum install -y …
3、卸载冲突包
rpm -qa | grep mariadb(查询冲突的包)
rpm -e 冲突的包 --nodeps
4、安装mysql
先安装客户端 client
再安装服务端server
rpm -ivh MySQL-client…
rpm -ivh MySQL-server…
5、修改配置文件
vi /usr/my.cnf
[client]
default-character-set=utf8
[mysqld]
skip-grant-tables
character_set_server=utf8
collation_server=utf8_general_ci

6、启动mysql
service mysql start
7.输入mysql进入mysql命令行
8.use mysql进入mysql库
9.修改root密码
update user set password=password(‘ok’);
quit退出
10.再次修改配置文件,把skip-grant-tables前面加上#并保存退出
11.重启mysql服务
service mysql restart
12.输入mysql -uroot -pok进入mysql命令行
13、重设密码
set password=password(‘ok’);
常用命令:
show databases;显示所有的库
use mysql; 切换到mysql库
show tables; 显示所有的表
建表语句
create table tb_a(aId int,aName varchar(10));
插入语句:
insert into tb_a values(1,“王思聪”);
查看
select * from tb_a;

vi /root/.mysql_secret 拷贝出来就是密码
mysql -uroot -p拷贝出来的密码
然后setpassword

Student:

package cn.kgc.kb09;

/**
 * @Author: ChaoKeAiMuZhi
 * @Date: 2020/8/6 13:57
 * @Description:
 **/
public class Student {
     
    public int stuId;
    private String stuName;
    String gender;

    private int getStuId() {
     
        return stuId;
    }

    private void setStuId(int stuId) {
     
        this.stuId = stuId;
    }

    public String getStuName() {
     
        return stuName;
    }

    public void setStuName(String stuName) {
     
        this.stuName = stuName;
    }

    public String getGender() {
     
        return gender;
    }

    public void setGender(String gender) {
     
        this.gender = gender;
    }

    private Student() {
     
    }

    private Student(int stuId, String stuName, String gender) {
     
        this.stuId = stuId;
        this.stuName = stuName;
        this.gender = gender;
    }

    @Override
    public String toString() {
     
        return "Student{" +
                "stuId=" + stuId +
                ", stuName='" + stuName + '\'' +
                ", gender='" + gender + '\'' +
                '}';
    }
}

TestStudent:

package cn.kgc.kb09;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @Author: ChaoKeAiMuZhi
 * @Date: 2020/8/6 13:59
 * @Description:
 **/
public class TestStudent<T> {
     
    public static void main(String[] args) throws Exception {
     
        /*获取类对象的另外一种方法,要先创建对象,通过变量名.getClass 一般不用
        TestStudent s = new TestStudent();
        Class sC = s.getClass();
        */

        //构造方法私有化无法建对象
        //Student s=new Student();
        //1.获取类对象
        //类型.class  获取编译后的class
        //Class stu = Student.class;
        //Class stu = Class.forName("cn.kgc.kb09.Student");
        //2.通过类对象获取构造方法
        //Constructor con = stu.getConstructor();
        //3.获取超级权限
        //Constructor con = stu.getDeclaredConstructor();
        //con.setAccessible(true);
        //3.通过构造方法创建对象
        //Student student=con.newInstance();
        //Student student=getStudent();
        TestStudent ts = new TestStudent<>();
        Class tClass=Class.forName("cn.kgc.kb09.Student");
        //Student student= (Student) ts.getObject("cn.kgc.kb09.Student");
        Student student= (Student) ts.getObject(tClass);
        student.stuId=1;
        student.gender="男";
        student.setStuName("王思聪");
        System.out.println(student);
    }

    //public T getObject(String classPath){
     
    public T getObject(Class<T> tClass){
     
        T t=null;
        try {
     
            //Class tClass= (Class) Class.forName(classPath);
            Constructor<T> c=tClass.getDeclaredConstructor();
            c.setAccessible(true);
            t=c.newInstance();
        } catch (NoSuchMethodException e) {
     
            e.printStackTrace();
        } catch (IllegalAccessException e) {
     
            e.printStackTrace();
        } catch (InstantiationException e) {
     
            e.printStackTrace();
        } catch (InvocationTargetException e) {
     
            e.printStackTrace();
        }
        return t;
    }


    public static Student getStudent(){
     
        Class<Student> s=Student.class;
        Student stu=null;
        try {
     
            Constructor<Student> c=s.getDeclaredConstructor();
            c.setAccessible(true);
            stu=c.newInstance();
        } catch (NoSuchMethodException e) {
     
            e.printStackTrace();
        } catch (InstantiationException e) {
     
            e.printStackTrace();
        } catch (InvocationTargetException e) {
     
            e.printStackTrace();
        } catch (IllegalAccessException e) {
     
            e.printStackTrace();
        }

        return stu;
    }
}

TestConstructor:

package cn.kgc.kb09;

import java.lang.reflect.Constructor;

/**
 * @Author: ChaoKeAiMuZhi
 * @Date: 2020/8/6 14:42
 * @Description:
 **/
public class TestConstructor {
     
    public static void main(String[] args) throws Exception{
     
        Class<Student> c=Student.class;
        Constructor<Student> sClass =
                c.getDeclaredConstructor(int.class, String.class, String.class);
        sClass.setAccessible(true);
        Student s=sClass.newInstance(2,"马爸爸","爹");
        System.out.println(s);
    }
}

TestMethod:

package cn.kgc.kb09;

import java.lang.reflect.Method;

/**
 * @Author: ChaoKeAiMuZhi
 * @Date: 2020/8/6 14:46
 * @Description:
 **/
public class TestMethod {
     
    public static void main(String[] args) throws Exception{
     
        //构建对象
        Student s=TestStudent.getStudent();
        //通过反射获取到类型的Class对象
        Class<Student> c=Student.class;
        //获取方法,要有方法名和参数列表,有多个的时候,用逗号分隔(三个点)
        Method setStuId = c.getDeclaredMethod("setStuId", int.class);
        setStuId.setAccessible(true);
        //具体指定那个对象赋值,传入参数
        setStuId.invoke(s,3);
        Method getStuId = c.getDeclaredMethod("getStuId");
        getStuId.setAccessible(true);
        //Class returnType = getStuId.getReturnType();
        Object stuId = getStuId.invoke(s);
        System.out.println(s);
        System.out.println(stuId);
    }
}

TestField:

package cn.kgc.kb09;

import java.lang.reflect.Field;

/**
 * @Author: ChaoKeAiMuZhi
 * @Date: 2020/8/6 15:02
 * @Description:反射获取属性
 **/
public class TestField {
     
    public static void main(String[] args) throws Exception{
     
        Student s=TestStudent.getStudent();
        Class<Student> c=Student.class;
        Field stuId = c.getField("stuId");
        stuId.set(s,11);
        Field stuName = c.getDeclaredField("stuName");
        stuName.setAccessible(true);
        stuName.set(s,"泡腾片");
        System.out.println(s);
        Field[] fields = c.getDeclaredFields();
        for (Field field : fields) {
     
            field.setAccessible(true);
            System.out.println(field.get(s));
            //System.out.println(field.getType());
        }
    }
}

Teacher:

package cn.kgc.kb09;

/**
 * @Author: ChaoKeAiMuZhi
 * @Date: 2020/8/6 15:26
 * @Description:单例模式:一个系统中只允许存在该类的一个对象
 * 类加载器,与数据库的连接
 **/
public class Teacher {
     

    private static Teacher t=null;
    private Teacher(){
     
        //不允许随意创建对象
    }
    //懒汉式
    /*public synchronized static Teacher getInstance(){
        if(t==null){
            t=new Teacher();
        }
        return t;
    }*/
    //饿汉式
    /*static {
        t=new Teacher();
    }
    public static Teacher getInstance(){
        return t;
    }*/
}

提问:为什么java里反射这个概念这么重要,而其他语言看好像没那么重要呢?
[码农翻身]刘欣:Java是静态语言,反射是为了在运行时获得一个类的各种信息,类名,字段名,方法,还可以通过“字符串名称”来调用方法。
动态语言如Python, Ruby也有这样的功能,并且强大得多,例如我可以给一个类添加一个新方法,删除一个现有方法。 有了这么强大的能力,就不用额外宣传:我有反射的能力了

你可能感兴趣的:(笔记)