思路

实现

代码

public class Main {

public static void main(String[] args) {
	Person p=new Person();
	p.setName("小明");
	p.setAge(30);
	p.setSex('男');
	Car b=new benz();
	b.setBrand("奔驰");
	System.out.println(p.getName()+"在"+p.getAge()+"岁时买了一辆"+b.getBrand()+"车"+p.getSex());

	Person p1=new Person();
	p1.setName("小花");
	p1.setAge(25);
	p1.setSex('女');
	Car w=new bmw();
	w.setBrand("宝马");
	System.out.println(p1.getName()+"在"+p1.getAge()+"岁时买了一辆"+w.getBrand()+"车"+p1.getSex());
	
	System.out.print(p.getName()+"开着");
	b.run();
	
	System.out.print(p1.getName()+"开着");
	w.run();

}

}
public class Person {
private String name;
private int age;
private char sex;
	
public void setName(String name) {
	this.name=name;
}
public String getName() {
	return name;
}
public void setAge(int age) {
	this.age=age;
}
public int getAge() {
	return age;
}
public void setSex(char sex) {
	this.sex=sex;
}
public char getSex() {
	return sex;
}
}
public class Car {
	private String brand;
public void setBrand(String brand) {
	this.brand=brand;
}
public String getBrand() {
	return brand;
}
public void run() {
		
		
}

}
public class benz extends Car{
		
public void run() {
	System.out.println("奔驰车在跑!");
}

}
public class bmw extends Car{
public void run() {
	System.out.println("宝马车在跑!");
}
}