OOP: Understand class and objects (1)
[Author: Unknown Add Time: 2001-9-7 8:10:45]
Source: www.zdnet.com.cn
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 objects in Java is an object.
Because many of this programming style is related to objects and classes, we will further examine them.
Objectory
One key to use the object is how to determine them when you browse the system analysis document or design documentation. Because the object is generally representative, a place or matter, a basic method of determining the object is to identify the noun 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 one Object, cat.
Class
In front, we have learned a class that defines how objects are moved and what should be included in the object when the object is created or instantiated. In the discussion of animals, we can say, "Dog Wang called, cat Called, the duck is called. "Determine the object in the sentence we got dog, cat and duck. As for Wang Wang, 喵 喵, 嘎 嘎, it is the behavior of our object.
To implement these objects, we need to create three objects called DOG, CAT, and Duck. To implement their behavior, we can create a method of sounding a sound emitted by each object, and we call this method Speak Or, if we play the imagination, you can call this method as SayHello.
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 them, as shown below:
Public class helloworld
{
Public static void main (string [] args)
{
DOG Animal1 = New Dog ();
Cat Animal2 = New cat ();
Duck Animal3 = New Duck ();
Animal1.SAYHELLO ();
Animal2.SAYHELLO ();
Animal3.SAYHELLO ();
}
}
Class Dog
{
Public void sayhello ()
{
System.out.println ("Bark");
}
}
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
Meow
Quack
Take a look at our procedure, we will notice some things immediately: Each object represents an animal, and each object has achieved an identical method, SayHello. Suppose we want to give more functions and represent The method and attribute of the animal referred to. For example, we can add a method to determine that an animal is not breastfeeding, or we add a method to determine if a animal is not meat. We can pass each object Adding these two methods to implement or we can also use OOP's two most powerful features: inheritance and polymorphism.
Because all objects represent an object, we will create a class called "base class" or "super class", which is animal. We can then let our objects inherit the same feature from the Animal class And forget each object only to achieve different features of the Animal class. Java specifies a class from another inheritance with an Extends keyword. Let us use the inheritance and polymorphic concept to reuse the benefits of code reuse to rebuild our program and make each Objects only realize different features with base class animal:
Public class helloworld
{
Public static void main (string [] args)
{
DOG Animal1 = New Dog ();
Cat Animal2 = New cat ();
Duck Animal3 = New Duck ();
System.out.println ("A Dog Says" Animal1.GetHello ()
", IS carnivorous:" Animal1.iscarnivorous ()
", IS A Mammal:" Animal1.Isamammal ());
System.out.println ("a cat sign" animal2.gethello ()
", IS Carnivorous:" Animal2.iscarnivorous ()
", IS A Mammal:" Animal2.Isamammal ());
System.out.println ("a Duck Says" Animal3.GetHello ()
", IS carnivorous:" Animal3.iscarnivorous ()
", IS A Mammal:" Animal3.Isamammal ());
}
}
Abstract Class Animal
{
Public Boolean IsamammmMal ()
{
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