[Java]类转型

Java中的类转型分为向上转型和向下转型

 转型是Java中的多态性的直接体现,另外一个体现是方法的重写和重载

1.向上转型

向上转型的目的是使得父类可以调用子类重写的父类的办法,此时父类并不能调用子类中非重写自己的方法,这些方法只能使用子类的对象或者子类访问(静态方法)

例子:

class Person {
	private String name;
	private int age;
	String sex = "secret";
	protected Person(String name, int age) {
		this(name);
		this.age = age;
		System.out.println("Father constructor 1th");
	}
	
	protected Person(String name) {
		this.name = name;
		System.out.println("Father constructor 2th");
	}
	
	protected String getName() {
		return name;
	}
	protected void setName(String name) {
		this.name = name;
	}
	protected int getAge() {
		return age;
	}
	protected void setAge(int age) {
		this.age = age;
	}
	protected String say () {
		return (this.name+" is saying sth");
	}
	
	public String getOldSex() {
		return sex;
	}
	
	public Person getCurrentObj () {
		return this;
	}
}

class Student extends Person {
	String sex = "male ";
	public Student(String name, int age) {
		super(name, age);
		System.out.println("child constructor 2th");
		// TODO Auto-generated constructor stub
	}
	
	public String getSex() {
		return sex;
	}
	
	public Student(String name) {
		super(name);
		System.out.println("child constructor 1th");
	}

	private String school;

	public String getSchool() {
		return school;
	}

	public void setSchool(String school) {
		this.school = school;
	}
	
	public String say() {
		return "super.say() = "+super.say()+"\n"+this.getName() + " said something"+"\n"+"super.sex = "+super.sex;
	}

}
public class T2 {
	
	public static void main(String [] args) {
		Person xiaoming = new Student("xiaoming", 21);
//		Student xiaoming = new Student("xiaoming", 21);
		System.out.println(xiaoming.getCurrentObj().say());
		
	}
}

运行结果:

 

分析:

这里将Person xiaoming  = new Student("xiaoming", 21);

就是使用了向上转型,此时转型得到的父类对象可以使用子类重写自己的say方法

但是不能访问子类独有的方法,不再举例

小结:

可以使用 父类名 对象名 = new 子类名(参数列表);

完成向上转型

2.向下转型

与向上转型不同,向下转型分为两步:

1.第一步,将子类转化为父类

2.第二步,将父类转化为子类

示例:

class Person {
	private String name;
	private int age;
	String sex = "secret";
	protected Person(String name, int age) {
		this(name);
		this.age = age;
		System.out.println("Father constructor 1th");
	}
	
	protected Person(String name) {
		this.name = name;
		System.out.println("Father constructor 2th");
	}
	
	protected String getName() {
		return name;
	}
	protected void setName(String name) {
		this.name = name;
	}
	protected int getAge() {
		return age;
	}
	protected void setAge(int age) {
		this.age = age;
	}
	protected String say () {
		return (this.name+" is saying sth");
	}
	
	public String getOldSex() {
		return sex;
	}
	
	public Person getCurrentObj () {
		return this;
	}
}

class Student extends Person {
	String sex = "male ";
	public Student(String name, int age) {
		super(name, age);
		System.out.println("child constructor 2th");
		// TODO Auto-generated constructor stub
	}
	
	public String getSex() {
		return sex;
	}
	
	public Student(String name) {
		super(name);
		System.out.println("child constructor 1th");
	}

	private String school;

	public String getSchool() {
		return school;
	}

	public void setSchool(String school) {
		this.school = school;
	}
	
	public String say() {
		return "super.say() = "+super.say()+"\n"+this.getName() + " said something"+"\n"+"super.sex = "+super.sex;
	}
	
	public String stuUnique () {
		return "unique method of Student";
	}

}
public class T2 {
	
	public static void main(String [] args) {
		Person xiaoming = new Student("xiaoming", 21);
		Student xiaomingstu = (Student)xiaoming;
//		Student xiaoming = new Student("xiaoming", 21);
		System.out.println(xiaomingstu.getCurrentObj().say());
		System.out.println(xiaomingstu.stuUnique());
	}
}

运行结果:

结果分析:

由于向下转型的本质是做了两次转型后又转型回来,所以最终得到的xiaomingstu可以调用

Student类中的独有方法,这与向上转型是不同的

向上转型是向下转型过程中的一环

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值