Day 9 (this关键字与实验)

this:表示当前对象或当前正在创建的对象,可调用本类属性,调用本类方法或构造器。

在类的方法中,可用this.属性名this.方法来调用当前对象的属性和方法,通常省略,当类中方法的形参名与属性名同名时,必须采用this.属性名来操作。(类的构造器情况类似)

1、调用本类属性

public class BookTest {
    public static void main(String[] args) {
        Book book = new Book();
        book.setBookName("The Ordinary World");
        book.setBookPrice(66);
        System.out.println(book.getBookName());
        System.out.println(book.getBookPrice());
    }
}

class Book{
    private String bookName;
    private int bookPrice;
    //方法形参名与类中属性名相同
    public void setBookName(String bookName){ 
        //利用this.属性名来区别
        this.bookName = bookName;
    }
    public String getBookName(){
        return this.bookName;
    }
    public void setBookPrice(int bookPrice){
        this.bookPrice = bookPrice;
    }
    public int getBookPrice(){
        return this.bookPrice;
    }
}

2、调用构造器

        (1)在类的构造器内部,通过使用this(形参列表)来调用当前类的其他构造器

        (2)构造器内部不能采用this来调用自己,且最多只能有一个this调用语句

        (3)使用this(形参列表)调用构造器语句必须放在构造器方法首行

public class BookTest {
    public static void main(String[] args) {
        Book book = new Book("The Ordinary World",66);
        System.out.println(book.info());
    }
}

class Book{
    private String bookName;
    private int bookPrice;
    public Book(){
        System.out.println("无参构造器");
    }

    public Book(String bookName){
        this();
        this.bookName = bookName;
    }
    public Book(String bookName,int bookPrice){
        this(bookName);
        this.bookPrice = bookPrice;
    }
    public String info(){
        return "the name of the book is:" + this.bookName + ", the price of the book is:" + this.bookPrice;
    }
}

3、实验

实验一

        创建Boy和Girl两个类,并创建私有属性name、age,并在对应的类中分别声明set/get方法以及相应的构造器,marry、shout、compare为各自类中的方法。(-表示private,+表示public) 

//Boy.java代码如下:

public class Boy{
    private String name;
    private int age;
//声明双参构造器
    public Boy(String name, int age) {
        this.name = name;
        this.age = age;
    }
//声明set get方法
    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getAge() {
        return age;
    }
//声明marry方法
    public void marry(Girl girl){
        System.out.println("marry:"+girl.getName());
    }
    //声明shout方法
    public void shout(){
        if(this.age >= 22){
            System.out.println("you can marry");
        }else{
            System.out.println("you're too young to marry");
        }
    }
}


//Girl.java代码如下:

public class Girl {
    private String name;
    private int age;

    public Girl(String name, int age) {
        this.name = name;
        this.age = age;
    }

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

    public String getName() {
        return name;
    }
    public void marry(Boy boy){
        System.out.println("marry to:"+boy.getName());
        boy.marry(this); //this:谁调用marry方法谁就是当前对象
    }
    //比较 当前对象 与 传递进来的对象 的年龄大小
    public int compare(Girl girl){
        return this.age-girl.age;
    }
}

//main() 代码如下:

public class BoyGirlTest{
    public static void main(String[] args) {
        Boy boy = new Boy("Mike",22);//Boy类的实例化
        boy.shout();//调用Boy类的方法
        Girl girl = new Girl("Marry",20);//Girl类的实例化
        girl.marry(boy);//调用Girl类的方法
        Girl girl1 = new Girl("Lily",24);
        int result = girl.compare(girl1);//比较girl与girl1的年龄 girl调用(为当前对象)
        if (result > 0){
            System.out.println(girl.getName()+"年龄大");
        }else if (result < 0){
            System.out.println(girl1.getName()+"年龄大");
        }else{
            System.out.println(girl.getName()+"和"+girl1.getName()+"年龄一样大");
        }
    }
}

实验二:

        创建Customer类和Account类,并实现存取款功能,并显示客户账户信息。

                (1)Customer类代码如下:

public class Customer {
    private String firstName;
    private String lastName;
    private Account account;
//声明双参构造器
    public Customer(String f, String l) {
        firstName = f;
        lastName = l;
    }
//声明相关set get方法
    public String getFirstName() {
        return firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setAccount(Account account) {
        this.account = account;
    }
    public Account getAccount() {
        return account;
    }
}

                (2)Account类代码如下:

public class Account {
    private int id;
    private double balance;
    private double annualInterestRate;
//声明三参构造器
    public Account(int id, double balance, double annualInterestRate) {
        this.id = id;  //用户id
        this.balance = balance;  //用户余额
        this.annualInterestRate = annualInterestRate;  //年利率
    }
//声明属性相关的set  get方法
    public void setId(int id) {
        this.id = id;
    }

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

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

    public int getId() {
        return id;
    }

    public double getAnnualInterestRate() {
        return annualInterestRate;
    }

    public double getBalance() {
        return balance;
    }
    //声明取钱方法
    public void withdraw(double amount){
        if(this.balance < amount){
            System.out.println("余额不足,取款失败");
        }else{
            this.balance -= amount;
            System.out.println("成功存入:"+amount);
        }
    }
    //声明存钱方法
    public void deposit(double amount){
        if (amount > 0) {
            this.balance += amount;
            System.out.println("成功取出:"+amount);
        }
    }
}

                (3)功能测试 

public class AccountCustomerTest {
    public static void main(String[] args) {
        //Customer类的实例化
        Customer c1 = new Customer("Jane","Smith");
        //Account类的实例化
        Account a1 = new Account(1000,2000,0.0123);
        //将对象c1的账户对象设置为对象a1  (将两个类联系起来)
        c1.setAccount(a1);
        //通过getAccount方法调用存钱方法
        c1.getAccount().deposit(100);
        //调用取钱方法
        c1.getAccount().withdraw(960);
        c1.getAccount().withdraw(2000);
        //打印客户账户信息
        System.out.println("Customer ["+c1.getLastName()+","+c1.getFirstName()+" has a account: id is "+a1.getId()
                +", annualInterestRate is "+ a1.getAnnualInterestRate()*100 +"%, balance is "+a1.getBalance());
    }
}

                输出: 

 实验三:

        创建Customer1、Account1、Bank类以及相关方法,并写出BankTest测试相关功能

                (1)Customer1类代码如下:

public class Customer1 {
    private String firstName1;
    private String lastName1;
    private Account1 account1;

    public Customer1(String f,String l) {
        this.firstName1 = f;
        this.lastName1 = l;
    }

    public String getFirstName1() {
        return firstName1;
    }

    public String getLastName1() {
        return lastName1;
    }

    public void setAccount1(Account1 account1) {
        this.account1 = account1;
    }
    public Account1 getAccount1() {
        return account1;
    }
}

                (2)Account1代码如下:

public class Account1 {
    private double balance;

    public Account1(double init_balance) {

        this.balance = init_balance;
    }

    public double getBalance() {
        return balance;
    }

    public void withdraw(double amt) {
        if (this.balance < amt) {
            System.out.println("余额不足,取款失败");
        } else {
            this.balance += amt;
            System.out.println("取款成功:" + amt + ",当前余额为: " + this.balance);
        }
    }

    public void deposit(double amount) {
        if (amount > 0) {
            this.balance += amount;
            System.out.println("存款成功:" + amount + ",当前余额为: " + this.balance);
        } else {
            return;
        }
    }
}

                (3)Bank类代码如下:

public class Bank {
    //创建私有 Customer1对象数组  存放多个客户
    private Customer1[] customers = new Customer1[10];   //10*3
    private int NumberOfCustomer;
    //创建无参构造器
    public Bank(){
    }

    public void addCustomer(String f,String l){
        //创建新Customer对象 并调用构造器赋初值
        Customer1 c1 = new Customer1(f,l);
        //customerIndex:客户所在对象数组中的索引
        int customerIndex = this.NumberOfCustomer++;
        //将新创建的对象c1放入对象数组中
        this.customers[customerIndex] = c1;
    }
    public int getNumberOfCustomers(){
        return this.NumberOfCustomer;
    }
    //返回值为对象
    public Customer1 getCustomer(int index){
        //如果索引满足条件
        if (index >= 0 && index < this.NumberOfCustomer){
            //返回index所在的对象
            return this.customers[index];
        }else{
            return null;
        }
    }
}

                (4)BankTest测试

public class BankTest {
    public static void main(String[] args) {
        //Bank实例化
        Bank bank = new Bank();
        //添加客户
        bank.addCustomer("Zhang","GuoHua");
        //创建客户
        bank.getCustomer(0).setAccount1(new Account1(2000));
        //取钱
        bank.getCustomer(0).getAccount1().withdraw(500);
        //存钱
        bank.getCustomer(0).getAccount1().deposit(10000);
        bank.getCustomer(0).getAccount1().withdraw(20000);
        //获取指定客户余额信息
        double theRest= bank.getCustomer(0).getAccount1().getBalance();
        System.out.println("客户"+bank.getCustomer(0).getLastName1()+"的余额为: "+theRest);
        System.out.println("-----------------------------------");
        bank.addCustomer("DaHua","OuYang");
        //获取当前客户人数
        System.out.println("当前银行客户人数为: "+bank.getNumberOfCustomers());
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值