Javascript is based on the three major characteristics of objects (encapsulation, inheritance, polymorphism)_javascript skills

The three major object-based characteristics of Javascript are the same as the three major object-oriented characteristics of C++ and Java, which are encapsulation, inheritance and polymorphism. It’s just that the implementation methods are different, but the basic concept is almost the same. In fact, in addition to the three major characteristics, there is another common characteristic called abstract, which is why we sometimes see the four major characteristics of object-oriented in some books. 1. Encapsulation Encapsulation is to encapsulate abstracted data and operations on the data together. The data is protected internally, and other parts of the program can only operate on the data through authorized operations (member methods). Case: PS: JS encapsulation has only two states, one is public and the other is private. The difference between adding member methods through constructors and adding member methods through prototype methods 1. Functions allocated through the prototype method are shared by all objects. 2. The attributes assigned through the prototype method are independent. (If you do not modify the attributes, they are shared) 3. It is recommended that if we want all objects to use the same function, it is best to use the prototype method to add the function, which…

Detailed explanation of encapsulation, inheritance, and polymorphism in Java

Encapsulation In the article How to Understand Object Orientation, it is mentioned that the so-called encapsulation means “the functions are all done for you, you don’t have to understand how it is written, you can just use it directly.”. But you have to be clear, that is, this sentence is relative to the user, and as developers, we have to do the encapsulation ourselves. So as developers, how should we encapsulate it? In fact, you should ask, in turn, how they should use it. It will be much simpler if you think about it this way. As a user, you naturally hope that the simpler the better. In other words, we should not let users operate some complicated things. That means we should seal complex and unnecessary parameters so that users cannot operate them. Why not let users do it? Because users are often not very professional, if too many interfaces are exposed to them, some weird problems are likely to occur. For example, if a person does not know how to cook boiled fish, if he is asked to do it, he will definitely It’s not good, so what should I do? Buy him a pack of boiled fish…

Java’s three major features: encapsulation, inheritance, and polymorphism

We know that the three major characteristics of object-oriented are encapsulation, inheritance and polymorphism. However, sometimes we are always confused about these concepts. These concepts are organized below to lay a solid foundation for future abstraction-oriented programming.  The concept of encapsulation is still easy to understand. If you can define classes, then I believe you have fully grasped the concept of encapsulation. The classes defined below are the encapsulation of data.  The advantage of inheritance is code reuse. Inherited subclasses automatically have all properties and methods in the parent class. So inheriting existing classes means reusing the methods and fields of these classes. On this basis, subclasses can also add some new methods and fields to meet new needs. This is a core technology in Java programming. So how to judge whether inheritance is needed? The “is-a” relationship is an obvious feature of inheritance. The meaning of this sentence can be interpreted as: The reason why Student inherits Person is because Student is Person. If there was no such relationship, there would be no need for inheritance. This is also the definition of the Liskov Substitution Principle, subtypes must be able to replace their parent types. When defining a subclass…

The underlying implementation of Java’s basic object-oriented mechanism (polymorphism, inheritance)

1. The past life of Java Why was Java created? What are the characteristics of Java? Starting with C language, C language is a structured language with modular programming, which facilitates program debugging. It relies on very comprehensive operators and diverse data types to easily complete the construction of various data structures. The Pointer type can also directly address memory and perform direct operations on hardware, so it can be used to develop both system programs and application software. Its disadvantage is that the encapsulation is weak and the security of the program is not very good. Exception handling in C language generally uses setjmp() and longjmp() to jump when an exception is caught; or use the abort() and exit() functions to forcibly terminate the running of the program. If you want to implement multi-threading, you should directly operate the underlying operating system. The language itself does not encapsulate this mechanism. C language is a process-oriented language. The so-called process-oriented refers to the programming idea centered on the “event process”, that is, according to the steps of solving the event, it is implemented step by step in the function. In fact, most things in life can be solved with process-oriented…

How to calculate the perimeter and area of ​​triangles and rectangles in Java using interfaces, polymorphism, inheritance, and classes

The examples in this article describe how Java uses interfaces, polymorphism, inheritance, and classes to calculate the perimeter and area of ​​triangles and rectangles. Share it with everyone for your reference. The details are as follows: Define interface specification: /** * @author vvv * @date 2013-8-10 08:56:48 am */ package com.duotai; /** * * */ public interface Shape { public double area(); public double longer(); } /** * @author vvv * @date 2013-8-10 09:10:06 am */ package com.duotai; /** * * */ public class Triangle implements Shape { double s1; double s2; double s3; //Initialize a triangle object and give the triangle three side lengths public Triangle(double s1, double s2, double s3) { if (isTri(s1, s2, s3)) { this.s1 = s1; this.s2 = s2; this.s3 = s3; } else { System.out.println(“Input three side lengths” + s1 + “, ” + s2 + “, ” + s3 + “Cannot form a triangle, please re-enter the three side lengths!”); } } // Determine whether it is a triangle public boolean isTri(double s1, double s2, double s3) { if (s1 + s2 <s3) { return false; } if (s1 + s3 <s2) { return false; } if (s2 + s3 <s1) { return…

Detailed explanation of the organizational structure, inheritance, and implementation relationships of Java collection classes

Collection inheritance and implementation relationships are as follows (Note: (I) represents interface, (C) represents Java Class, <– indicates inheritance, <<—— indicates implementation): (I)Iterable |<– (I)Collection                                                                                                                                                                                                                                                                                                                                                                                                                                                     …

[Mechanism] JavaScript prototype, prototype chain, inheritance

1. The concept of prototype and prototype chain When js creates an object, such as calling it obj, They will secretly add a reference to him. This reference points to an object, such as yuanxing. This object can provide attribute sharing for the object that refers to it. , for example: yuanxing has an attribute name, which can be accessed by obj.name. This can provide an object for attribute sharing. , is called the prototype of the previous object And the prototype itself is also an object, so it will also have its own prototype. This continues layer by layer until it finally points to null, which forms Prototype chain How is the prototype mechanism of js implemented? 2. Prototype implementation Let’s look at an example first: // code-01let obj = new Object({name:’xiaomin’})console.log(obj.name)console.log(obj.toString())// xiaomin // [object Object] We first create an object obj, which has an attribute name The attribute name was created by us, but when creating obj, the toString attribute was not created for it. Why can obj.toString() be accessed? prototype attribute Let’s first take a look at the Object.prototype attribute We found that there is toString here attributes, so in fact Object.prototype is the prototype of obj.…

Java language encapsulation, inheritance, abstraction, polymorphism, interface

Table of contents Foreword 1. Encapsulation 1.1 Definition of encapsulation 1.2 Access modification Use of symbols 2. Inheritance 2.1 Definition of inheritance 2.2 Methods of inheritance 2.3 Points to note when using inheritance 3. Polymorphism 3.1 Definition of polymorphism 3.2 Dynamic binding 3.3 Method rewriting 3.4 Upward (Downward)Transformation 4. Abstraction 4.1 Overview and definition of abstraction 4.2 Use of abstraction p> 5. Interface 5.1 Meaning of interface 5.2 Definition of interface Summary 😽Personal Homepage: tq02’s Blog_CSDN Blog-C Language, Java Blogger 🌈 Ideal goal: study hard and move towards Java There are regrets. 🎁Welcome → Like👍 + Collection⭐ + Comment📝+Follow✨ Contents of this chapter&# xff1a;Java encapsulation, inheritance, abstraction, polymorphism, interface Use compiler :IDEA Preface Before talking about encapsulation, inheritance, abstraction, polymorphism, and interfaces, we must first understand what classes and objects are. Detailed explanation of classes and objects:tq02’s explanation of classes and objects 1 .Package 1.1 Definition of package         Encapsulation ,Operation in the class,Encapsulate the member variables and member methods of the class,Meaning: Can be thought of as a protective barrier,preventing the code and data of this class from being randomly accessed by code defined by external classes. To encapsulate the class we need to use access modifiers, which are…

JavaScript functions, object creation, encapsulation, properties and methods, inheritance

First, function I feel so flexible from the beginning when I came into contact with js. Everyone’s way of writing is different. For example, there are N ways of writing a functionFor example: function showMsg(){}, var showMsg=function( ){}, showMsg=function(){} There seems to be no difference. They are all the same. Are they really the same? Take a look at the following example The code is as follows: ///——————————————— ————————————————– — ——- //Function definition: named function (declarative), anonymous function (reference) //Declarative, the definition code precedes the function The execution code is parsedfunction t1(){ dwn(“t1”); } t1(); function t1(){ dwn(“new t1” ); } t1(); //Reference type, dynamically parsed during function operationvar t1=function(){ dwn(“new new t1”); } t1(); var t1=function(){ dwn(“new new new t1”); } t1(); // The above output: new t1, new t1, new new t1, new new new t1 You may think that the output should be t1, new t1, new newt1, new new new t1, but the result is not like this, you should understand This sentence: Declaration The definition code is parsed before the function execution codeIf you go one step further, it should be said that it is a scope chain problem. In fact, the first two methods…

JavaScript is object-oriented, implementing namespace, class, inheritance, and overloading

Since most of the client-side work such as Javascript and CSS in the group was handled by another colleague, and that colleague was too busy to refactor, the boss only made suggestions and did not immediately implement the refactoring. But I also fixed some small bugs in the client a few days ago. The code is indeed a bit confusing. I don’t know where I am, and I don’t dare to touch the code easily, so I tinkered with it myself. I once loved and hated it. Javascript, write a simple js to implement namespace, inheritance, overloading and other object-oriented features. Welcome to contribute . Define namespace Namesapce.js The code is as follows: Namespace = new Object(); Namespace.register = function(fullname){ try { var nsArray = fullname.split(” .”); var strNS = “”; var strEval = “”; for(var i=0;i<nsArray.length;i++){ if(strNS.length > 0) strNS += “.”; strNS += nsArray[i]; strEval += ” if(typeof(“+ strNS +”) ==’undefined’) ” + strNS + ” = new Object(); “; } if(strEval != “”) eval(strEval); }catch(e){alert(e.message);} } .Employee.js Employee.js The code is as follows: //Register namespaceNamespace.register (“MyCompany”); //1. Class: EmployeeMyCompany.Employee = function(empName){ this.Name = empName; this.Salary = 1000; this .Position = “cleaner”; } MyCompany.Employee.prototype.ShowName = function(){ return “I’m…

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索