上转型对象

查找写资料对上转型对象做个了解:

  假设B类是A类的子类或间接子类,当用子类B创建一个对象,并把这个对象的引用放到A类的对象中时,如
A a;
a=new B();

A a;
B b=new B();
a=b;
那么,称这个A类对象a是子类对象b的上转型对象(好比说:"老虎是哺乳动物").对象的上转型型对象实体是子类负责创建的,但上转型对象会失去原来的一些功能.

上转型对象具有如下特点:
1)上转型对象不能操作子类新增的成员变量(失掉了这部分属性),不能使用子类新增的方法(失掉了一些功能).
2)上转型对象可以操作子类继承或隐藏的成员变量,也可以使用子类继承的或重写的方法.
3)上转型对象操作子类继承或重写的方法时,就时通知对应的子类对象去调用这些方法.因此,如果子类重写了父类的某个方法后,对象的上转型对象调用这个方法时,一定是调用了这个重写的方法.
4)可以讲对象的上转型对象再强制转换到一个子类的对象,这时,该子类对象又具备了子类的所有属性和功能.


下面开始测试研究:

父类Computer.java(注意这里父类定义成了抽象类)

package org.test.parent;

public abstract class Computer {
	private String cpu="you choose";//可以给出默认值,也可以不给
	public String price;
	
	public void showPrice(){
		System.out.println("(父类方法)价格:"+getPrice());
	}
	
	public void showAppraise(String appraise){
		System.out.println("(父类方法)用户评价:"+appraise);
	}
	
	
	//showFatherSelf是private方法,子类对象不能使用,只有父类对象可以使用,而此父类是抽象类,不能实例化对象,所以这个方法只限于在此类中使用。
	//上转型对象是父类申请的对象,但是用子类实例化的,只能使用继承或重写的方法。private方法不能被继承。
	private void showFatherSelf(String name){
		System.out.println("(父类方法)showFatherSelf:"+name);
	}
	
	public abstract void showCpu();//抽象方法
	

	public String getCpu() {
		return cpu;
	}

	public void setCpu(String cpu) {
		this.cpu = cpu;
	}

	public String getPrice() {
		return price;
	}

	public void setPrice(String price) {
		this.price = price;
	}


}

子类ComputerLenovo.java

package org.test.parent;

public class ComputerLenovo extends Computer{
	

	@Override
	public void showCpu() {//实现父类中的抽象方法(重写)
		// TODO Auto-generated method stub
		System.out.println("Lenovo的cpu具体型号:"+getCpu());
	}
	
	public void showAppraise(String appraise){//重写父类中的非抽象方法
		System.out.println("Lenovo笔记本用户评价:"+appraise);
	}
	
	public void showNotebook() {//子类独有的方法
		// TODO Auto-generated method stub
		System.out.println("Lenovo笔记本的具体型号:"+getNotebook());
	}
	
	//和父类中的方法不同,这个方法是子类自己的,不是继承的,上转型对象不能调用。
	private void showFatherSelf(String name){
		System.out.println("(子类方法)showFatherSelf:"+name);
	}
	
	public String notebook;

	public String getNotebook() {
		return notebook;
	}

	public void setNotebook(String notebook) {
		this.notebook = notebook;
	}


}

测试类:

package org.test.parent;

public class TestCom {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// 方式一:声明子类对象,用子类来实例化。(常用的声明)
		ComputerLenovo child=new ComputerLenovo();
		// 方式二:声明父类对象,用子类来实例化。(这种方式就是上转型对象的声明)
		Computer father=new ComputerLenovo();
		
		
		System.out.println("--------------------child--------------------------------");
		
		System.out.println(child.getCpu()+"--"+child.getPrice()+"--"+child.getNotebook());
		child.setCpu("intel");
		child.setPrice("500元");
		child.setNotebook("ThinkPad");
		System.out.println(child.getCpu()+"--"+child.getPrice()+"--"+child.getNotebook());
		System.out.println("*********");
		
		child.showCpu();
		child.showPrice();//使用继承父类中的方法
		child.showNotebook();
		child.showAppraise("一般");
		
		System.out.println("#####对象访问######");
		System.out.println("child.price--"+child.price);
		System.out.println("child.notebook--"+child.notebook);
		
		System.out.println("---------------------father-------------------------------");
		System.out.println(father.getCpu()+"--"+father.getPrice());
		father.setCpu("CMD");
		father.setPrice("600元");
		System.out.println(father.getCpu()+"--"+father.getPrice());
		System.out.println("*********");
		father.showCpu();//上转型对象实际调用子类中子类中重写的方法
		father.showPrice();//上转型对象实际调用子类中子类中继承父类的方法(或者说调用父类自己的方法)
		father.showAppraise("很好");//上转型对象实际调用子类中子类中重写的方法(同上showCpu)
		//上转型对象不能调用子类中独有的方法showNotebook()
		
		System.out.println("#####对象访问######");
		System.out.println("father.price--"+father.price);//
		/*打印如下:
		 --------------------child--------------------------------
you choose--null--null
intel--500元--ThinkPad
*********
Lenovo的cpu具体型号:intel
(父类方法)价格:500元
Lenovo笔记本的具体型号:ThinkPad
Lenovo笔记本用户评价:一般
#####对象访问######
child.price--500元
child.notebook--ThinkPad
---------------------father-------------------------------
you choose--null
CMD--600元
*********
Lenovo的cpu具体型号:CMD
(父类方法)价格:600元
Lenovo笔记本用户评价:很好
#####对象访问######
father.price--600元

		 */
		/*
		 总上,上转型对象总结:
		1:声明父类对象,用子类来实例化。
		2:声明父类对象说明实际就是父类对象,但是这个对象的方法会去子类中寻找匹配。
		只要是在子类中继承或者重写的方法就符合匹配,在调用时,实际调用子类中匹配的方法。
		没有匹配的就使用自己的方法。
		同时,这个父类对象也只能访问子类继承的属性,子类中新增的属性只属于子类,跟父类无关;父类中私有的属性不会被继承,也访问不到。
		上转型对象本质是父类,方法却使用子类中继承或者重写的方法(如果此方法没有被继承或重写,就是用自己的这个方法)。
		
		 */
		
	}

}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值