于此,在次申明个人觉得把握多态的最重要的一点:(方法)编译看定义、运行找实际。(属性)两者皆可抛,是啥就是啥。
造型Casting,也许大家大家看着会有点陌生,但是ClassCastException相信大家会有见过,这个异常是转型前后不一致时就会报出的。也许会出现这么个情况:想让对象变量编译时就调用运行时方法。那么别无选择,你得重新为这个变量造型,也就是类型转换。
造型,从变量是否含有对象特征,可分为基本数据造型和对象造型(且看图2和图3)。基本类型并没有对象的特征,它没有属性和方法甚至构造器,只是Java为了照顾传统程序员的习惯。以下是图片上主要意思的代码
相关代码:
/**************************************************
* explain for picture 2 and picture 3 ,群:152380972
* @author: 瘋叻ハ.兩
* @revision: 1.0
* @create-time: 2011-9-7 上午09:09:14
***************************************************/
public class CastingTest1 {
public static void main(String[] args) {
// casting of basic data type
double d = 13;
// cast
int l = (int)d;
System.out.println("数据已丢失部分,值从"+ d + "变为" +l);
// casting of object type
Object obj = "Hello"; // automatically change from String to Object
String temp1 = (String)obj; // cast from Object to String
System.out.println("转型前后一致,值还是:"+temp1);
//
try{
Integer ma = (Integer)obj;
System.out.println(ma);
}catch(Exception e){
e.printStackTrace();
}
}
}
运行结果:
分 析 :看代码注释
从上面的例子我们可以得到2个信息:1、基本数据类型的强制转换是我们不能控制的,但是对象还是可以的 2、基本数据类型缺乏面向对象的特征。下面归纳个人的总结方法(尽量将知识点结合一起)
1、防止对象转型抛出异常,可通过关键字instanceof进行判断。使用instanceof可以判断前面的对象是否是后面的类(编译时类型),或者其子类、实现类的实例。如果是,返回true,否则false。格式如:
a instanceof B // 判断a对象是否为B类的实例
但是请注意:instanceof运算符前面的操作数据的编译时类型要么与后面一致,要么是后面类的父类,否则会出现编译错误。且看示例代码。
相关代码:
import java.io.Serializable;
/**************************************************
* test instanceof【群:152380972】
* @author: 瘋叻ハ.兩
* @revision: 1.0
* @create-time: 2011-9-7 下午12:32:37
***************************************************/
public class InstanceofTest {
public static void main(String[] args) {
// declare a variable
// declared type is Object, actual type is String
Object obj = "Hello";
System.out.println("字符串是否是Object类的实例:" + (obj instanceof Object));
System.out.println("字符串是否是Math类的实例:" + (obj instanceof Math));
// class String implements Serializable
System.out.println("字符串是否是Serializable接口的实例:" + (obj instanceof Serializable));
// prove the attention
String hello = "hello";
// System.out.println("字符串是否是Math类的实例:" + (hello instanceof Math)); // cann't be compiled
}
}
运行结果:
字符串是否是Object类的实例:true
字符串是否是Math类的实例:false
字符串是否是Serializable接口的实例:true
分 析 :结合上文与代码注释
2、基本数据类型的转换,个人建议分两种:
2.1、自动类型转换,则没有任何问题。
2.2、
强制类型转换,建议使用数据的包装类,包装数据。这样就可以体现Java面向对象的编程啦,使用起来就方便多了。下面简单介绍基本数据的包装。
相关代码:
/**************************************************
* different time, different wrapper.【 群:152380972 】
* @author: 瘋叻ハ.兩
* @revision: 1.0
* @create-time: 2011-9-7 下午01:16:41
***************************************************/
public class WrapperTest {
public static void main(String[] args) {
/*
* before JDK 1.5, you should create a object by constructor! for example int
*/
// declare int variable
int it = 5;
// create Integer object by using int variable
Integer itg = new Integer(it);
// print the value as byte、float...
System.out.println(itg.intValue());
/*
* since JDK 1.5, the new feature of Autoboxing and AutoUnboxing makes wrapper more easily.
* for example int
*/
/*
* it can automatically evaluate basic data type to object type,
* but the left type must be the same as the right type whethor Autoboxing or AutoUnboxing
* or it will cause an Exception
*/
// create an Integer object by Autoboxing
Integer itgr = 10;
// AutoUnboxing
int it2 = itgr;
System.out.println(it2);
// transform Integer into String
System.out.println(String.valueOf(itgr));
}
}
运行结果:
5
10
10
分 析 :结合上文与代码注释
从最后一个结果我们可以看到,包装后的基本数据类型就能很好的与String交互(更多详情,请查询API)。不仅如此,基本数据类型的包装也为浮点型的精度提供了解决思路。这个类是BigDecimal,有兴趣的可查阅API或者加入我的群一起交流。
如果想创建不可变类、父类的方法不想被子类重载...答案尽在下篇---> final