Core java Chapter 4 notes, corejava
Import java. util. *; public class Employee {private static int nextid = 1; private String name; private double salary; private int id; public Employee (String n, double s) {name = n; salary = s; id = 0;} public String getname () {return name;} public double getsalary () {return salary;} public int getid () {return id ;} public void setid () {id = nextid; nextid ++;} public static int getnextid () {return nextid;} public static void mian (String [] args) {Employee e = new Employee ("Harry", 50000); System. out. println (e. getname () + "" + e. getsalary ();} public static void main (String [] args) {// method stub automatically generated by TODO: Employee [] staff = new Employee [3]; staff [0] = new Employee ("TOM", 40000); staff [1] = new Employee ("Dick", 60000 ); staff [2] = new Employee ("Harry", 65000); for (Employee e: staff) {e. setid (); System. out. println ("name =" + e. getname () + "id =" + e. getid () + "salary =" + e. getsalary ();} int n = Employee. getnextid (); System. out. println ("next available id =" + n );}}
1/The method in the class is marked as public. The public keyword means that any method of any class can call these methods.
2/use in the class
private int a;
This means that only the methods of the class can access these instance domains, while the methods of other classes cannot read and write these domains.
3/sometimes, an instance domain itself is an object. For example, we define a private String name in the class; The name domain is a String object, classes generally include instance domains of a type.
4/The constructor has the same name as the class. When we construct a class object, the constructor runs to initialize the instance domain to the desired state.
5/The constructor is always called along with the execution of the new operator. You cannot call the constructor on an existing object to reset the instance domain.
For example:
staff[0]=new Employee("diyige",75000,1987,12,25);
A. the constructor has the same name as the class.
B. Each class can have more than one constructor.
C. The constructor can have 0, 1, or multiple parameters.
D. The constructor does not return values.
E. The constructor is always called along with the new operation.
F. Do not construct local variables with the same name as the instance domain in the constructor.
6. In each method, the keyword "this" indicates an implicit parameter, for example;
public void raisesalary(double bypercent){ double raise = this.salary*bypercent/100; this.salary = this.salary + raise;}
In this method, double bypercent is called a display parameter.
7. The accessor method is used only to view and return the object status without modifying the instance domain.
The method for modifying the instance domain is called the modifier method.
8. The instance domain can be defined as final. Such a domain must be initialized when an object is built. That is to say, after being executed by a constructor, the value of this field must be set and cannot be modified in subsequent operations. In the above program, the name in the Employee class can be declared as final, because after the object is constructed, the value of the name will not be modified.
The 10/static keyword indicates that the instance domain belongs to the class and does not belong to any object.
For example, a static constant is defined in the Math class.
Public class Math(){ ······ public static final double PI = 3.1415926; ······}
In the program, you can use Math. PI to obtain this constant.
If static is omitted, PI will become an instance domain of Math, so only the object of the Math class can access PI, in addition, each Math object has its own copy of PI.
11. The static method is a method that cannot be operated on objects. For example, the pow () method in the Math class is a static method.
Math. pow (x, );
No Math object is used in the operation. That is to say, there is no implicit parameter. Therefore, it can also be considered as a method without the this parameter. In the Employee class, static methods cannot access the id instance domain because they cannot operate on objects, but static methods can access their own static fields.
For example:
public static int getnextid(){ return nextId;}
You can call this method by Class Name:
int n = Employee.getnextid();
Static can also be omitted, but if it is omitted, this method must be called through the object of the Employee class.
Generally, the static method is used in two cases:
A: A method does not need to access the object state. All required parameters are provided by explicit parameters.
B: A method only needs the static domain of the callback class.
The most common main method is also a static method because it does not operate on any object. When we started eclipse, there was no object yet.
12/There are three ways to pass parameters to the method in the programming language:
Call by value: indicates that the method accepts the value provided by the caller.
Reference call: The method accepts the variable address provided by the caller.
Name call: this transfer method has become a history.
In the world of Java, it is always called by value.
There are two types of method parameters:
A. Basic Data Types
B. Object Reference
When java references a row of the basic data type, its value is not changed. When the object is referenced, it will change.
13/When writing the constructor, you can add a before each parameter to enhance the readability of the Code.
14/If the first statement in the constructor is like this (...); this constructor will call another constructor of the same class, in this way, the common constructor code can be written only once.
15/Java allows classes to be organized using packages. The main reason for using the package is to ensure the uniqueness of the class name. To ensure the absolute uniqueness of the package name, sun recommends that the company's Internet domain name be used as the package name in reverse order.
A class can use all the classes of the package and the public classes of other packages. We can use two methods. The first is to add the complete package name before each class name. For example:
java,util.Date today = new java.util.Date();
This method is really flawed.
The other is to use
import java.util.*;
You can import all classes in the java. util package.
Import java.util.Date;
You can import a specific class in a package.