老师作业还有一个疑问
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | package jicheng.one1; public class Car { private String color; private String userName; public Car() {} public Car(String color,String userName) { this .setColor(color); this .setUserName(userName); } public String getColor() { return color; } public void setColor(String color) { this .color = color; } public String getUserName() { return userName; } public void setUserName(String userName) { this .userName = userName; } public void use() { System.out.println( "我是机动车!" ); } public boolean equals(Object obj) { if (obj== null ) return false ; Car sc=(Car)obj; if ( this .getColor().equals(sc.getColor())&& this .getUserName().equals(sc.getUserName())) return true ; else return false ; } public boolean equals(Car obj) { if (obj== null ) return false ; if ( this .getColor().equals(obj.getColor())&& this .getUserName().equals(obj.getUserName())) return true ; else return false ; } public static void main(String[] args) { // TODO Auto-generated method stub } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | package jicheng.one1; public class taxi extends Car { private String company; public taxi() {} public taxi(String color,String userName,String company) { this .setCompany(company); this .setColor(color); this .setUserName(userName); } public String getCompany() { return company; } public void setCompany(String company) { this .company = company; } final public void ride() { System.out.println( this .getUserName()+ "出租车是所属于在" + this .getCompany()+ "公司的" ); } @Override public void use() { // TODO Auto-generated method stub System.out.println( "出租车是提高市民条件之一" ); } @Override public String toString() { // TODO Auto-generated method stub return this .getUserName()+ "拥有一辆" + this .getColor()+ "的出租车" ; } public static void main(String[] args) { // TODO Auto-generated method stub } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | package jicheng.one1; final public class HomeCar extends Car{ private int num; public HomeCar() {} public HomeCar(String color,String userName, int num) { super (color,userName); this .setNum(num); } public int getNum() { return num; } public void setNum( int num) { this .num = num; } public void display() { System.out.println( this .getUserName()+ "拥有的" + this .getColor()+ "颜色的私家车有" + this .getNum()+ "个座位" ); } public void display( int num) { System.out.println( "加用私家车大多有" + this .getNum()+ "个座位" ); } public static void main(String[] args) { // TODO Auto-generated method stub } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package jicheng.one2; import jicheng.one1.*; public class ce5 { public static void main(String[] args) { // TODO Auto-generated method stub Car one= new Car( "蓝色" , "张小泉" ); Car two= new Car( "蓝色" , "张小泉" ); one.use(); System.out.println( "one与two属性比较" +one.equals(two)); taxi sc1= new taxi( "蓝色" , "张小泉" , "长生" ); sc1.ride(); sc1.use(); System.out.println(sc1); System.out.println(sc1.toString()); HomeCar sc2= new HomeCar( "紫色" , "张二娘" , 7 ); sc2.display(); } } |
为什么是重载的两个方法,sc2.display();直接两个方法都输出了不是应该只输出一个吗
4
收起
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧