Polymorphic up type conversion

1, the parent class

package com.wyq.study;

public class Car {
	//1、书写私有属性
	private String carType;
	private String carNo;
	public void setCarType(String carType){
		this.carType = carType;
	}
	public String getCarType(){
		return carType;
	}
	public void setCarNo(String CarNo){
		this.carNo = carNo;
	}
	public String getCarNo(){
		return carNo;
	}
	public Car(){
		super();
		System.out.println("这里是无参的构造方法。");
	}
	public Car(String carType,String carNo){
		super();
		this.carType = carType;
		this.carNo = carNo;
		System.out.println("这里是父类的带参构造方法。");
	}
	public void start(){
		System.out.println("我是车,我启动");
	}
	public void stop(){
		System.out.println("我是车,我停止");
	}
}

2, subclass 1

package com.wyq.study;

public class Taxi extends Car{
	private String company;
	public void setCompany(String company){
		this.company = company;
	}
	public String getCompany(){
		return company;
	}
	public Taxi(){
		super();
	}
	public Taxi(String carType,String carNo,String company){
		super(carType,carNo);
		this.company = company;
		System.out.println("这里是子类的带参构造:"+company+"\t"+super.getCarType()+"\t"+super.getCarNo());
	}
	@Override
	public void start() {
		super.start();
		System.out.println("我是"+company+"公司的,我是车,我启动。");
	}
	@Override
	public void stop() {
		super.stop();
		System.out.println("我是"+company+"的,我是车,我停止");
	}
}

2, subclass 2

package com.wyq.study;

public class MyCar extends Car{
	private String name;
	public void setName(String name){
		this.name = name;
	}
	public String getName(){
		return name;
	}
	public MyCar(){
		super();
	}
	public MyCar(String carType,String carNo,String name){
		super(carType,carNo);
		this.name = name;
	}
	@Override
	public void start() {
		super.start();
		System.out.println("我是"+name+"我启动"+super.getCarType()+"\t"+super.getCarNo());
	}
	@Override
	public void stop() {
		super.stop();
		System.out.println("我是"+this.name+"我的汽车,我停止。");
	}
}

3, the test class

package com.wyq.study;

public class TestCar {
	public static void main(String[] args) {
		Car c = new Car("大众","123456");
		c.start();
		c.stop();
		System.out.println("**********************************");
		Car ta = new Taxi("北京现代","京A11111", "景顺出租车");
		ta.start();
		ta.stop();
		System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
		Car mc = new MyCar("奥迪A8","京P882","张三");
		mc.start();
		mc.stop();
//		System.out.println("输出所有的属性:"+c.getCarType()+"\t"+c.getCarNo()+"\t"+ta.getCarType()+"\t"+ta.getCarNo()+"\t"+ta.getCompany()+"\t"+mc.getCarType()+"\t"+mc.getCarNo()+"\t"+mc.getName());
	}
}

4, operating results

这里是父类的带参构造方法。
我是车,我启动
我是车,我停止
**********************************
这里是父类的带参构造方法。
这里是子类的带参构造:景顺出租车	北京现代	京A11111
我是车,我启动
我是景顺出租车公司的,我是车,我启动。
我是车,我停止
我是景顺出租车的,我是车,我停止
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
这里是父类的带参构造方法。
我是车,我启动
我是张三我启动奥迪A8	京P882
我是车,我停止
我是张三我的汽车,我停止。

5, summary

1) to write the package, write inheritance

2) new subclass object parent

3) Intelligent call the object method of the parent class, and parent class property, the subclass inherits the parent class.

Guess you like

Origin blog.csdn.net/wyqwilliam/article/details/91679156