Basic concepts of Java classes and objects

Enumeration Type

Enumeration types can be used when a finite set is required and the data in the finite set is a specific value. The definition of an enumeration type uses the keyword enum, which has the following syntax format:

[public] enum Enumeration Type Name [implements List of interface names]
{
	enum;
	Variable member declaration and initialization;
	Method declarations and body;
}
enum Score{
    EXCELLENT,
    QUAILFIED,
    FAILED;
};
public class ScoreTester {
    public static void main(String [] args){
        giveScore(Score.EXCELLENT);
    }
    public static void giveScore(Score s){
        switch (s){
            case EXCELLENT:
                System.out.println("EXCELLENT");
                break;
            case QUAILFIED:
                System.out.println("QUAILFIED");
                break;
            case FAILED:
                System.out.println("FAILED");
                break;
        }
    }
}

Enumeration types have the following characteristics:
1) The enumeration type is a class, not a simple integer type, and the enumeration value is an object of the class;
2) Enumeration type inherits java.lang.Enum class;
3) The enumeration type does not have a public constructor;
4) The enumeration values are public, static, final.

There are many default methods for enumeration types.
Enumeration types are equivalent to defining a class, so you can define data and methods for that class.
In addition, enumeration types can define constructors, but to prevent programmers from defining objects of enumeration types themselves, constructors of enumeration types can only be private and default to private.

Examples of applications

First, declare the bank account class, including status, construction method, get method and set method.

enum Grade{//Defines an enumeration type that represents the level of an account.
    VIP,
    General;
}
public class BankAccount {
    private String ownerName;
    private int accountNumber;
    private float balance;
    Grade grade;
    public BankAccount(){
        this("",0,0,Grade.General);
    }
    public BankAccount(String initName,int initAccNum,float initBal,Grade g){
        ownerName=initName;
        accountNumber=initAccNum;
        balance=initBal;
        grade=g;
    }
    public String getOwnerName(){
        return ownerName;
    }
    public int getAccountNumber(){
        return accountNumber;
    }
    public float getBalance(){
        return balance;
    }
    public Grade getGrade(){
        return grade;
    }
    public void setOwnerName(String newName){
        ownerName=newName;
    }

    public void setAccountNumber(int newNum) {
        accountNumber = newNum;
    }
    public void setBalance(float newBalance){
        balance=newBalance;
    }
    public void setGrade(Grade g){
        grade=g;
    }
}

Declare test classes:

public class AccountTester {
    public static void main(String args[]){
        BankAccount anAccount;
        anAccount=new BankAccount("zhangsan",10000,0,Grade.General);
        anAccount.setBalance(anAccount.getBalance()+100);
        System.out.println("here is the account:"+anAccount);
        System.out.println("account name:"+anAccount.getOwnerName());
        System.out.println("account number"+anAccount.getAccountNumber());
        System.out.println("Balance: $"+anAccount.getBalance());
        System.out.println("Grade:"+anAccount.grade);
    }
}
Output results:
here is the account:BankAccount@75b84c92
account name:zhangsan
account number10000
Balance: $100.0
Grade:General

Declare the toString() method

When an exclusive output to the display is required, its toString() method is usually called to convert the object's content to a string. All Java classes have a default toString () method, and if you need special conversion capabilities, you need to override the toString () method yourself.

The system default toString() method body is as follows;

getClass().getName()+'@'+Integer.toHexString(hashCode())

In practice, this method is often needed to return more meaningful information. This requires us to override our toString() method, whose main function is to use a string to describe the main information of an object.
Add the toString() method to BankAccount.

public String toString(){
        return (grade+"account #"+accountNumber+"with balance $"+balance);
    }
System.out.println("here is the account:"+anAccount.toString());

Then recompile the run test class;

Output:
here is the account:Generalaccount #10000with balance $100.0
account name:zhangsan
account number10000
Balance: $100.0
Grade:General

When declaring the toString() method, the following points should be noted:
1) The toString() method must be declared public;
2) The return type is String;
3) The method must be named toString with no parameters;
4) Do not use the output method System in the body of the method. Out. Println().

Format string output

Format the data using the instance method format of the DecimalFormat class in the toString() method. The DecimalFormat class is in java.text method.

public String toString(){
        return (grade+"account #"+accountNumber+"with balance"+new java.text.DecimalFormat("$0.00f").format(balance));
    }

Another way to format the output string is to use printf.
Output two decimal places with a $sign.

System.out.printf("$%.2f",anAccount.getBalance());

Declare class methods to generate special instances

	public static BankAccount example(){
        BankAccount ba=new BankAccount();
        ba.setOwnerName("zhangwei");
        ba.setAccountNumber(5000);
        return ba;
    }
    public static BankAccount example1(){
        BankAccount ba=new BankAccount();
        ba.setOwnerName("wanger");
        ba.setAccountNumber(2000);
        ba.deposit(1000);
        return ba;
    }
    public static BankAccount example2(){
        BankAccount ba=new BankAccount();
        ba.setOwnerName("mazi");
        ba.setAccountNumber(3000);
        ba.deposit(2000);
        return ba;
    }

Declare the method of saving and withdrawing money;

	public float deposit(float anAmount){
        balance+=anAmount;
        return (balance);
    }
    public float withdraw(float anAmount){
        balance-=anAmount;
        return (anAmount);
    }

Keywords: Java Back-end

Added by canobi on Fri, 17 Dec 2021 05:56:50 +0200