AP计算机A自学笔记:方法类型

  1. constructor 构造函数
    构造函数创建该类的一个对象,constructor无返回值
    constructor分两类:
    1 default constructor:
    不传入参数,使用默认值
    2 constructors with parameters:
    创建新对象时传入参数
  2. accessors 访问器
    访问一个对象,有返回值
  3. mutators 存取器
    改变一个对象,如改变该对象一个变量值
package aPComputerScience;

public class BankAccount {
	//this is an example of class
	
	
	//static variables contain values that are shared by all instances of the class
	//static variables are initialized outside the constructor
    private String password;
    private double balance;
    //static final variables cannot change values
    public static final double OVERDRAWN_PENALITY = 20.00;
    
    
    //constructors
    /*A constructor creates an object of the class
     *A constructor has no return type
     */
    
    /**Default constructor
     * Constructs a bank account with default values
     * @return 
     */
    public BankAccount() {
    	password = "";
    	balance = 0.00;
    }
    BankAccount a = new BankAccount();
    
    /**Constructor with parameters 
     * Constructs a bank account with specified password and balance
     */
    public BankAccount(String acctPassword, double acctBalance) {
    	password = acctPassword;
    	balance = acctBalance;
    }
    BankAccount b = new BankAccount("KevinC", 15.00);
    //a and b are object variables that store the addresses of their respective BankAccount object
    
    
    //accessors
    //An accessor method accesses a class object without alerting the object
    //An accessor return some information
    public double getBalance() {
    	return balance;
    }
    
    
    //mutators
    /**Deposits amount in a bank account with the given password.
     * @param acctPassword
     * @param amount the amount to be deposited
     */
    public void deposit(String acctPassword, double amount) {
    	if (!acctPassword.equals(password)) {
    		System.out.println("Wrong password");
    	} else {
    		balance += amount;
    	}
    }
    /**
     * Withdraws amount from bank account with given password.
     * Assesses penalty if balance is less than amount
     * @param acctPassword the password of this bank account
     * @param amount the amount to be withdrawn
     */
    public void withdraw(String acctPassword, double amount) {
    	if (!acctPassword.equals(password)) {
    		System.out.println("wrong password");
    	} else {
    		balance -= amount;
    		if (balance < 0) {
    			balance -= OVERDRAWN_PENALITY;
    		}
    	}
    }
}


此处创建一个类叫银行账户,分别给出constructor, accessor, 和mutator的例子

package aPComputerScience;

public class BankAccountClient {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
    BankAccount b1 = new BankAccount("Adam", 500.0);
    BankAccount b2 = new BankAccount("Bella", 800.0);
    b1.deposit("Adam", 200.0);
    b2.withdraw("Bella", 400.0);
    b2.deposit("Belle", 20000.0);
    if (b1.getBalance() > b2.getBalance()) {
    	System.out.println("Adam is richer");
    } 
	}

}

此处给出对该类对象的几个方法的调用示例

上一篇:javascript面向对象


下一篇:ES6面向对象新特性