在 Java 中使用 getter 和 setter 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19691217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 19:25:49  来源:igfitidea点击:

Using getter and setter methods in Java

javagetter-setter

提问by user2854982

Trying to create a project that uses user input and known information to determine how much a car rental costs per day. The problem I'm having is the CarRental class doesn't seem to be getting the input from the test class. Not sure how to solve it.

尝试创建一个项目,该项目使用用户输入和已知信息来确定每天的汽车租赁费用。我遇到的问题是 CarRental 类似乎没有从测试类中获取输入。不知道如何解决。

class CarRental

类租车

public class CarRental {

private String customerName;
private String ratingKey;
private double baseFactor;
private double basefactor;
private double fudge;
private double computeDailyFee;

public CarRental(String customerName, String ratingKey, double baseFactor, double fudge, double computeDailyFee) {
    this.customerName = customerName;
    this.ratingKey = ratingKey;
    this.baseFactor = baseFactor;
    this.basefactor = basefactor;
    this.fudge = fudge;
    this.computeDailyFee = computeDailyFee;
    //intitialize
}

public String getcustomerName() {
    return this.customerName;
}

public String getratingKey() {
    return ratingKey;
}



public void setcustomerName(String newCustomerName) {

    customerName = newCustomerName;
    System.out.println(customerName);
}

public void setratingKey(String newRatingKey) {
    ratingKey = newRatingKey;
    System.out.println(ratingKey);
}



public double baseFactor() {
    switch (ratingKey.charAt(0)) {
        case 'S':
            baseFactor = 1.0;
            break;
        case 'C':
            baseFactor = 1.2;
            break;
        case 'U':
            baseFactor = 1.4;
            break;
        case 'T':
            baseFactor = 1.6;
            break;
        case 'B':
            baseFactor = 2.0;
            break;
        default:
            System.out.println("Invalid type");
            break;
    }
    System.out.println(baseFactor);
    return baseFactor;
}

public double fudge() {
    switch (ratingKey.charAt(1)) {
        case 'N':
            fudge = 11.40;
            break;
        case 'V':
            fudge = 0;
            break;
        default:
            System.out.println("Invalid type");
            break;
    }
    System.out.println(fudge);
    return fudge;
}

public double computeDailyFee() {
    computeDailyFee = (baseFactor() * (89.22 - fudge()));


    System.out.println(computeDailyFee);
    return computeDailyFee;
}
}

class RentalTest

类租赁测试

public class RentalTest {

public static void main(String[] args) {

     CarRental rental = new CarRental("customerName", "ratingKey", 0.0, 0.0, 0.0); 
String newCustomerName = rental.getcustomerName();   
    String newCustomerName = JOptionPane.showInputDialog("Enter your name");
   //System.out.println(newCustomerName);
    RentalTest rentaltest = new RentalTest();
            JOptionPane.showMessageDialog(null, "Input code for model, then code for condition (No Spaces)");

    String newRatingKey = JOptionPane.showInputDialog(
            "Models: 'S' = Sub-compact   'C'=Car   'U'=SUV   'T'=Truck   'B'=Bigfoot\n"
            + "Condition: 'N'= New   'V' = Vintage");
   newRatingKey = newRatingKey.toUpperCase();

    //System.out.println(newRatingKey);

}
}

采纳答案by Devrim

You may try something like this;

你可以尝试这样的事情;

CarRental carRental = CarRental("customerName", "ratingKey", 0.0, 0.0, 0.0);
carRental.getcustomerName();

回答by SlightlyCuban

You need to actually construct an instanceof CarRental, then get the name. In main:

实际上你需要构建一个实例CarRental,然后得到的名称。在main

// Build the object
CarRental rental = new CarRental("first", "last", 1.0, 1.0, 100.0);
// Now we can call instance methods.
System.out.println(rental.getCustomerName());

回答by user2885596

There are many errors in this program , try to re view the code as your both the class are declared as public as it won't work in a single java program and

这个程序中有很多错误,请尝试重新查看代码,因为您的两个类都被声明为公共类,因为它在单个 Java 程序中不起作用,并且

String newCustomerName = getcustomerName;

this above code will alos give compilation error .

上面的代码也会给出编译错误。

Here i thnk getCustomerName is a method so it should be declared as a method like this

在这里,我认为 getCustomerName 是一种方法,因此应将其声明为这样的方法

getcustomerName();

and to access it try to create the object of the carRental class and even you have not initialize the instance member of the carRental class as well by passing the value in the constructor.

并访问它尝试创建 carRental 类的对象,即使您还没有通过在构造函数中传递值来初始化 carRental 类的实例成员。