Object Oriented (i)

Source: Internet
Author: User

Object-oriented definition: Construct the software system from the objective things in the real world, and use the human thinking mode as far as possible in the structure of the system.

A class is an abstract concept used to describe the same class object, and classes use properties and methods to make static and dynamic properties of a thing

A class can be seen as a template for a class of objects, and an object can be seen as a concrete instance of an object.

The definition of the car, with what characteristics is a car, can be manned (static properties), can run around---(Dynamic properties) car shape, construction, such as, steering wheel, door, glass, car color is the static properties of the car

Summary: The car has static and dynamic properties, in Java there are static members and dynamic members (methods) to drive the car, this is the car to the abstract, we call the car class, the book is a class, electronic products is also a class, we put a kind of things specific one thing, Something that conforms to the characteristics of such things is called an object.

eg. how do you abstract the class of employees? It is also from two aspects, on the one hand is its static property, on the other hand its dynamic property


What are the attributes of the staff? There are names, ages, current wage amounts and other attributes,

What methods does he have? Let the clerk show his name, show his age, change his name and get his salary. Of course display name, show age, change name, collect salary these can also let others to do,

But object-oriented design thinking is the most appropriate method that should come out of the most suitable class. Display the name, show age, change the name, pay the salary by who do more suitable, that is the staff of the most appropriate.

So these methods should appear in the staff category.

According to the method there is no way to differentiate between two objects. So each object has its own property, and the property value is not the same as another object.

Inheritance: xx is xx

Inheritance is single-inheritance

Inherit from the coverage difficulty

When the subclass needs to call the constructor of the parent class, be aware that

1) If there is no constructor method in the parent class or if there is an argument-free constructor, the subclass has no constructor method, and the subclass will display the constructor method of the parent class.

2) If the constructor method of the parent class has a parameter construct, then the sub-class point must explicitly call the constructed method of the parameter, otherwise the error will be

3) If the parent class has a constructor that has no arguments and no arguments, the subclass calls the no-argument-constructor method by default

Method overrides:

1, subclasses overriding the parent class must have the same parameter return type

2, the parameter return class can be a subclass of the parent class that returns the class

3, subclasses can only enlarge the scope of the modifier, not shrink

4, Inheritance has polymorphic

Polymorphic:

. 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.

Application of polymorphism

This.show (o), Super.show (O), This.show ((Super) O), Super.show ((Super) O).

Class A {

Public String Show (D obj) ... {

Return ("A and D");

}

Public String Show (A obj) ... {

Return ("A and a");

}

}

Class B extends a{

Public String Show (B obj) ... {

Return ("B and B");

}

Public String Show (A obj) ... {

Return ("B and A");

}

}

Class C extends B ... {}

Class D extends B ... {}

A a1 = new A ();

A A2 = new B ();

b b = new B ();

c C = new C ();

D d = new D ();

System.out.println (A1.show (b)); ①

System.out.println (A1.show (c)); Ii

System.out.println (A1.show (d)); ③

System.out.println (A2.show (b)); ④

System.out.println (A2.show (c)); ⑤

System.out.println (A2.show (d)); ⑥

System.out.println (B.show (b)); ⑦

System.out.println (B.show (c)); ⑧

System.out.println (B.show (d)); ⑨


When a Superclass object reference variable refers to a subclass object, the type of the referenced object rather than the type of the reference variable determines which member method to call

, but the method that is called must be defined in the superclass, which means the quilt class overrides the method.

For example ④,a2.show (b), A2 is a reference variable, type A, then this is A2,b is an instance of B, so it went to class A inside the show (b obj) method, not found,

So to a super (super Class) to find, and a no superclass, so go to the third priority This.show (Super) O),

This is still a2, where O is B, (super) o i.e. (super) B is A, so it is in class A to find the method of show (a obj), Class A has this method,

But since A2 refers to an object of Class B, B overrides the show (a obj) method of a, so it eventually locks to show (a obj) of Class B, and the output is "B and A".

Http://www.jb51.net/article/34413.htm



This article is from the "Banyan OPS rules" blog, please be sure to keep this source http://10879428.blog.51cto.com/10869428/1788481

Object Oriented (i)

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.