1. To understand classes, you must understand the concepts of Java entities and instantiation.
2. In Java programs, the program components that describe the object abstraction concepts are called classes, and the program components that describe the specific entity are called objects. The attribute of an object is defined as a data member of the class, and the behavior of the object is defined as a method member of the class.
3. The class name must be a valid identifier and should start with an upper-case letter. The content enclosed by curly braces after the class name is called the class body. Multiple members can be declared in the class body. These members are divided into data members and method members. The user-defined attributes of the data member class. For example, balance can indicate the current deposit balance of the account. This method is used to define the behavior of the class.
4. The usage class must comply with the "declare first, then use" rule.
When declaring a class member, the private and public keywords can be used to control whether the data member or method member allows other programs to access the program fragment.
The private Declaration Member is a private member and can only be accessed by the method member declared in this class.
The declared members after public are called public members. These members are interfaces of the class and the external world. Besides the method members declared by this class, they can access these members, the method members declared in other classes or the main () method can access these members.
5.
The public method members that return private data members are also called getter methods. These public methods are usually named get ...()
The public method member for modifying private data members is also called the setter method. These methods are usually named set ...()
6. classes defined in a program can not only divide complex programs into modules, but also each class can be used as a user-defined type. That is to say, a class is also a type. It can declare a specific instance like an int or double basic data type. These instances are called objects.
For example, acount () is a class for managing bank accounts. The following bank () is a class for transferring funds.
7. The new keyword is used to allocate buckets to objects and initialize the values of these buckets. When an object is created, it is used as a template. It determines how the object storage space is organized.
Public class bank ()
...{
Public static void main (sting [] ARGs)
...{
Acount Zhang = new acount (); // opens an account for Zhang
Zhang. Deposit (500); // Zhang's account is saved to 500
If (! Zhang. Withdraw (100) // withdraw 100 from the Zhang account
System. Out. println ("withdrawal failed due to insufficient balance ");
Acount li = new acount (); // opens an account for Li
If (! Zhang. Withdraw (150 ))
System. Out. println ("insufficient balance, transfer failed"); // start the transfer
Else
...{
Li. Deposit (150 );
}
System. Out. println ("Zhang account balance" + Zhang. getbalance ());
System. Out. println ("the Li account balance is" + Li. getbalance ());
}
}
8. Static members
In a bank account, the overdraft quota attribute is not so much the attribute of each credit card as the overall attribute of the credit card. As long as the attribute is defined as static, that is, the keyword static is added before the attribute definition, the attribute is defined as a class attribute, rather than the attribute of an object instance, the Public Area of the class. All static data members are shared by all object instances of a class.
Not only can data members of a class be defined as static, but also methods of a class be defined as static. For static method members, they can be called directly with the class name and decimal point prefix without creating any object instance. Because static method members can execute without object instances, therefore, no non-static members are referenced in the method bodies of static method members. In their method bodies, only static data members can be referenced or static method members can be called.
Public class adjustablecreditcard
...{
Private Static double maxoverwriting = 1000;
Private double balance = 0;
Public static void adjustoverdraft (double max)
...{
Maxoverdraft = max;
}
Public static void deposit (double amount)
...{
Maxoverdraft = max;
}
Unfinished