This article address: http://www.cnblogs.com/archimedes/p/java-study-note3.html, reprint please indicate source address.
1. An overview of object-oriented program design methods
Object-Oriented Programming
Compared with the structured programming method, it is more in line with the human understanding of the real world thinking mode, has become the mainstream direction of program design
Key concepts involved: abstraction, encapsulation, inheritance, polymorphism
Object
In the real world: All things are objects, they have their own attributes, and they show their behavior to the outside.
In the program: Everything is an object, has identity, attribute and Behavior (method), saves its state through one or more variables, and implements his behavior through methods.
classattribute and behavior of the same or similar objects are classified as a class, classes can be regarded as an abstract object, representing the common properties and behavior of such objects, in object-oriented program design, each object belongs to a particular class
Abstract
Ignoring aspects of the problem that are not relevant to the current goal in order to pay more attention to the aspects related to the current objectives; the abstractions used in computer software development are: process abstraction and Data abstraction
Process abstraction:
–-the function of the whole system into several parts, emphasizing the process and steps of function completion, and hiding its concrete implementation
–-any well-defined function operation can be considered as a single entity, although this operation may actually be done by a series of lower-level operations
–-two standard program design process based on process abstraction: decomposition, recursive technology
Data Abstraction
–-combines the data that needs to be processed with the operations on the data, abstracting it into different abstract data types
–-each abstract data type contains both data and operations for that data
–-data abstraction is a more reasonable abstract approach than process abstraction
Example: Clocks
Data (attributes)
int Hour; int Minute; int Second;
Method (Behavior)
SetTime (); ShowTime ();
Packaging
is a kind of information hiding technology
Encapsulate data and data-based operations with abstract data types
The user can only see the object's encapsulated interface information, the object's internal details are hidden from the user
The purpose of encapsulation is to separate the object's consumer from the designer, and the user does not need to know the details of the behavior implementation, simply using the message from the designer to access the object
Inherited
Refers to a new class that can obtain properties and behaviors of an existing class (called a superclass, base class, or parent Class), and that the new class is a derived class (also known as a subclass) of an existing class
Derived classes inherit the attributes of the base class, including methods and instance variables, during inheritance
Derived classes can also modify inherited methods or add new methods to make them more suitable for special needs
Helps to solve the problem of reusability of software, makes the program structure clear, reduces the workload of coding and maintenance
PS:Java language only supports single inheritance
Polymorphic
Coexistence of different methods with the same name in a program
Mainly through the subclass of the parent class method overrides to implement
Different classes of objects can respond to messages with the same name (methods), but the implementation method is different
The advantage of flexible, abstract, behavioral sharing, and code sharing, which solves the problem of application method with the same name.
2. Classes and objects
In a program, an object is described by an abstract data type called a class, and a class is a description of a category of objects. A class is a template that constructs an object, which is a concrete instance of a class
Declaration declaration form for a class:
[Public] [Abstract | final] class name [Extends parent class name] [implements interface Name list] {
Declaration and initialization of variable members;
Method declaration and method body;
}
Class: Indicates that the declaration is followed by a category.
Extends: If the declared class is derived from a parent class, the name of the parent class should be written after extends
Implements:l if the class being declared implements some interface, then the name of the interface should be written after implements
Modifier: can have more than one, to qualify how the class is used
Public: Indicates that the class is
Abstract: Indicates that this class is an abstraction class
Final: Indicates that this class is a terminating class
Let's look at an example of a clock class:
Public classclock{//member Variables inthour; intminute; intsecond; //member Methods Public voidSetTime (intNEWH,intNEWM,intNewS) {Hour=NEWH; Minute=NEWM; Second=News; } Public voidShowTime () {System.out.println (hour+ ":" + Minute + ":" +second); }}
Declaration of the object
Format: class name Variable name
For example: Clock is a class name that has already been declared, then the variable Aclock declared by the following statement will be used to store a reference to that class object: Clock Aclock;
There is no object generation when declaring a reference variable
Creation of objects
The format of the build instance:
New < class name > ()
Example: Aclock=new Clock ()
Its role is:
Allocating memory space for this object in memory
Returns a reference to an object (reference, equivalent to the object's storage address)
A reference variable can be assigned a null value
For example: Aclock=null;
Data members
Represents the state of a Java class, which declares that the data member must give the variable name and the type to which it belongs, and can specify additional attributes. In a class, the member variable name is unique, and the type of the data member can be any data type in Java (Simple type, class, interface, array)
Divided into instance variables and class variables
– Declaration format
[ Public | Protected | Private]
[Static] [ final] [transient] [volatile]
Variable data type variable name 1[= variable initial value],
Variable name 2[= variable initial value], ...;
– Format Description
Public , protected, private are access control characters
Static indicates that this is a static member variable
Final indicates that the value of the variable cannot be modified
transient indicates that the variable is a temporary state
volatile indicates that a variable is a shared variable
A class variable , also known as a static variable, is declared with a static modifier, regardless of how many objects the class has, there is only one copy of the class variable, there is only one value in the entire class, and the class is initialized with the value assigned
Applicable situation:
All objects in a class have the same properties
Data that you often need to share
Some of the constant values used in the system
Example: For all objects of a circle class, the value of π is required to calculate the area of the circle, and a class attribute pi can be added to the Circle class declaration
Public classCircle {Static DoublePI = 3.14159265; intradius;} Public classClassvariabletester { Public Static voidMain (String args[]) {Circle x=NewCircle (); System.out.println (X.PI); System.out.println (CIRCLE.PI); Circle.pi= 3.14; System.out.println (X.PI); System.out.println (CIRCLE.PI); } }
Execution Result:
3.14159265
3.14159265
3.14
3.14
final modifier
instance variables and class variables can all be declared as final
The final instance variable must be assigned an initial value before the end of each construction method to ensure that it is initialized before use.
The final class variable must be initialized at the same time as the declaration
class method,Also called a static method, which represents the common behavior of an object in a class, the declaration is preceded by a static modifier and cannot be declared abstract, and the class method can be called directly with the class name without an object being created, or by invoking a class instance.
//convert Celsius temperature (centigrade) to Chenghua temperature (Fahrenheit)//conversion formula is Fahrenheit = Centigrade * 9/5 +//In addition to the constant values required in the Celsius temperature values and formulas, this function does not depend on the property values of the specific class instance, so it can be declared as a class method//Conversion method Centigradetofahrenheit in class converter Public classConverter { Public Static intCentigradetofahrenheit (intcent) { return(cent * 9/5 + 32); } }//Method InvocationConverter.centigradetofahrenheit (40);
Get method
The function is to get the value of the property variable, the Get method name begins with "Get", followed by the name of the instance variable, and generally has the following format:
Public <fieldType> get<fieldname> () {
Return <fieldName>;
}
For the instance variable radius, declare its Get method as follows:
Public int Getradius () { return
Set method
The function is to modify the value of the property variable, the Set method name begins with "set", followed by the name of the instance variable, and generally has the following format:
public void set<fieldname> (<fieldType> <paramName>) {
<fieldName> = <paramName>;
}
The set method for declaring the instance variable radius is as follows:
Public void Setradius (int r) { = r;}
Use of the keyword this
If the formal parameter name is the same as the instance variable name, you need to add the This keyword before the instance variable name, or the instance variable will be treated as a formal parameter.
In the set method above, if the formal parameter is radius, you need to precede the member variable radius with the keyword this. The code is as follows:
Public void Setradius (int radius) { this. radius = radius;}
Resources:
"Java Programming"--Tsinghua University
Java Learning Notes Basic concepts of 3--classes and objects (1)