Objects and classes
On the concept of OOP, there are many introductions on the Internet.
Three main characteristics of the object:
The behavior of an object (behavior)--what actions can be applied to an object, or what methods can be applied to an object.
The state of the object-how the object responds when those methods are applied.
Identity of an object--how to distinguish between different objects that have the same behavior and state.
The most common relationships between classes are dependent ("uses-a"), Aggregations ("has-a"), Inheritance ("Is-a"). Dependency is one of the most obvious and most common relationships.
Objects and object variables, to use a variable, you must first construct the object, specify its initial state, and then exert a method on the object. Constructs a new instance using the constructor (constructor), which is a special method for constructing and initializing objects. The constructor should have the same name as the class name. For example, to construct a Date object, you need to precede the constructor with the new operator, such as New Date (), which constructs a new object. This object is initialized to the current date and time. If you want, you can also pass this object to a method: System.out.println (New Date ()), or you can apply a method to the object that you just constructed. There is a ToString method in the date class. This method will return a string description of the date: string s = new Daate (). toString (); Typically, objects that you want to construct can be used more than once, so you need to place the object in a variable: date birthday = new Date ();
There is an important difference between an object and an object variable. For example: Date deadling;//This is not an object. Defines an object variable deadline, which can reference a date-type object, but it is not an object in itself, nor does it refer to an object, and no date method can be applied to this variable at this time. The statement s = deadline.tostring () will compile the error. Variable deadline must be initialized first. Of course, you can initialize the variable with the newly constructed object: Deadline = new Date (), or reference an existing object: deadline = birthday; So the two variables will reference the same object. It is important to realize that an object variable does not actually contain an object, but simply refers to an object. In Java, the value of any object variable is a reference to an object that is stored in another place. The return value of the new operator is also a reference.
Change method and Accessor methods
The usual practice is to prefix the accessor method name with a get, preceded by a prefix set with the change method.
User-defined Classes
In Java, the simplest form of a class definition is:
Class ClassName
{
Constructor1
Constructor2
...
Method1
Method2
...
Field1
Field2
...
}
Example:
Import java.util.*;
public class Employeetest
{
public static void Main (string[] args)
{
File the staff array with three Employee objects
employee[] staff = new EMPLOYEE[3];
Staff[0] = new Employee ("Carl cracker", 7500,1987,12,15);
STAFF[1] = new Employee ("Harry Hacker", 50000,1989,10,1);
STAFF[2] = new Employee ("Tony Tester", 40000,1990,3,15);
Raise everyone ' s salary by 5%
for (Employee E:staff)
E.raisesalary (5);
Print out information about all Employee objects
for (Employee E:staff)
System.out.println ("name=" + e.getname ()
+ ", salary=" + e.getsalary ()
+ ", hireday=" + e.gethireday ());
}
}
Class Employee
{
Public Employee (String n,double s,int year,int month,int Day)
{
name = N;
Salary = s;
GregorianCalendar calendar = new GregorianCalendar (year,month-1,day);
GregorianCalendar uses 0 for January
Hireday = Calendar.gettime ();
}
Public String GetName ()
{
return name;
}
Public double getsalary ()
{
return salary;
}
Public Date Gethireday ()
{
return hireday;
}
public void Raisesalary (double bypercent)
{
Double raise = salary * BYPERCENT/100;
Salary + = raise;
}
private String name;
private double salary;
Private Date Hireday;
}
In this example, a source file contains two classes. When compiling, you can invoke the Java compiler using wildcard characters: Javac Employee*.java or just type: Javac Employeetest.java. After compiling, two class files Employeetest.class and Employee.class are created. There can be only one public class in a source file, but there may be any number of non-public classes.
With this example, we found that there is a constructor and 4 methods in the Employee class:
Public Employee (String n,double s,int year,int month,int Day);
Public String getName ();
Public double getsalary ();
Public Date gethireday ();
public void Raisesalary (double bypercent);
Three instance domains:
private String name;
private double salary;
Private Date Hireday;
All methods of this class are flagged as public. means that this method can be invoked by any method of any class. The domain uses the keyword private to ensure that only the employee class's own methods are accessible and other classes cannot read or write.
Attention:
The constructor has the same name as the class;
Each class can have more than one constructor;
The constructor can have 0, one, or more than 1 parameters;
Constructor has no return value;
Constructors are always used along with the new operator.
Implicit and explicit parameters: In this example the Raisesalary method has two parameters, the first parameter is called an implicit parameter, is the employee type object that appears before the method name, and the second parameter is the value in parentheses after the method name, which is an explicit argument. The keyword this represents an implicit argument.
Advantages of Encapsulation:
Sometimes, you need to get or set the value of an instance field, which should provide three items: a Private data field, a public domain accessor method, and a public domain change method. This is more complicated, but it has obvious benefits: you can change the internal implementation, in addition to the method of the class, will not affect other code; The Change method can perform error checking, but assigning a field directly to the domain will not do the processing. For example, the Setsalary method may check whether the salary is less than 0.
The final modifier is mostly applied to basic data type fields, or fields of mutable types, which are immutable classes if each method in the class does not change its object. The string class is a immutable class.
Static domain and static method:
If you define a field as static, then there is only one such domain in each class.
A static method is a method that cannot be applied to an object. For example, the Pow method of the math class is a static method. The expression Math.pow (x,a) computes power xa. It does not use any of the math objects at the time of operation. In other words, there is no implicit argument. You can think of a static method as a method without this parameter. The instance domain cannot be accessed in a static method because the static method cannot manipulate the object. However, static methods can access static fields in their own classes.
Use static methods in the following two situations:
When a method does not require access to the state of an object, its required parameters are provided through explicit parameters.
When a method only needs to access the static domain of the class.