有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java需要使用setter和getter的帮助

我对二传手和二传手都是新手。我正在编写一个简单的程序,试图更好地使用它们。我有两门课,我所要做的就是为一个随机的人做一个账户,设置一个余额,然后退出。我认为我没有正确地使用二传手和传接手,如果有人能把我引向正确的方向,我将非常感激

package SetterGettersPractice;

public class account {
private String name;
public double balance =0;
public double withdraw;
public double deposit;
public double getDeposit() {
    return deposit;
}

public void setDeposit(double deposit) {
    this.deposit = deposit;
}

public account(String name, double d){

    this.name = name;
    this.balance = d;
}

private double sumDeposits(){
    balance = balance + deposit;
    return balance;
}
private double sumWithdrawals(){
    balance = balance - withdraw;
    return balance;
}
public double getwithdraw() {
    return withdraw;
}

public void setwithdraw(double withdraw) {
    this.withdraw = withdraw;
}

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public double getBalance() {
    return balance;
}
public void setBalance(double balance) {
    this.balance = balance;
}
public String toString(){

    return "Name="+name+" balance="+balance;
}

}

这是我的另一节课

package SettersGettersPractice;

public class testAccount {

public static void test1(){

    account a = new account("John Doe", 100);
    a.withdraw(10.0);
    System.out.print("Balance %d", balance);
}
}

正如你所看到的,一旦我弄清楚我在做什么,我计划尝试扩展这个项目。任何帮助noob摆脱困境的建议都会很棒


共 (4) 个答案

  1. # 1 楼答案

    您的类没有任何名为withdraw的方法。在您的测试方法中也没有变量名balance

    我会调用我在类中定义的方法来正确设置和获取如下值

    package SettersGettersPractice;
    
    public class testAccount {
    
    public static void test1(){
    
        account a = new account("John Doe", 100);
        a.setwithdraw(10.0);
        System.out.print("Balance %d", a.sumWithdrawals());
    }
    }
    
    <> P>当上面的代码可以给我期望的输出时,我会考虑重命名我的类来命名(命名约定),我也会根据它们中实现的功能将^ ^ {CD3>}改为^ {CD1>}和^ {CD5>}到^ {CD6>}。我建议您阅读Basics of java,其中包括命名约定、代码约定等,然后开始编码

  2. # 2 楼答案

    如果您想使用getter和setter,您的代码应该是这样的,您需要运行它们,以便:

     account a = new account("John Doe", 100);
        a.setBalance(10.0);
    
        System.out.print(a.getBalance);
    

    就这么简单:)

    p.S.课程名称总是以大的第一个字母开头

  3. # 3 楼答案

    将数据成员设置为私有,您就可以了。此外,您可能希望更好地设置代码格式-约定要求类名应大写:Accountvsaccount

    methidsetwithdraw不应是setter。它包含更高级的逻辑,所以我更愿意称之为withdraw

  4. # 4 楼答案

    为什么是能手和二传手

    您使用它们相当正确,但有一些错误。getter和setter旨在提供数据的封装

    例如,假设您有一个“Dog”类,它有一个“weight”变量

    public class Dog {
        int weight;
    }
    

    当前,您可以创建Dog对象的新实例,并编辑其权重变量,如下所示:

    Dog dog = new Dog();
    dog.weight = 45;
    

    虽然合乎逻辑,但这种做法可能会设置错误的值,可能会打乱整个程序。例如,通过这种设置,有人可以将权重变量设置为负数,这是没有意义的

    dog.weight = -50;
    

    如何创建setter方法

    为了防止这种情况发生,可以将权重变量设置为Dog类private而不是public

    public class Dog {
        private int weight;
    }
    

    现在,如果您试图从类外部访问该变量,它将被阻止

    dog.weight = 45; // would result in an error
    

    那么,如何让他们再次访问它,但不提供错误的值?您可以用setter封装它的数据

    public class Dog {
        private int weight;
    
        public void setWeight(int i) {
            this.weight = i;
        }
    }
    

    如何用setter封装(保护数据)

    这是一个setter的创建。但为了确保其数据正确,您可以执行以下操作:

    public class Dog {
        int weight;
    
        public void setWeight(int i) {
            if (i > 0) {
                this.weight = i;
            } else {
                System.out.println("A dog can't have negative weight!");
            }
        }
    }
    

    如何创建getter方法

    而且,由于该数据是私有的,这意味着他们不能以任何方式使用它,甚至不能显示它,因此要从对象中检索该变量的值,您需要创建一个getter方法

    public int getWeight() {
        return weight;
    }
    

    如何在代码中使用/fire getter和setter

    现在,要在这些getter和setter就绪的情况下更改“Dog”对象的变量,可以执行以下操作:

    Dog dog = new Dog();
    dog.setWeight(45);
    System.out.println(dog.getWeight());
    

    如果您还有任何问题,请在评论中提问,我一定会解释