Java-object-oriented (1), java object-oriented

Source: Internet
Author: User

Java-object-oriented (1), java object-oriented

When it comes to object-oriented, we should say the core mechanisms of it: inheritance, rewriting, polymorphism, abstraction, and interfaces. They are the core of determining whether object-oriented programming is used. Next, let's take a few examples to see their evolution and their relationships.

Inheritance: in simple terms, subclass automatically owns all the methods and attributes of the parent class.

For example, both dogs and cats inherit the call of animals. Subclass inherits the parent class.

Class Animal {private String name; public void setName (String name) {this. name = name;} public String getName () {return name;} public void enjoy () {// animal call... System. out. println ("Cry...") ;}} class Cat extends Animal {private String eyesColor; // method unique to the Cat. Public String geteyesColor () {return eyesColor;} public void seteyesColor (String eyesColor) {this. eyesColor = eyesColor;} class Dog extends Animal {private String eyesColor; // method unique to the Dog. Public String geteyesColor () {return eyesColor;} public void seteyesColor (String eyesColor) {this. eyesColor = eyesColor;} public class Test {public static void main (String args []) {Cat c = new Cat (); Dog d = new Dog (); c. seteyesColor ("cat is blue eyes"); // implements its own unique method. D. seteyesColor ("the dog is black eyes"); c. enjoy (); // It also inherits the method of the parent class. D. enjoy (); System. out. println (c. geteyesColor (); System. out. println (d. geteyesColor ());}}
Result:

 

Explanation: Dogs and Cats have both their own unique attributes and methods, and all have the methods and attributes of animals (parent class. This is the feature of inheritance.

However, the obvious drawback is that both dogs and cats have their own unique voices, but they all have the popular voice of animals. How can we change it? We use our rewrite.

Rewrite: You can override the methods inherited from the base class as needed in the subclass. Let's take a look at the rewrite below.

Class Animal {private String name; public void setName (String name) {this. name = name;} public String getName () {return name;} public void enjoy () {// animal call... System. out. println ("cry ...... ") ;}} class Cat extends Animal {private String eyesColor; public String geteyesColor () {return eyesColor;} public void seteyesColor (String eyesColor) {this. eyesColor = eyesColor;} public void enjoy () {// rewrite the parent method. System. out. println ("cat call ...... ") ;}} class Dog extends Animal {private String eyesColor; public String geteyesColor () {return eyesColor;} public void seteyesColor (String eyesColor) {this. eyesColor = eyesColor;} public void enjoy () {// rewrite the parent method. System. out. println ("dog call ...... ") ;}} public class Test {public static void main (String args []) {Cat c = new Cat (); Dog d = new Dog (); c. seteyesColor ("cat is blue eyes"); d. seteyesColor ("the dog is black eyes"); c. enjoy (); d. enjoy (); System. out. println (c. geteyesColor (); System. out. println (d. geteyesColor ());}}
Result:

Explanation: both dogs and cats rewrite the animal's Enjoy () method, so they achieve their own unique call. Increased flexibility.

It can be seen from the above that rewriting evolved from inheritance, so inheritance was first followed by rewriting.

PS: the remaining three object-oriented features are described in the next blog.


What Is Object-Oriented in Java?

The target object is:
Put data and data operation methods together as an interdependent whole-object. Abstract The commonalities of similar objects to form classes. Most of the data in the class can only be processed using the method of this class. A simple external interface is used to communicate with an object through a message. The procedure is determined by the user in use.
Objects are an abstract concept of various objects. People need to access various objects every day, such as mobile phones.
In object-oriented programming, objects have multiple features, such as the height, width, thickness, color, and weight of mobile phones. These features are called object attributes. Objects also have many functions, such as listening to music, making phone calls, sending messages, watching movies, etc. These functions are called object methods, which are actually a function. The objects are not isolated, but have parent-child relationships. For example, mobile phones belong to electronic products and electronic products belong to objects. Such parent-child relationships are called the inheritance of objects. In the compilation process, all problems are taken as objects and solved in the way of human thinking. This method is very user-friendly. objects are actually a set of data, and the data has been named. The data is the property of the object and can be accessed by the program. Objects also include many functions. These functions are called object methods and can be accessed by programs. However, attributes or methods in the external access object must first reference the object, and then access the attributes and methods of the object with the point number.

The process orientation is:
The program structure is divided into several basic modules by function. These modules form a tree structure. The relationship between modules is as simple as possible, the functions are relatively independent. Each module is composed of three basic structures: sequence, selection, and loop. The specific method of modularization is to use subprograms. The program process is determined when writing the program.

Use the java object-oriented method to describe yourself and the specific code.

For example, if you are Li Gang and add a shouting method, there is no other method in the. main method.
Class Person {
Int age // age
String name // name
String gender // gender

Public person (){}
Public person (String name1 ){
Name = name1;
}
Public int getAge (){
Return age;
}
Public void setAge (int age ){
This. age = age;
}
Public String getName (){
Return name;
}
Public void setName (String name ){
This. name = name;
}
Public String getGender (){
Return gender;
}
Public void setGender (String gender ){
This. gender = gender;
}
// Call Method
Public say (){
System. out. println ("hello ");
}
}
Public class YourSelf {
Public static void main (String args []) {
Person you = new Person ("Li Gang ");
You. say ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.