ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

21. java面向对象 - JavaBean

2020-01-12 16:01:36  阅读:258  来源: 互联网

标签:java name void 21 age balance public JavaBean String


一、定义

​ JavaBean是一种Java语言写成的可重用组件,所谓JavaBean是指符合如下要求的java类:①类是公共的 ②有一个无参的公共构造器 ③有属性,且有对应的get、set方法。用户可以使用JavaBean将功能、处理、值、数据库访问和其他任何可以用Java代码创造的对象进行打包,并且其他的开发者可以通过内部的JSP页面、Servlet、其他JavaBean、applet程序或应用来使用这些对象。用户可以认为JavaBean提供了一种随时随地的复制粘贴的功能,而不要关心任何改变。

二、this关键字

  • this可以修饰属性、方法、构造器
    • this理解为当前对象
    • 在类方法中,用"this.属性"或"this.方法"是调用对象的属性或方法,一般我们都省this
public class Person {
    private String name;
    private int age;

    //构造器
    public Person(){
        this.eat();
    }

    public Person(String name){
        this.name = name;
    }

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


    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
    //如果不加this,则设置是就是空字符串,因为name就是形参就近原则
    //加上后,就可区分this.name是属性;后面name是形参
    public void setName(String name) {
        this.name = name;
    }

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

    public void eat(){
        System.out.println("people eat fruit");
        this.study();
    }

    public void study(){
        System.out.println("pepole study english");
    }
}

class PersonTest{
    public static void main(String[] args) {
        Person p1 = new Person();
        p1.eat();
    }
}
  • this调构造器
    • 我们在构造器中,可以显示使用"this(形参列表)"方式调用指定本类中指定其他构造器
    • 构造器中,不能使用"this(形参列表)"方式调用自己
    • 如果一个类中有n个构造器,最多有n-1个调用,不能成环
    • 构造器中只能声明一个"this(形参列表)"
public class Person {
    private String name;
    private int age;

    //构造器;若没创建对象都要去实现一段代码为了减少冗余,使用this调构造器
    public Person(){
        System.out.println("person 需要创建很多...,现行创建");
    }

    public Person(String name){
        // 调用空参构造器
        this();
        this.name = name;
    }

    public Person(String name, int age){
        // 调用带参构造器,此构造起中有name参数,就不需要自己创建了
        this(name);
        //this.name = name;
        this.age = age;
    }

    // 封装性体现
    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

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

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

    public void eat(){
        System.out.println("people eat fruit");
        this.study();
    }

    public void study(){
        System.out.println("pepole study english");
    }
}

class PersonTest{
    public static void main(String[] args) {
        Person p1 = new Person("tom", 18);
        System.out.println(p1.getAge());
    }
}

三、实例

1. account

public class Account {
    /*
     * id:账号
     * balance:余额
     * annualInterestRate:年利率
     * */
    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;
    }

    //get和set方法
    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("sorry, Insufficient balance");
            return;
        }
        balance -= amount;
        System.out.println("Successfully withdraw" + " " + amount + " yuan!");
    }

    //存钱
    public void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
            System.out.println("Successfully deposited" + " " + amount + " yuan!");
        }
    }
}

2. customer

public class Customer {
    private String firstName;
    private String lastName;
    //对象属性
    private Account account;

    public Customer(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Account getAccount() {
        return account;
    }

    public void setAccount(Account account) {
        this.account = account;
    }
}

3. customerTest

public class CustomerTest {
    public static void main(String[] args) {
        /*
        * 一个用户Jane Smith,账号1001,余额2000,利率1.23%
        * */
        Customer customerObj = new Customer("Jone","Smith");
        Account accountObj = new Account(1001, 2000, 0.0123);
        //在银行开个户,设置id=1001,金额2000
        customerObj.setAccount(accountObj);
        //先通过用户获取账户,存100元,取960元
        customerObj.getAccount().deposit(100);
        customerObj.getAccount().withdraw(960);
        customerObj.getAccount().withdraw(2000);
    }
}

标签:java,name,void,21,age,balance,public,JavaBean,String
来源: https://www.cnblogs.com/hq82/p/12182881.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有