首页 > 编程语言 >Java基础-Day11

Java基础-Day11

时间:2023-07-05 17:35:14浏览次数:42  
标签:Customer customer Account Java String 基础 Day11 return public

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

相关文章

  • JAVA微服务分布式事务的几种实现方式
    基础理论CAP理论一致性(Consistency):在分布式系统中所有的数据备份,在同一时刻都保持一致状态,如无法保证状态一致,直接返回错误;可用性(Availability):在集群中一部分节点故障,也能保证客户端访问系统并得到正确响应,允许一定时间内数据状态不一致;分区容错性(Partitiontolerance):分布式......
  • python基础day38 并发编程
    进程概念进程、线程都是操作系统中的基本概念,也就是说进程和线程都是操作系统层面的东西,专业术语表达就是进程和线程都是由操作系统来调度的,而不是由我们程序员自己来操控的。在操作系统这门课里面,进程和线程是操作系统的概念,协程不是操作系统中的概念,而是我们程序员层面的协程......
  • 浅谈java8中map的新方法
    Map在java8中新增了两个replace的方法1.replace(k,v)在指定的键已经存在并且有与之相关的映射值时才会将指定的键映射到指定的值(新值)在指定的键不存在时,方法会return回来一个nulljavadoc的注释解释了该默认值方法的实现的等价Java代码:if(map.containsKey(key)){returnmap.put(ke......
  • python基础 进程、操作系统调度算法、同步异步、开启进程、process类的参数、进程锁、
    进程概念进程、线程都是操作系统中的基本概念,也就是说进程和线程都是操作系统层的东西,专业术语表达就是进程和线程的使用都是由操作系统来调度的‘,而不是由我们来操控的。在操作系统这门课里,进程和线程是操作系统的概念,协程不是操作系统中的概念,而是我们程序层面的......
  • 金九银十跳槽涨薪Java面试题!568页真题+答案解析,大厂都在考
    2023年一半又过去了,各大企业的招聘也又开始大量放岗了,各位苟着的小伙伴们要抓住机会了! 但很多小伙伴对面试不够了解,不知道如何准备,对面试环节的设置以及目的不了解,尤其是面试题还很难,有些还偏重实战,因此成功率不高。要知道目前校招生面试的成功率低于1%,而社招的面试成功率也......
  • Java面向对象
    Java面向对象什么是面向对象1.面向过程步骤简单清晰2.面向对象分类的思维模式适合处理复杂的问题3.oop以类的方式组织代码,以对象的组织(封装)数据4.特点:抽象5.特性:封装、继承、多台回归方法的定义和调用1.方法的定义1.return代表方法结束了......
  • 【笔试实战】LeetCode题单刷题-编程基础 0 到 1【三】
    682. 棒球比赛题目链接682. 棒球比赛题目描述你现在是一场采用特殊赛制棒球比赛的记录员。这场比赛由若干回合组成,过去几回合的得分可能会影响以后几回合的得分。比赛开始时,记录是空白的。你会得到一个记录操作的字符串列表 ops,其中 ops[i] 是你需要记录的第 i 项操作......
  • JavaWeb知识
    day01-XML&tomcatXML介绍XML:eXtendsiblemarkuplanguage可扩展的标记语言XML有什么用?1.可以用来保存数据2.可以用来做配置文件3.数据传输载体定义XML其实就是一个文件,文件的后缀为.xml文档声明简单声明:version:解析这个xml的时候,使用什么版本的解析器解析<?xmlversi......
  • JAVA 调试高内存占用与CPU满载异常场景
    高内存占用,堆溢出,OOM代码: @RequestMapping(value="/oom",method={RequestMethod.GET}) publicResultBasegetMessage2()throwsInterruptedException{ List<String>strList=Lists.newArrayList(); for(inti=0;i<10240;i++){ strLi......
  • java常见面试题分析及答案
    new一个object对象占多少字节对象头加实例数据16个字节java反射的优缺点Java反射的优点1.增加程序的灵活性,避免将程序写死到代码里2.代码简洁,提高代码的复用率,外部调用方便3.对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法Java反射......