Java基础-Day11
- 关键字this
- 知识点理解方式:3W:what?why?how?
-
this的使用示例:
-
this关键字的使用:
1.this可以用来修饰:属性、方法、构造器
2.this修饰属性和方法:
this理解为:当前对象
在类的方法中,我们可以使用 this.属性或this.方法的方式,调用当前对象的属性或方法。但是,通 常情况下,我们都选择省略 this. 特殊情况下,如果方法的形参和类的属性重名时,我们必须显式的使用 this.变量 的方式,来表明变量时属性,而非形参。
3.this调用构造器
①我们在类的构造器中,可以显式的使用 this(形参列表) 方式,调用本类中指定的其他构造器
②构造器不能通过 this(形参列表) 方式调用自己 ③规定:this(形参列表) 必须声明在当前构造器的首行,否则报错
④构造器内部,最多只能声明一个 this(形参列表)用来调用其他构造器
-
package com.lurenj.com;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
/*
*
* this关键字的使用:
* 1.this可以用来修饰:属性、方法、构造器
* 2.this修饰属性和方法:
* this理解为:当前对象
* 在类的方法中,我们可以使用 this.属性或this.方法 的方式,调用当前对象的属性或
* 方法。但是,通常情况下,我们都选择省略 this. 特殊情况下,如果方法的形参和类的
* 属性重名时,我们必须显式的使用 this.变量 的方式,来表明变量时属性,而非形参
* 3.this调用构造器
* ①我们在类的构造器中,可以显式的使用 this(形参列表) 方式,调用本类中指定的其他构造器
②构造器不能通过 this(形参列表) 方式调用自己
③规定:this(形参列表) 必须声明在当前构造器的首行,否则报错
④构造器内部,最多只能声明一个 this(形参列表)用来调用其他构造器
* */
public class PersonTest {
public static void main(String[] args) {
Person p1 = new Person();
p1.setAge(1);
System.out.println(p1.getAge());
}
}
class Person{
private String name;
private int age;
//构造器
public Person(int age){
this.age = age;
}
public Person(String name,int age){
this(age);//通过this()调用构造器
this.name = name;
}
public void setName(String name){
this.name = name;//this可以理解为当前对象
}
public String getName(){
return name;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return age;
}
}
实例1:Account_Customer
类Account:
package com.lurenj.exer1;
public class Account {
private int id;
private double balance;
private double annualInterestRate;
public Account(int id, int 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 withdraw(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("成功存入:" + amount);
}
}
}
类Customer
package com.lurenj.exer1;
public class Customer {
private String fistName;
private String lastName;
private Account account;
public Customer(String fistName, String lastName) {
this.fistName = fistName;
this.lastName = lastName;
}
public String getFistName() {
return fistName;
}
public String getLastName() {
return lastName;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
}
类CustomerTest
package com.lurenj.exer1;
public class CustomerTest {
public static void main(String[] args) {
Customer customer = new Customer("Jane","Smith");
Account account = new Account(1000,2000,0.0123);
customer.setAccount(account);
customer.getAccount().deposit(100);
customer.getAccount().withdraw(960);
customer.getAccount().withdraw(2000);
System.out.println("Customer [" + customer.getLastName()
+ ", " + customer.getFistName() + "]" + " has a account: id is "
+ customer.getAccount().getId() + ", annualInterestRate is "
+ customer.getAccount().getAnnualInterestRate() * 100 + "%" + ", balance is " + customer.getAccount().getBalance());
}
}
实例2:Account_Customer_Bank
类BanTest
package com.lurenj.exer2;
public class BankTest {
public static void main(String[] args) {
Bank bank = new Bank();
bank.addCustomer("Jane","Smith");
bank.getCustomer(0).setAccount(new Account(2000));
bank.getCustomer(0).getAccount().withdraw(500);
double balance = bank.getCustomer(0).getAccount().getBalance();
System.out.println("客户: " + bank.getCustomer(0).getFistName()
+ " 的账户余额为: " + balance);
System.out.println("******************");
bank.addCustomer("万里","杨");
System.out.println("银行客户的个数为: " + bank.getNumberOfCustomer());
}
}
类:Bank
package com.lurenj.exer2;
public class Bank {
private Customer[] customers;//可以存多个客户的数组
private int numberOfCustomers;//记录客户的个数
public Bank(){
customers = new Customer[10];
}
public void addCustomer(String f,String l){//添加客户
Customer customer = new Customer(f,l);
customers[numberOfCustomers] = customer;
numberOfCustomers++;
//或customers[numberOfCustomers++] = customer;
}
//获取指定位置上的客户
public Customer getCustomer(int index) {
if (index >= 0 && index < numberOfCustomers){
return customers[index];
}
return null;
}
//获取客户的个数
public int getNumberOfCustomer() {
return numberOfCustomers;
}
}
类Account
package com.lurenj.exer2;
public class Bank {
private Customer[] customers;//可以存多个客户的数组
private int numberOfCustomers;//记录客户的个数
public Bank(){
customers = new Customer[10];
}
public void addCustomer(String f,String l){//添加客户
Customer customer = new Customer(f,l);
customers[numberOfCustomers] = customer;
numberOfCustomers++;
//或customers[numberOfCustomers++] = customer;
}
//获取指定位置上的客户
public Customer getCustomer(int index) {
if (index >= 0 && index < numberOfCustomers){
return customers[index];
}
return null;
}
//获取客户的个数
public int getNumberOfCustomer() {
return numberOfCustomers;
}
}
类Customer
package com.lurenj.exer2;
import com.lurenj.exer2.Account;
public class Customer {
private String fistName;
private String lastName;
private Account account;
public Customer(String fistName, String lastName) {
this.fistName = fistName;
this.lastName = lastName;
}
public String getFistName() {
return fistName;
}
public String getLastName() {
return lastName;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
}
标签:Customer,customer,Account,Java,String,基础,Day11,return,public
From: https://www.cnblogs.com/lurenj/p/17529302.html