Dark Horse Programmer "object-oriented in Java"

Source: Internet
Author: User
Tags what inheritance

object-oriented in Java

in software development learning, my first contact with the development language is Java, but are simple functions and loop array of applications. Said object-oriented, the first time to see the word or in the C # learning process, I remember the PPT on the domineering explanation, what is the object? All things are objects! Enough domineering, then the object-oriented thought to me is still quite collapsed, what inheritance polymorphic Ah! After countless links to the project's writing, finally to the object-oriented understanding, and now just in the Java learning to revisit the object-oriented, then I see the object-oriented writing to share to everyone.

When it comes to object-oriented, we have to mention three of his features, inheriting encapsulation and polymorphism. Let's start with encapsulation, what is encapsulation? is to combine the properties and operations of an object into a single whole, and hide the internal implementation details. This statement is more official, simply to modify the attribute with private, and then establish a public method to assign a value to the property, and in the method to control the property assignment rules. Before the code to share a myeclipse package shortcut, write the private properties and press Alt+shift+s after the menu bar and then press R can be fully encapsulated.

 Public classStudent {PrivateString Stuname; Private intStuage; Private intStuscore;//cursor Positioning here Press the shortcut key     PublicStudent (String name,intAgeintscore) {         This. Stuname =name;  This. Stuage =Age ;  This. Stuscore =score; }     PublicString Getstuname () {returnStuname; }     Public voidsetstuname (String stuname) { This. Stuname =Stuname; }     Public intGetstuage () {returnStuage; }     Public voidSetstuage (intstuage) {         This. Stuage =Stuage; }     Public intGetstuscore () {returnStuscore; }     Public voidSetstuscore (intStuscore) {         This. Stuscore =Stuscore; }         Public voidsay () {System.out.println ( ThisStuname+ "is greeting everyone."); }}

The above is the encapsulation of an object, you can add the assignment requirement in the Set method. With the class encapsulated above, let's take a look at the next feature, which is inheritance. Inheritance in our daily life understanding is from our previous generation or the boss of the teacher to get his things, and generally is the whole thing, then the inheritance in Java what meaning? Inheritance is the derivation of new classes from existing classes, which can absorb the data and behavior of existing classes and extend new capabilities. The existing class is generally referred to as the parent or base class, and the new class is called a subclass or derived class. In the Java inheritance attribute, only single inheritance can be implemented, that is, all subclasses can have only one parent class, the subclass inherits the parent class The keyword is extends followed by the class name.

Here is a three-class, he inherits the student class, and overrides the Say method in the class:

 public  class  Stuzs "extends   Student { public  Stuzs ( String name, int  Age, int   score) { super   (name, age, score);  public  void   say () {System.out.println () "Hello everyone!"    "); }}

Here are a few points to note that a private property or method in a parent class cannot be inherited in a subclass, that is, when the Stuzs class cannot directly use the Stuname variable, you need to use the set (Assignment) get (take value) method in the class. If there is a constructor with a parameter in the parent class and no parameterless constructor, then after inheriting the parent class, the subclass must have the same constructor and the first statement in the function calls the constructor in the parent class, because when you instantiate the subclass object, Will go first to the constructor of the parent class and then back to the constructor of the child class. So if there is a parameter construct in our parent class, then the friendly writing is based on the following code, plus an argument-less constructor.

Now that we know that subclasses can use the parent class method, now the question is, what if there are properties or methods in the subclass that have the same name as the parent class? The name of the parent class in a positive subclass is called overwrite, which means that the method of overriding the parent class can be regarded as one of the two classes in the call, but it is not the same, it can be distinguished by this and the Super keyword in the subclass, and this is called the method of this class. Super calls the parent class method, and the properties are the same, the code is as follows:

 Public classStuzsextendsStudent { PublicStuzs (String name,intAgeintscore) {        Super(name, age, score); }     Public voidsay () {System.out.println ("Hello, everyone!" "); }    Public voidThisandsuper () { This. Say (); Super. Say (); }}
     Public Static void Main (string[] args) {        new Stuzs ("Zhang San");        Stu.thisandsuper ();    }

This code will output "Zhang San is greeting everyone" and "Hello everyone!" "Two sentences, so that we can distinguish in the subclass who this method or attribute belongs to." But what's the point of doing this, we're going to inherit the object that the subclass can use the properties or methods of the parent class, let them "equals", and the parent class we generally just create the method and the property, so the parent class say () The content of the method is superfluous, not used, then there is our abstract method. Abstract methods in Java are modified with the abstract keyword. Take a look at some of the features and considerations of the abstract method before you look at the specific code:

1. The class of abstract method must be abstract class, abstract class is also modified with abstract

2, the abstract class can have common methods and properties

3, abstract methods can not have method body

4. Subclasses must override the abstract method of the parent class, unless the subclass itself is an abstract class

5. Abstract classes cannot be instantiated

Okay, some basic considerations for abstract classes and abstract methods We also understand, then the following things are simple, we just need to modify the code a little bit OK:

 Public classStuzsextendsStudent { PublicStuzs (String name,intAgeintscore) {        Super(name, age, score); } @Override Public voidsay () {//TODO auto-generated Method Stub            }         Public voidThisandsuper () { This. Say (); Super. Say (); }}

This way we can call the parent method with super, we will find that the red line is reported, it is not allowed to be called.

Here is our play today, that is, polymorphism, the front of the package and inheritance is almost in preparation for polymorphism, then what is polymorphic? Let's start with an official explanation: polymorphism refers to allowing objects of different classes to respond to the same message, polymorphism including parameterization and inclusion polymorphism. Polymorphism language has the advantage of flexibility, abstraction, behavior sharing and code sharing, which solves the problem of application function with the same name. Polymorphism is so cattle, so many states have what good? Here's a list of the following:

1. Replaceable (substitutability). Polymorphism is replaceable for existing code. For example, polymorphism works on the Circle Circle class and works on any other circular geometry, such as a ring.
2. expandability (Extensibility). Polymorphism has extensibility for code. Adding new subclasses does not affect the polymorphism, inheritance, and the operation and manipulation of other attributes of existing classes. In fact, new subclasses are more likely to get polymorphic functions. For example, it is easy to add the polymorphism of sphere class on the basis of realizing the multi-state of cone, semi-cone and hemispherical body.
3. Interface (interface-ability). Polymorphism is a superclass that, by way of signature, provides a common interface to subclasses, which is implemented by subclasses to refine or overwrite the class. As shown in 8.3. The super-Class shape in the figure specifies two interface methods for implementing polymorphism, Computearea () and Computevolume (). Subclasses, such as circle and sphere, refine or overwrite both interface methods in order to achieve polymorphism.
4. Flexibility (flexibility). It embodies the flexible operation in the application, and improves the use efficiency.
5. Simplification (simplicity). Polymorphism simplifies the process of coding and modifying the application software, especially when dealing with the operation and operation of a large number of objects, which is particularly prominent and important.

See the polymorphism of so many benefits, is not feel very strong appearance, let us look at polymorphism in the code of the rendering Way (a piece of God around the code, we watch, at night):

 Public classStua { Public voidsay (StuD Stu) {System.out.println ("D say Hello"); }     Public voidsay (Stua Stu) {System.out.println ("A say Hello"); }} Public classStuBextendsStua { Public voidsay (Stua Stu) {System.out.println ("A say Hello"); }     Public voidsay (StuD Stu) {System.out.println ("B say Hello"); }} Public classStuDextendsStua { Public voidsay (Stua Stu) {System.out.println ("A say Hello"); }     Public voidsay (StuB Stu) {System.out.println ("B say Hello"); }} Public Static voidMain (string[] args) {Stua a=NewStua (); StuB b=NewStuB (); StuD D=NewStuD ();        A.say (b);        A.say (d);        B.say (a);        B.say (b);        D.say (d);            D.say (d); }

In this I also can not help everybody what, around out polymorphic you understand almost, run the result to everybody, I want to go to bed.

A say Hello
D say Hello
A say Hello
A say Hello
D say Hello
D say Hello

Dark Horse Programmer "object-oriented in Java"

Related Article

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.