1. new一个对象 和反射的区别?这个问题 送分的,答对即给分。。2. 设计模式大家用的多吗? 可以举个你用的例子吗?感谢。。

解决方案 »

  1.   

    又是你啊。。
    new一个对象和反射的区别就相当于宰一只鸡和桌子的区别 你说宰一只鸡和桌子的区别是什么
      

  2.   

    通过反射能够实现一些更为动态的效果,用new只能在编译期就确定对象的类型.
      

  3.   

    反射可以动态加载一个类 生成该类的实例
    new 只能静态加载一个类 生成一个类的实例 package day0815.factory;
    /**
     * 简单工厂模式
     * @author Cool
     *
     */
    public class TestFactory1 {
    public static void main(String[] args) {
    // Fruit apple = new Apple();
    // apple.eat();
    // Fruit orange = new Orange();
    // orange.eat();
    Fruit f = Factory.getFruit("orange");
    if(f!=null){
    f.eat();
    }
    }

    }
    interface Fruit{
    public void eat();
    }
    class Apple implements Fruit{
    public void eat() {
    System.out.println("吃苹果...");
    }
    }
    class Orange implements Fruit{
    public void eat() {
    System.out.println("吃橘子...");
    }
    }
    class Factory{
    public static Fruit getFruit(String fruitName){
    Fruit f = null;
    if("apple".equals(fruitName)){
    f = new Apple();
    }
    if("orange".equals(fruitName)){
    f= new Orange();
    }
    return f;
    }
    }
      

  4.   

    /**
     * 代理模式
     * @author Cool
     *
     */
    public class TestProxy {
    public static void main(String[] args) {
    //Give give = new ProxyGive(new RealGive());
    Give give = new RealGive();
    give.giveMoney();
    }
    }
    //讨债
    interface Give{
    public void giveMoney();
    }
    class RealGive implements Give{
    public void giveMoney() { //讨债
    System.out.println("把钱还给我....");
    }
    }
    class ProxyGive implements Give{//代理公司
    private Give give = null;
    public ProxyGive(Give give){
    this.give=give;
    }
    public void beforeGive(){
    System.out.println("准备:各种讨债工具");
    }
    public void giveMoney() {// 讨债方法
    this.beforeGive();
    this.give.giveMoney();//讨债
    this.afterGive();
    }  
    public void afterGive(){
    System.out.println("销毁所有的罪证");
    }
    }
      

  5.   

    1.new是创建一个对象。反射是判断对象的类型。
    2.不怎么会用设计模式。代理算吗? 
    class Carrier {
      private Fighter J10;
      private J10Count = 10;  Carrier() {
          for(int i=0; i<J10Count; i++)
              J10Factory.createFighter();
      }  public void attack() {
          J10.prepare();
          J10.fly();
          J10.searchEnemy();
          if(J10.findTarget()) {
              J10.suicideBombing();
              J10Count--;
          }
      } 
    }
      

  6.   

    1.用反射比用new更灵活
    2.单例模式常用
      

  7.   

    建议楼主看一下jive源代码吧,里面用了很多的设计模式
      

  8.   

    反射的名称解释就有能够在程序运行期间动态的获取程序集,模块,类型等信息!所以它比new的用法要灵活!
    至于各种模式也只是听说的比较多!嘿嘿嘿嘿..
      

  9.   

    最近写的一个Swing 小项目中就用了一个单例模式。用三层思想去写Swing程序连接Sqlserver数据库JdbcDao 类就是用的单例模式。
      

  10.   

    最近再做一个项目,基于tomcat,使用的模式有:
    单态(曾经用过,后来看了一些文章,把单态换成静态类了,由tomcat启动时自动做初始化)
    工厂
    代理
    反射
    观察者
    恩,就这么多了,项目还没开发完,有待进一步完善,另外jive很值得看看,学模式很好,我项目中的缓存就是用的它的缓存类,呵呵