老师作业还有一个疑问
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
}
}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
}
}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
}
}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
2023版Java工程师
- 参与学习 人
- 提交作业 8790 份
- 解答问题 9886 个
综合就业常年第一,编程排行常年霸榜,北上广深月薪过万! 不需要基础,无需脱产即可学习,只要你有梦想,想高薪! 全新升级:技术栈升级(包含VUE3.0,ES6,Git)+项目升级(前后端联调与功能升级)
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星