Java——07——this的使用

一:this调用属性和方法

在这里插入图片描述

在这里插入图片描述

二:this调用构造器

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

三:this例题

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述Account.java

package com.xx.study;

public class Account {
	private int id;// 账号
	private double balance;// 余额
	private double annualInterestRate;// 年利率

	public Account(int id, double balance, double annualInterestRate)// 构造器
	{
		this.id = id;
		this.balance = balance;
		this.annualInterestRate = annualInterestRate;

	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public double getBalance() {
		return balance;
	}

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

	public double getAnnualInterestRate() {
		return annualInterestRate;
	}

	public void setAnnualInterestRate(double annualInterestRate) {
		this.annualInterestRate = annualInterestRate;
	}

	public void withdrew(double amount) {// 取钱
      if(balance<amount) {
    	  System.out.println("余额不足,取款失败");
    	  return;
      }
      balance-=amount;
      System.out.println("成功取出"+amount);
	}

	public void deposit(double amount) {// 存钱
     if(amount>0) {
    	 balance+=amount;
    	 System.out.println("存款成功");
     }
	}
}

Customer.java

package com.xx.study;

public class Account {
	private int id;// 账号
	private double balance;// 余额
	private double annualInterestRate;// 年利率

	public Account(int id, double balance, double annualInterestRate)// 构造器
	{
		this.id = id;
		this.balance = balance;
		this.annualInterestRate = annualInterestRate;

	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public double getBalance() {
		return balance;
	}

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

	public double getAnnualInterestRate() {
		return annualInterestRate;
	}

	public void setAnnualInterestRate(double annualInterestRate) {
		this.annualInterestRate = annualInterestRate;
	}

	public void withdrew(double amount) {// 取钱
      if(balance<amount) {
    	  System.out.println("余额不足,取款失败");
    	  return;
      }
      balance-=amount;
      System.out.println("成功取出"+amount);
	}

	public void deposit(double amount) {// 存钱
     if(amount>0) {
    	 balance+=amount;
    	 System.out.println("存款成功");
     }
	}
}

CustomerTest.java

package com.xx.study;

public class CustomerTest {
  public static void main(String[] args) {
	Customer cust=new Customer("Jane","Smith");
	Account acct=new Account(1000,2000,0.0123);
	cust.setAccount(acct);
	cust.getAccount().deposit(100);
	cust.getAccount().withdrew(960);
	System.out.println(cust.getLastName()+"ddd"+cust.getAccount().getId()+"ai "+cust.getAccount().getAnnualInterestRate()*100+"%");
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Java的IO流和字符串操作来修改txt文件中某一行的内容。具体步骤如下: 1. 创建一个File对象来表示要修改的txt文件。 2. 创建一个BufferedReader对象来读取txt文件内容。 3. 创建一个StringBuilder对象来存储修改后的txt文件内容。 4. 通过BufferedReader对象逐行读取txt文件内容,并在需要修改的行进行字符串替换操作。 5. 将修改后的内容写入StringBuilder对象中。 6. 关闭BufferedReader对象。 7. 创建一个FileWriter对象来写入修改后的内容。 8. 将StringBuilder对象中的内容写入txt文件。 9. 关闭FileWriter对象。 下面是一个修改txt文件中某一行内容的示例代码: ```java import java.io.*; public class ModifyTxtFile { public static void main(String[] args) { String filePath = "test.txt"; // 要修改的txt文件路径 int lineNumber = 3; // 要修改的行号 String newLineContent = "This is the new line content."; // 修改后的内容 try { // 创建File对象 File file = new File(filePath); // 创建BufferedReader对象 BufferedReader reader = new BufferedReader(new FileReader(file)); // 创建StringBuilder对象 StringBuilder sb = new StringBuilder(); // 逐行读取txt文件内容并进行替换操作 String line; int lineNum = 1; while ((line = reader.readLine()) != null) { if (lineNum == lineNumber) { sb.append(newLineContent).append(System.lineSeparator()); // 添加修改后的内容 } else { sb.append(line).append(System.lineSeparator()); // 添加原内容 } lineNum++; } // 关闭BufferedReader对象 reader.close(); // 创建FileWriter对象 FileWriter writer = new FileWriter(file); // 将StringBuilder对象中的内容写入txt文件 writer.write(sb.toString()); // 关闭FileWriter对象 writer.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 在这个示例中,我们将第3行的内容修改为"This is the new line content."。你可以根据自己的需求修改这个示例代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值