Dowemo

Programming concepts

If you've never used an object oriented programming language before you write code, learning some basic programming concepts that are essential is necessary. This course will introduce you to. and. In discussing how these concepts relate to real-world objects, the syntax of java is introduced.

What's an object?

Objects are key to technology. Look around around, you'll find a lot of real-world objects: Your dog, your desk, your tv, your bike, and so on.

All real-world objects share two features: Both of them have ( state ) and ( behavior ).


例如:


1. 如狗的状态有名称、颜色、集合、种类;行为有吠、摇尾巴、捡东西给主人(fetching)。


2. 单车状态有当前速度、当前档位、当前踏板节奏;行为有换挡、改变踏板节奏、刹车。






Identifying the state and behavior of real-world objects helps us think about the approach.

软件世界中的对象

Objects in the world of soft parts are similar to those in the real world. Both contain state and related behavior. Object stores its state in member variables, exposing its behavior by means of a method. Method to manipulate the internal state of object, and as the main mechanism for communication between objects. Hiding internal state and interacting with methods is the basic principle of packaging programming
作为软件对象建模的自行车

The bicycle object controls the use of the external world by giving the state ( current speed, current pedal rhythm, current gear ) and providing a change state. For example, if a bicycle has 6 gears, then the way to change the gear will be pushed when you pass in an illegal gear value ( less than 1 or ).

Binding code ( state, behavior ) to a object has many benefits:
1, modular, and object source code, maintenance independent, independent. Once created, passing objects in the system will be easy.
2. Information hiding. Communication by means of a method that can hide internal implementatio & of an external world.
3. Code reuse. Existing objects can be used in your own program.
4,, and easy to debug. If there's a problem with an object in the system, replace it with a good object, it's similar to the actual world 's mechanical repair, such as a tire blast, just a.

What's a class?

In the real world, you often see a lot of personalized objects. For example, hundreds of bikes are made through a model that leads to the same model. In terminology, we say a bicycle is an instance of a certain class of objects. The class is a blueprint for creating an object.

The following bicycle classes are the implementation of the bicycle:





class Bicycle {



 int cadence = 0;


 int speed = 0;


 int gear = 1;



 void changeCadence(int newValue) {


 cadence = newValue;


 }



 void changeGear(int newValue) {


 gear = newValue;


 }



 void speedUp(int increment) {


 speed = speed + increment; 


 }



 void applyBrakes(int decrement) {


 speed = speed - decrement;


 }



 void printStates() {


 System.out.println("cadence:" +


 cadence + " speed:" + 


 speed + " gear:" + gear);


 }


}






Java syntax is very unfamiliar to you, but the design of bicycle classes is based on the previous bicycle objects. Fields cadence, speed, and gear represent the state of the object, method ( changecadence, changegear, speedup, etc. ) defines the way to interact with the outside world.

Perhaps you've found that the bicycle class doesn't contain an entry function ( main ). Because this isn't a complete program, this is just a blueprint that may be used by a system. The responsibility of creating and using bicycle objects belongs to the responsibility of other classes in the program.

Examples of the following bicycledemo classes create two bicycle objects and execute their respective methods:


class BicycleDemo {


 public static void main(String[] args) {



//Create two different 


//Bicycle objects


 Bicycle bike1 = new Bicycle();


 Bicycle bike2 = new Bicycle();



//Invoke methods on 


//those objects


 bike1.changeCadence(50);


 bike1.speedUp(10);


 bike1.changeGear(2);


 bike1.printStates();



 bike2.changeCadence(50);


 bike2.speedUp(10);


 bike2.changeGear(2);


 bike2.changeCadence(40);


 bike2.speedUp(10);


 bike2.changeGear(3);


 bike2.printStates();


 }


}









Two bicycle objects pedal cadence, speed, and gear


cadence:50 speed:10 gear:2


cadence:40 speed:20 gear:3






What's inherited.

Different objects have a common thing. Mountain bicycle, highway bicycle and double bicycle have bicycle features ( current speed, current pedal cadence, current gear ). Each bicycle can define additional features to distinguish from other bikes: A double bicycle has two seats and two handles; The bike bicycle has a. Some mountain bikes have extra links, etc.

programming allows classes to inherit common states and behaviors from other classes. In this example, bicycle is the base class for MountainBike, roadbike, and tandembike ( parent ). In the java programming language, each class allows you to have a direct pare not class, each of which allows you to have any subclass ( derived classes ).
自行车的层次结构

The syntax for creating subclasses is very simple, using the keyword in the beginning of the class declaration to extends, and then is the name of the class that's inherited:


class MountainBike extends Bicycle {



 //new fields and methods defining 


 //a mountain bike would go here



}






The state and behavior of MountainBike and bicycle, however, allow its code to focus on the unique features of it. Inheritance makes it easy for subclasses to read, but to add a suitable document annotation to the pare not class, because the code for the superclass doesn't appear in the subclass code.

What's an interface?

As you've learned before, objects define their interactions with external interactions through exposure methods. Method to form ( form ) and the interface between objects and outside objects. For example, the * tv button in front of you is an interface to the wires on the other side of the plastic shell. You press the button to turn the tv on or off.

The most common form of interfaces is a set of methods that are empty. A bicycle method, if specified as an interface, looks like the following


interface Bicycle {



 //wheel revolutions per minute


 void changeCadence(int newValue);



 void changeGear(int newValue);



 void speedUp(int increment);



 void applyBrakes(int decrement);


}






In order to implement these interfaces, the name of the class changes ( for example, a bicycle for a brand, such as ACMEBicycle ), and the keyword _ implementatio & is used when declaring the class:


class ACMEBicycle implements Bicycle {



 int cadence = 0;


 int speed = 0;


 int gear = 1;



 //The compiler will now require that methods


 //changeCadence, changeGear, speedUp, and applyBrakes


 //all be implemented. Compilation will fail if those


 //methods are missing from this class.



 void changeCadence(int newValue) {


 cadence = newValue;


 }



 void changeGear(int newValue) {


 gear = newValue;


 }



 void speedUp(int increment) {


 speed = speed + increment; 


 }



 void applyBrakes(int decrement) {


 speed = speed - decrement;


 }



 void printStates() {


 System.out.println("cadence:" +


 cadence + " speed:" + 


 speed + " gear:" + gear);


 }


}






Implementing an interface allows a class to become more formal ( more foraml ) for its behavior. The interface forms the terms ( contracts ) of the classes and external worlds, which are executed by the compiler at compile time. If your class implements an interface, all methods that implement the interface must appear in the source code of the class, otherwise the compilation isn't.

In order to successfully compile the ACMEBicycle class, you've to add the _ keyword to all implementation methods; otherwise, the"class ACMEBicycle"is displayed as a, because it tries to give a more weak permission, the pare not class is public.

English hint: 'changecadence ( ) 'in ACMEBicycle clashes with 'changecadence ( ) 'in bicycle; attempting assign weaker access privileges ( 'packagelocal '); was 'public '.

Description: attribute methods declared in an interface can only be modified with public or without access modifier. No access modifier modifiers are also public, meaning that members in the interface are always public.

What's a package?

The package organizes a set of related classes or interfaces. Concept you can think of a package and a folder on a computer. You may store html pages in a folder, and another folder holds images, scripts, or programs in another folder. Because software written in a java programming language might consist of hundreds of classes, it makes sense to make files organized by putting together related classes.

The java platform provides a huge number of libraries that are suitable for use in your program ( set of packages ). This library is known as the application programming interface ( application programming interface ) which is known as api. In programming purposes ( purpose ), the package is generally reflected in its mission. For example, string objects contain the status and behavior of strings; In the file system, file objects can simplify the creation, deletion, contrast, and modification of the file operations. Socket object allows operation and creation of network sockets; A ugi object controls the gui component, such as a button, check box. Apis have many classes to choose from, allowing programmers to focus only on the application 's requirements design rather than enabling the application.

A java platform api specification contains complete interface, class, field, method list. These will be one of the most important references.




Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs