Java中对象转换的理解
2011-03-09 17:01
204 查看
Java的变量类型之间可以进行转换,对象之间也可以。
子类对象转为父类对象,可以不进行强制转换,因为子类继承父类对象。
但是,当父类对象转换为子类对象时(当且仅当父类对象本来是由子类默认转换过去的情况),可以对父类对象进行强制转换。
public class TestObject {
public static void main(String[] args) {
// TODO Auto-generated method stub
Animal a = new Animal();
Dog d = new Dog("yellow");
d.name = "bigYellow";
System.out.println( a.print( d ) );
}
}
class Animal{
String name;
public void setName(String n){
this.name = n;
}
public String getName(){
return this.name;
}
public String print(Animal a){
String result = "";
if (a instanceof Dog) {
Dog d = (Dog)a;
result = "Name:" + a.name + "\n" + "FurColor:" + d.furColor;
}else if (a instanceof Cat) {
Cat c = (Cat)a;
result = "Name:" + a.name + "\n" + "EyeColor:" + c.eyeColor;
}else{
result = "Name:" + a.name;
}
return result;
}
}
class Cat extends Animal{
String eyeColor;
public void setEyeColor(String e){
this.eyeColor = e;
}
public String getEyeColor(){
return this.eyeColor;
}
Cat(String e){
this.setEyeColor(e);
}
}
class Dog extends Animal{
String furColor;
public void setFurColor(String f){
this.furColor = f;
}
public String getFurColor(){
return this.furColor;
}
Dog (String d){
this.setFurColor(d);
}
}
96堆栈 软件编程网,http://www.96dz.com,提供C语言、C++编程、VC++编程、Java编程、C#编程、NET编程、Linux编程、Web编程等全面技术信息
子类对象转为父类对象,可以不进行强制转换,因为子类继承父类对象。
但是,当父类对象转换为子类对象时(当且仅当父类对象本来是由子类默认转换过去的情况),可以对父类对象进行强制转换。
public class TestObject {
public static void main(String[] args) {
// TODO Auto-generated method stub
Animal a = new Animal();
Dog d = new Dog("yellow");
d.name = "bigYellow";
System.out.println( a.print( d ) );
}
}
class Animal{
String name;
public void setName(String n){
this.name = n;
}
public String getName(){
return this.name;
}
public String print(Animal a){
String result = "";
if (a instanceof Dog) {
Dog d = (Dog)a;
result = "Name:" + a.name + "\n" + "FurColor:" + d.furColor;
}else if (a instanceof Cat) {
Cat c = (Cat)a;
result = "Name:" + a.name + "\n" + "EyeColor:" + c.eyeColor;
}else{
result = "Name:" + a.name;
}
return result;
}
}
class Cat extends Animal{
String eyeColor;
public void setEyeColor(String e){
this.eyeColor = e;
}
public String getEyeColor(){
return this.eyeColor;
}
Cat(String e){
this.setEyeColor(e);
}
}
class Dog extends Animal{
String furColor;
public void setFurColor(String f){
this.furColor = f;
}
public String getFurColor(){
return this.furColor;
}
Dog (String d){
this.setFurColor(d);
}
}
96堆栈 软件编程网,http://www.96dz.com,提供C语言、C++编程、VC++编程、Java编程、C#编程、NET编程、Linux编程、Web编程等全面技术信息
相关文章推荐
- Java中对象的list与JSON格式字符串的互相转换
- Json对象与Json字符串的转化、JSON字符串与Java对象的转换
- 关于Java两种同步方法的理解(代码块级同步对象设置)
- 深入理解Java虚拟机笔记---引用类型和对象是否死亡
- java中json包的使用以及字符串,map,list,自定义对象之间的相互转换
- java 日期对象与日期字符串的转换
- Java多线程总结(二):理解对象锁 & synchronized和Lock的区别
- jackson 进行json与java对象转换 之四
- JSON 字符串 与 java 对象的转换
- JSON对象与java对象转换
- GSON 转换JSON为JAVA对象的日期坑
- java入门教程-4.6Java多态对象的类型转换
- Java中对象和引用的理解
- Java服务器端List对象转换为JSON对…
- Gson在java对象和json字符串之间转换
- java中的String类型的对象为什么可以自动转换成Object类型的?而Object却要强制转换成String类型的
- Java中This ,Static,对象,对象的引用 等关键字的理解
- java对象转换成为json数据
- java中Map等对象转换为json
- java面向对象-理解封装思想