我的java项目中有2个对象,我想从每个对象中取一个字段(比如说我们有2个酒店房间,其他字段中还有一个字段"价格&# 34;)并将em放在一个方法中并返回一个变量(在前面的例子中,让我们说这两个房间的差异)。我的主要问题是在你创建方法之后,你如何在主类中调用它? 我做了一个尝试(下面的代码),但我知道它不对。所以,如果你能给我一个如何做或接近它的建议将是非常有帮助的。
difference = priceDifference(roomA.price, roomB.price);
完整代码 主要课程
package project1;
import java.util.Scanner;
public class TestRoom {
public static void main(String[] args) {
String roomNumber, category, view;
int numberOfBeds;
double price;
double difference;
Room roomA = new Room("C101",2,"Standard","Sea",95.89);
Scanner sc = new Scanner(System.in);
System.out.println("Give room number \n");
roomNumber=sc.next();
System.out.println("Give number of beds \n");
numberOfBeds=sc.nextInt();
System.out.println("Give category \n");
category=sc.next();
System.out.println("Give view \n");
view=sc.next();
System.out.println("Give price \n");
price=sc.nextDouble();
Room roomB = new Room(roomNumber, numberOfBeds, category, view, price);
System.out.println(roomA.toString());
System.out.println(roomB.toString());
difference = priceDifference(roomA.getprice(), roomB.getprice());
}
}
课堂
package project1;
public class Room {
public String roomNumber;
public int numberOfBeds;
private String category;
private String view;
private double price;
public Room(String roomNumber, int numberOfBeds, String category, String view, double price){
this.roomNumber = roomNumber;
this.numberOfBeds = numberOfBeds;
this.category = category;
this.view = view;
this.price = price;
}
public void setroomNumber(String roomnumber){
this.roomNumber = roomnumber;
}
public String getroomNumber(){
return this.roomNumber;
}
public void setnumberOfBeds(int numberofbeds){
this.numberOfBeds = numberofbeds;
}
public int getnumberOfBeds(){
return this.numberOfBeds;
}
public void setcategory(String category){
this.category = category;
}
public String getcategory(){
return this.category;
}
public void setview(String view){
this.view = view;
}
public String getview(){
return this.view;
}
public void setprice(double price){
this.price = price;
}
public double getprice(){
return this.price;
}
public double priceDifference(double double1 ,double double2){
double difference = 0;
difference = double1 - double2;
return difference;
}
@Override
public String toString() {
return "Room number:" + this.roomNumber + ",\n "
+ "Number of beds:" + this.numberOfBeds + ",\n " + "Category:"
+ this.category + ",\n " + "View:" + this.view + ",\n " + "Price:" + this.price;
}
}
答案 0 :(得分:1)
正确的Java方法是在Room
类
public double priceDifference(Room roomB) {
return this.price - roomB.getPrice(); //or roomB.price if you do not use getters
}
然后您可以使用以下方法在主应用程序中调用它:
double difference = roomA.priceDifference(roomB);
更新:根据他的评论解决OP问题:
我认为你的方法如下:
public double priceDifference(double roomAprice, double roomB.price) { // code here }
如果是这种情况,请将其从Room
课程中取出,而不是放入public static void main()
所在的同一班级。或者,您可以在不移动它的情况下访问它:< / p>
difference = roomA.priceDifference(roomA.price, roomB.price);
请注意,第一个建议可能仍然是最佳选择,因为此更新中的建议将被视为语法正确,但设计不当。为了完成起见,我根据OP的评论添加了其他两个。
答案 1 :(得分:0)
封装您的字段是一种很好的做法。将它们保密在&#34;房间&#34; class,并使用getter和setter方法,而不是直接访问该值。
difference = priceDifference(roomA.getPrice(), roomB.getPrice);
在没有更多背景的情况下,我可以给出的所有建议。这段代码没有错。如果您尝试调试错误,我们需要更多信息。
正如您的代码所代表的那样,您应该更新方法调用以引用房间类的实例。你可以改变
difference = priceDifference(roomA.getprice(), roomB.getprice());
到
difference = roomB.priceDifference(roomA.getprice(), roomB.getprice());