Thursday, September 6 2001 2:30 PM
The last time I learned a programming language to really be object-oriented, it should support information hidden / encapsulation, polymorphism, inheritance, and dynamic binding. In addition, we I know that Java fully supports these functions, and knows that Java is an interpretative language and runs inside the virtual machine, so any programs written by Java can be on any operating system that supports Java Virtual Machine (JVM). Operation. We also understand that the object is a software-programming model representing things in real life - programming models and objects are defined by their status and behavior. Finally, we know that everything other than the original data object is object. Because of this Many of the contents of the program design style are related to the objects and classes. We will further examine them in the following. The object of the object uses the object of the object is how to determine them when you browse the system analysis document or design documentation. Because objects generally represent people, local or things, a basic way to determine the object is to find the nouns used in the sentence. There is a simple example. In the sentence "a customer can have more than one bank account", We have identified two objects, customers and accounts. In the sentence "Kitten 喵 喵", we can determine an object, a cat. In front of the class, we have learned a class is how the object is defined and the object What entities should be included when creating or instantiation. In the discussion of animals, we can say, "Dogs Wang Wang, the cat is called, the duck is called." Determine the object in the sentence we got it. Dogs, cats and ducks. As for Wang Wang, 喵, 嘎 叫 嘎,, it is the behavior movement of our object. To implement these objects, we need to create three objects called Dog, Cat and Duck. To implement them Behavior, we can create a method of sound emitted by each object, and we call this method Speak or if we play the imagination, you can call this method as Sayello. In the context of the program, in order to demonstrate These concepts, let us modify the HelloWorld program in the article, add these three new objects and add a SayHello method to each of them, as shown below: public class helloworld {public static void main (string [] args) { Dog animal1 = new DOG (); CAT Animal2 = new cat (); duck arnimal3 = new duck (); Animal1.SAYHELLO (); Animal2. Sayello (); Animal3.SAYHELLO ();}} class dog {public void sayhello () {system.out.println ("bar");}} Class Cat {PUBLIC VOID SAYHELLO () {system.out.println ("Meow ");}} Class Duck {PUBLIC VOID SAYHELLO () {system.out.println (" quack ");}} After compiling and running this program, the output should be as follows: Bark MeionQuack looks at our program, we will immediately Noted some things: Each object represents an animal, and each object has realized an identical method, Sayhello. Suppose we want to give more functions and methods and properties used to represent objects referred to. For example, we can add a method to determine whether an animal is not breastfeeding, or we add a method to determine that one animal is not a meat. We can add these two ways to each object or us It is also possible to use OOP's two most powerful features: inheritance and polymorphism. Because all objects represent an object, we will create a "base class" or "super class"
The class, its name is Animal. We can then let our objects inherit the same feature from the Animal class and force each object to implement different features with the Animal class. Java specifies a class from another inheritance with an Extends keyword. Let us use inheritance and polymorphic concepts to get the benefits of code reuse to rebuild our procedures and make each object differently different features: public class helloworld {public static void main (string [] args) {dog Animal1 = new DOG (); CAT Animal2 = new cat (); duck arnimal3 = new duck (); system.out.println ("a dog say" animal1.gethello () ", IS carnivous:" Animal1. ISCarnivorous () ", IS A Mammal:" Animal1.isamammmal (); System.out.Println ("a cat sign" animal2.gethello () ", IS Carnivors:" Animal2.iscarnivors () ", is a mammal:" Animal2.isamammal ()); System.out.Println ("a Duck Says" Animal3.GetHello () ", IS Carnivors:" Animal3.iscarnivors () ", IS A mammal: " animal3.isAMammal ());}} abstract class Animal {public boolean isAMammal () {return (true);} public boolean isCarnivorous () {return (true);} abstract public String getHello ();} class Dog extends Animal {public String getHello () {return ( "Bark");}} class Cat extends Animal {public String getHello () {return ( "Meow");}} class Duck extends Animal {public boolean isAMammal () {Return (false);} public boolean iscarnivorous () {return (false);} public string getHello () {Return ("quack");