定义java类对象的方法

定义一个银行账户类Account,要求如下:

  1. 属性(成员变量)包括:
    ①账号id(String)
    ②密码password(String)
    ③用户姓名name(String)
    ④金额money(double)
  2. 方法包括:
    ① showInfo,无返回值无参数,显示除密码外的账户全部信息
    ② getInterest,无参数,返回值为浮点型。返回一年期的活期存款利息
    (提示:一年期活期存款利率为0.25%,也就是说,10000元活期存款一年利息为25元)
  3. 定义测试类Test,在其main方法中创建一个Account对象,并对该对象的各种属性进行赋值。最后依次调用showInfo方法显示账户信息,调用getInterest计算存款利息并输出。

Account.java

public class Account {
    private String accountId;
    private String password;
    private String name;
    private double money;

    public void showInfo() {
        System.out.println("账号ID:" + accountId);
        System.out.println("用户姓名:" + name);
        System.out.println("金额:" + money);
    }

    public double getInterest() {
        double interestRate = 0.0025; // 利率为0.25%
        double interest = money * interestRate; // 计算利息
        return interest;
    }

    public String getAccountId() {
        return accountId;
    }

    public void setAccountId(String accountId) {
        this.accountId = accountId;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }
}

Test.java

public class Test {
    public static void main(String[] args) {
        Account account = new Account();
        account.setAccountId("123456789");
        account.setPassword("password123");
        account.setName("John Doe");
        account.setMoney(10000.0);

        account.showInfo();

        double interest = account.getInterest();
        System.out.println("存款利息:" + interest);
    }
}

运行结果如下:

Java项目实操_System