大佬們這樣子使用多態(tài)可以嗎,有什么需要改進(jìn)的地方?
public?class?Trans?{
public?String?name;
public?String?way;
public?int?person;
public?void?show()?{
System.out.println("交通工具的載客人數(shù):");?????}}
public?class?Bus?extends?Trans{
public?void?show()?{
name="公共汽車";
person=50;
way="陸地行走";
System.out.println(name+"在"+way+"上運(yùn)輸人數(shù)為:"+person); }}
public?class?Airplane?extends?Trans{
public?void?show()?{
name="飛機(jī)";
way="天空飛行";
person=60;
System.out.println(name+"在"+way+"上運(yùn)輸人數(shù)為:"+person); }}
public?class?Ship?extends?Trans{
public?void?show()?{
name="輪船";
way="海洋航行";
person=200;
System.out.println(name+"在"+way+"上運(yùn)輸人數(shù)為:"+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方法,是比較嚴(yán)謹(jǐn)?shù)淖龇?。后面就?huì)了解到的。