Java框架第一次作业

一、自己设计一个类,演示一下常用的反射的功能。

设计了一个学生类

class People {

    private String name;

    private int age;

    public int number = 2008;

    public String color = "red";

    public void log() {

        System.out.println("People.log()");

    }

    People(String name, int age) {

        this.name = name;

        this.age = age;

    }

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public int getAge() {

        return age;

    }

    public void setAge(int age) {

        this.age = age;

    }

}

  1. 通过objecat,gerclass()方法反射获取了class的类名

public class Main {

        public static void main(String[] args) {

            People people = new People("bob", 18);

            Class<?> c = people.getClass();

            print(c.getName());       

            print(c.getSimpleName());

        }

    private static void print(Object o) {

        if (o != null) {

            System.out.println(o.toString());

        }

    }

}

运行截图

2.自己设计两个类,但这两个类有依赖关系,不同的同学设计的类尽量不要雷同。利用构造器注入方式 实现控制反转,利用XML文件配置。

Compare

package com.spring;

import com.spring.Computer;

public class Compare {

    private Computer computer;

    public Compare(Computer computer)

    {

        this.computer = computer;

    }

}

Computer

package com.spring;

public class Computer {

    private String shoes;

    private String cameara;

    private String age;

    public Computer(String shoes,String cameara,String age )

    {

        this.shoes = shoes;

        this.cameara = cameara;

        this.age = age;

    }

}

上面两个类都有有参的构造器,在xml里配置两个bean,更改构造器的参数值

<?xml version="1.0" encoding="UTF-8"?>

<bean id="Compare" class="com.spring.Compare">

    <constructor-arg ref="computer"></constructor-arg>

<bean id=" computer " class="com.spring. Computer ">

<constructor-arg value="奥特曼"></constructor-arg>

<constructor-arg value="肯德基"></constructor-arg>

<constructor-arg value="南通"></constructor-arg>

</bean>

</bean>

============================

3.用setter注入方法将第一题重构一遍;

(1在类里面申明属性并且实现set方法

package com.spring;

public class Compare {

    private String Atm;

    private String Kfc;

    private String City;

    public void coding() {

        System.out.println("coding!!!");

    }

    public String getAtm () {

        return atm;

    }

    public void setAtm (String atm) {

        this.atm = atm;

    }

    public String getKfc() {

        return kfc;

    }

    public void setKfc(String kfc) {

            this.kfc = kfc;

    }

    public String getCity() {

        return city;

    }

    public void setCity(String city) {

        this.city = city;

    }

}

(2)接下来写spring的xml配置文件,需要name赋值

<?xml version="1.0" encoding="UTF-8"?>

<bean id="compare" class="com.springCompare">

    <constructor-arg ref="computer"></constructor-arg>

<bean id=" computer " class="com.spring. Computer ">

<constructor-arg name="atm" value="奥特曼"></constructor-arg>

<constructor-arg name="kfc" value="肯德基"></constructor-arg>

<constructor-arg name="city" value="南通"></constructor-arg>

</bean>

</bean>

===========================

  1. 注解的处理方法一般是在编译期直接扫描:编译器在编译Java代码的时候扫描对应的注解并处理,或者在运行期通过反射处理,比如Spring框架的@Component都是通过反射来处理的。自己设计一个注解接口,并写出接口处理程序,并进行测试。

(1)自定义注解A

@Retention(value = RetentionPolicy.RUNTIME)

@Target(value = {ElementType.METHOD,ElementType.TYPE,ElementType.FIELD,ElementType.PARAMETER} )

public static @interface A {

    String value() default "";

(2)创建一个Test类

给方法参数添加注解ATM

@Retention(value = RetentionPolicy.RUNTIME)

@Target(value = {ElementType.METHOD,ElementType.TYPE,ElementType.FIELD,ElementType.PARAMETER} )

public static @interface ATM {

    String value() default "";

}

(3)在Main中获取show方法参数上的注解

import com.spring.Computer;

import com.spring.Test;

import java.lang.annotation.*;

import java.lang.reflect.Method;

public class Main {

        public static void main(String[] args) throws NoSuchMethodException, ClassNotFoundException {

            // 得到Class类对象

            Class<?> clazz = Class.forName("com.spring.Test");

            // 获取Test类的所有方法

            Method[] methods = clazz.getMethods();

            for (Method method : methods) {

                Annotation[][] annotations = method.getParameterAnnotations();

                for(int i=0;i<annotations.length;i++){

                    for(int j= 0;j<annotations[i].length;j++){

                        if(annotations[i][j] instanceof Test.ATM){

                            Test.ATM b = (Test.ATM) annotations[i][j];

                            System.out.println(b);

                        }

                    }

                }

            }

        }

(4)运行截图

5.根据群里的Java设计模式电子书,把工厂设计模式重构一遍

(1)

/**

 * 抽象金融产品类

 */

public abstract class Un1 {

    /**

     * 所有产品类的公共业务方法

     */

    public void methodSame() {

        String className = this.getClass().getName();

        //公共方法的实现

        System.out.println("抽象类公共方法,该类是:" + className);

    }

    /**

     * 声明抽象业务方法

     */

    public abstract void disPlayProduct();

}

(2)

创建产品枚举

/**

 * 产品名称枚举

 */

public enum ProductEnum {

    /**

     * 产品类别

     */

    Bond("Bond"), Loan("Loan"), Stock("Stock");

    String name;

    ProductEnum(String name) {

        this.name = name;

    }

}

(3)

创建简单工厂类

import com.spring.Un1;

public class SimpleFactory {

    static Un1 creatProduct(ProductEnum productEnum) {

        switch (productEnum) {

            case Bond :

                return new BondFinanceProduct();

            case Loan :

                return new LoanFinanceProduct();

            case Stock :

                return new StockFinanceProduct();

            default:

                throw new RuntimeException("No such product" + productEnum.name());

        }

    }

}

(4)

窗口工厂调用方法

import com.spring.Un1;

public class SimpleFactoryPattern {

    public static void main(String[] args) {

        Un1 stockProduct = SimpleFactory.creatProduct(ProductEnum.Stock);

        Un1 bondProduct = SimpleFactory.creatProduct(ProductEnum.Bond);

        stockProduct.methodSame();

        stockProduct.disPlayProduct();

        bondProduct.disPlayProduct();

    }

}

一、自己设计一个类,演示一下常用的反射的功能。

设计了一个学生类

class People {

    private String name;

    private int age;

    public int number = 2008;

    public String color = "red";

    public void log() {

        System.out.println("People.log()");

    }

    People(String name, int age) {

        this.name = name;

        this.age = age;

    }

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public int getAge() {

        return age;

    }

    public void setAge(int age) {

        this.age = age;

    }

}

  1. 通过objecat,gerclass()方法反射获取了class的类名

public class Main {

        public static void main(String[] args) {

            People people = new People("bob", 18);

            Class<?> c = people.getClass();

            print(c.getName());       

            print(c.getSimpleName());

        }

    private static void print(Object o) {

        if (o != null) {

            System.out.println(o.toString());

        }

    }

}

运行截图

2.自己设计两个类,但这两个类有依赖关系,不同的同学设计的类尽量不要雷同。利用构造器注入方式 实现控制反转,利用XML文件配置。

Compare

package com.spring;

import com.spring.Computer;

public class Compare {

    private Computer computer;

    public Compare(Computer computer)

    {

        this.computer = computer;

    }

}

Computer

package com.spring;

public class Computer {

    private String shoes;

    private String cameara;

    private String age;

    public Computer(String shoes,String cameara,String age )

    {

        this.shoes = shoes;

        this.cameara = cameara;

        this.age = age;

    }

}

上面两个类都有有参的构造器,在xml里配置两个bean,更改构造器的参数值

<?xml version="1.0" encoding="UTF-8"?>

<bean id="Compare" class="com.spring.Compare">

    <constructor-arg ref="computer"></constructor-arg>

<bean id=" computer " class="com.spring. Computer ">

<constructor-arg value="奥特曼"></constructor-arg>

<constructor-arg value="肯德基"></constructor-arg>

<constructor-arg value="南通"></constructor-arg>

</bean>

</bean>

============================

3.用setter注入方法将第一题重构一遍;

(1在类里面申明属性并且实现set方法

package com.spring;

public class Compare {

    private String Atm;

    private String Kfc;

    private String City;

    public void coding() {

        System.out.println("coding!!!");

    }

    public String getAtm () {

        return atm;

    }

    public void setAtm (String atm) {

        this.atm = atm;

    }

    public String getKfc() {

        return kfc;

    }

    public void setKfc(String kfc) {

            this.kfc = kfc;

    }

    public String getCity() {

        return city;

    }

    public void setCity(String city) {

        this.city = city;

    }

}

(2)接下来写spring的xml配置文件,需要name赋值

<?xml version="1.0" encoding="UTF-8"?>

<bean id="compare" class="com.springCompare">

    <constructor-arg ref="computer"></constructor-arg>

<bean id=" computer " class="com.spring. Computer ">

<constructor-arg name="atm" value="奥特曼"></constructor-arg>

<constructor-arg name="kfc" value="肯德基"></constructor-arg>

<constructor-arg name="city" value="南通"></constructor-arg>

</bean>

</bean>

===========================

  1. 注解的处理方法一般是在编译期直接扫描:编译器在编译Java代码的时候扫描对应的注解并处理,或者在运行期通过反射处理,比如Spring框架的@Component都是通过反射来处理的。自己设计一个注解接口,并写出接口处理程序,并进行测试。

(1)自定义注解A

@Retention(value = RetentionPolicy.RUNTIME)

@Target(value = {ElementType.METHOD,ElementType.TYPE,ElementType.FIELD,ElementType.PARAMETER} )

public static @interface A {

    String value() default "";

(2)创建一个Test类

给方法参数添加注解ATM

@Retention(value = RetentionPolicy.RUNTIME)

@Target(value = {ElementType.METHOD,ElementType.TYPE,ElementType.FIELD,ElementType.PARAMETER} )

public static @interface ATM {

    String value() default "";

}

(3)在Main中获取show方法参数上的注解

import com.spring.Computer;

import com.spring.Test;

import java.lang.annotation.*;

import java.lang.reflect.Method;

public class Main {

        public static void main(String[] args) throws NoSuchMethodException, ClassNotFoundException {

            // 得到Class类对象

            Class<?> clazz = Class.forName("com.spring.Test");

            // 获取Test类的所有方法

            Method[] methods = clazz.getMethods();

            for (Method method : methods) {

                Annotation[][] annotations = method.getParameterAnnotations();

                for(int i=0;i<annotations.length;i++){

                    for(int j= 0;j<annotations[i].length;j++){

                        if(annotations[i][j] instanceof Test.ATM){

                            Test.ATM b = (Test.ATM) annotations[i][j];

                            System.out.println(b);

                        }

                    }

                }

            }

        }

5.根据群里的Java设计模式电子书,把工厂设计模式重构一遍

(1)

/**

 * 抽象金融产品类

 */

public abstract class Un1 {

    /**

     * 所有产品类的公共业务方法

     */

    public void methodSame() {

        String className = this.getClass().getName();

        //公共方法的实现

        System.out.println("抽象类公共方法,该类是:" + className);

    }

    /**

     * 声明抽象业务方法

     */

    public abstract void disPlayProduct();

}

(2)

创建产品枚举

/**

 * 产品名称枚举

 */

public enum ProductEnum {

    /**

     * 产品类别

     */

    Bond("Bond"), Loan("Loan"), Stock("Stock");

    String name;

    ProductEnum(String name) {

        this.name = name;

    }

}

(3)

创建简单工厂类

import com.spring.Un1;

public class SimpleFactory {

    static Un1 creatProduct(ProductEnum productEnum) {

        switch (productEnum) {

            case Bond :

                return new BondFinanceProduct();

            case Loan :

                return new LoanFinanceProduct();

            case Stock :

                return new StockFinanceProduct();

            default:

                throw new RuntimeException("No such product" + productEnum.name());

        }

    }

}

(4)

窗口工厂调用方法

import com.spring.Un1;

public class SimpleFactoryPattern {

    public static void main(String[] args) {

        Un1 stockProduct = SimpleFactory.creatProduct(ProductEnum.Stock);

        Un1 bondProduct = SimpleFactory.creatProduct(ProductEnum.Bond);

        stockProduct.methodSame();

        stockProduct.disPlayProduct();

        bondProduct.disPlayProduct();

    }

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值