public?class?Trans?{
public?String?name;
public?String?way;
public?int?person;
public?void?show()?{
System.out.println("交通工具的載客人數:");?????}}
public?class?Bus?extends?Trans{
public?void?show()?{
name="公共汽車";
person=50;
way="陸地行走";
System.out.println(name+"在"+way+"上運輸人數為:"+person); }}
public?class?Airplane?extends?Trans{
public?void?show()?{
name="飛機";
way="天空飛行";
person=60;
System.out.println(name+"在"+way+"上運輸人數為:"+person); }}
public?class?Ship?extends?Trans{
public?void?show()?{
name="輪船";
way="海洋航行";
person=200;
System.out.println(name+"在"+way+"上運輸人數為:"+person); }}
public?class?Test?{
public?static?void?main(String[]?args)?{
Trans?t=new?Trans();
Trans?t1=new?Bus();
Trans?t2=new?Airplane();
Trans?t3=new?Ship();
t.show();
t1.show();
t2.show();
t3.show(); }}
2019-08-21
整體沒問題。但是一般父類屬性私有化,然后提供getter/setter方法,是比較嚴謹的做法。后面就會了解到的。