简体   繁体   English

我们可以从同一个 class 的其他方法访问 setter 和 getter 的数据吗? #Java

[英]Can we access the data of setter and getter from other method of same class? #JAVA

** **

public class Main {公共 class 主要 {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    System.out.println("********WELCOME TO CUSTOMER DETAIL**********");
    System.out.println("");
    BankAccount customerDetails = new BankAccount();
    customerDetails.setAccountNumber(527553054);
    System.out.println("Account Number : " + customerDetails.getAccountNumber());
    customerDetails.setBalance(300000.00);
    System.out.println("Balance : " + customerDetails.getBalance());
    customerDetails.setCustomerName("Anish Shrestha");
    System.out.println("Customer Name : " + customerDetails.getCustomerName());
    customerDetails.setEmail("blah@gmail.com");
    System.out.println("Email : " + customerDetails.getEmail());
    customerDetails.setPhoneNumber(980709277);
    System.out.println("Phone Number : "+ customerDetails.getPhoneNumber());
    System.out.println("");
    System.out.println("*********************");

    BankAccount deposit = new BankAccount();
    deposit.depositFunds(566);
}

} }

package learnJava; package 学习Java;

public class BankAccount {公共 class 银行账户 {

  public void depositFunds(double depositAmount) {
        this.balance += depositAmount;
        System.out.println("The total sum of money : " + this.balance);
  }


private long accountNumber ;
private double balance;
private String customerName;
private String email;
private long phoneNumber;

//SETTER
public void setAccountNumber(long accountNumber) {
    this.accountNumber = accountNumber;
}

public void setBalance(double balance) {
    this.balance = balance; 
}

public void setCustomerName(String customerName) {
    this.customerName = customerName;
}

public void setEmail(String email) {
    this.email = email ;
}

public void setPhoneNumber(long phoneNumber) {
    this.phoneNumber = phoneNumber ;
}

//GETTER
public long getAccountNumber() {
    return this.accountNumber;
}

public double getBalance() {
    return this.balance;
}

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

public String getEmail() {
    return this.email;
}
public long getPhoneNumber() {
    return this.phoneNumber;
}

} }

** **

i could not add up deposit Amount to main balance Is it due to the setter/getter or there is something wrong in my code...... I am beginner so help me out guysss.....我无法将存款金额加到主余额中是由于 setter/getter 还是我的代码有问题......我是初学者所以帮我解决......

I am not able to access the data of getter What could be the problem or is it a rule?我无法访问 getter 的数据 可能是什么问题或者这是一条规则?

I have copy-paste your code, there is no problem.我已经复制粘贴你的代码,没有问题。 In comment, the output:在评论中,output:

      BankAccount deposit = new BankAccount(); 
      deposit.depositFunds(100); // The total sum of money : 100.0
      deposit.depositFunds(100); // The total sum of money : 200.0

      System.out.println(deposit.getBalance()); // 200.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM