中type 和class的本质区别(转)
2009-02-03 18:59
239 查看
<jsp:useBean id = "person" type = "foo.Person" scope = "page"/>
<jsp:useBean id = "person" class = "foo.Person" scope = "page"/>
id:可以随便命名,是为了下文中通过id使用这个bean,例如:<%=person.getName()%>
type:是bean的引用类型,这里有多态的概念,可以是抽象类,接口,比如Animal是抽象类,子类有Cat和Dog就可以这样写:type=“Animal” 或者type=“Cat”
class:是bean的具体类,即实例化的类,那就肯定不会是抽象类了吧,所以这里只能这样写:class=“Cat”
scope:就是指定bean在哪个范围内活动,次范围以外没有他的身影,缺省是 page;
正如我们笔记中所写:使用type时:如果scope中没有此bean 就会抛异常,
java.lang.InstantiationException:bean person not found within scope
使用class时:如果scope没有 则会自动创建一个新bean 相应的_jspService()代码:
foo.Person person = null;
person = (foo.Person)_jspx_page
_context.getAttribute("person",PageContext.PAGE_SCOPE);
if(person ==null){
perosn = new foo.Person();
_jspx_page_context.getAttribute
("person",person,PageContext.PAGE_SCOPE);
}
我总结如下:class是具体类,没有实例的时候可以根据 class=“Cat” 创建一个实例,很简单就直接new;而是type 就不一定了行了,type=“Animal”,是抽象类,当然不能实例化,就是能实例化,那是该化Cat还是化Dog呢?你没告诉我实例化 狗还是猫 具体的类啊!
所以啊,我建议 如果你想用type,那你首先保证 scope中有这个类型的bean,否则异常多麻烦!
还可以这样说:type==引用类型 class==对象类型
http://bbs.tarena.com.cn/viewthread.php?tid=141163
<jsp:useBean id = "person" class = "foo.Person" scope = "page"/>
id:可以随便命名,是为了下文中通过id使用这个bean,例如:<%=person.getName()%>
type:是bean的引用类型,这里有多态的概念,可以是抽象类,接口,比如Animal是抽象类,子类有Cat和Dog就可以这样写:type=“Animal” 或者type=“Cat”
class:是bean的具体类,即实例化的类,那就肯定不会是抽象类了吧,所以这里只能这样写:class=“Cat”
scope:就是指定bean在哪个范围内活动,次范围以外没有他的身影,缺省是 page;
正如我们笔记中所写:使用type时:如果scope中没有此bean 就会抛异常,
java.lang.InstantiationException:bean person not found within scope
使用class时:如果scope没有 则会自动创建一个新bean 相应的_jspService()代码:
foo.Person person = null;
person = (foo.Person)_jspx_page
_context.getAttribute("person",PageContext.PAGE_SCOPE);
if(person ==null){
perosn = new foo.Person();
_jspx_page_context.getAttribute
("person",person,PageContext.PAGE_SCOPE);
}
我总结如下:class是具体类,没有实例的时候可以根据 class=“Cat” 创建一个实例,很简单就直接new;而是type 就不一定了行了,type=“Animal”,是抽象类,当然不能实例化,就是能实例化,那是该化Cat还是化Dog呢?你没告诉我实例化 狗还是猫 具体的类啊!
所以啊,我建议 如果你想用type,那你首先保证 scope中有这个类型的bean,否则异常多麻烦!
还可以这样说:type==引用类型 class==对象类型
http://bbs.tarena.com.cn/viewthread.php?tid=141163
相关文章推荐
- 转 class和struct最本质的区别
- c++ typename class区别
- Interger.class 和 Interger.Type的区别
- JAVA 中 Boolean.class 和 Boolean.Type的区别
- Valuatio type & Valuation Class的区别,分割评估
- type_info Class中name()和raw_name()区别
- ParameterizedType getGenericSuperclass 获取泛型参数;class getClass区别
- type接口和class的区别(类型和类)
- ibatis中的resultClass,parameterClass,resultMap,resultType的使用与区别
- ibatis中的resultClass,parameterClass,resultMap,resultType的使用与区别
- ibatis中resultMap和resultClass(Mybatis的resultType)的区别
- enum class type 和enum type 的区别
- jsp:useBean中type和class的区别
- c++之typename(同class有what区别)
- java反射当中的Integer.class和Integer.Type的区别
- C++ 模板template<class T>和template<typename T>有区别吗?
- 浅谈haskell中Functor typeclass和普通typeclasses的区别
- class typename区别
- web.xml 配置中classpath: 与classpath*:的区别
- 类名.class, class.forName(), getClass()区别