.NET programming initiator is easy to make mistakes and easily confused concepts

zhaozj2021-02-16  80

Reprinted several articles today, this duality is most useful, I hope to be useful to you!?!! Post here! Reprinted from M $ Chinese Technology Station

First, the objects and classes that are unclear, even beginners also know the object-oriented noun. But many people don't know the difference between "objects" and "class". I don't want to explain the object of the object and the class, and these things can be found. If you carefully read a lot of IT books, you will find that the object (Object) is mixed with each other. For example, any object-oriented book that is introduced will tell you: the object is an instance of a class. The car is a class, and the Santana of your home is an example of the car class, so Santana is an object, and the car is a class. However, you will often find this statement such as car objects. It seems that the object has become a class? ? ? Distinguish this concept, you need to know, there are two meanings: narrow objects and generalized objects. It looks a bit complicated? But I promise that this is not the relativity of Einstein. Narrow objects refer to objects in object-oriented programming (OOP), narrow object definitions are: objects are instances of classes. Generalized objects refer to object theory and object-oriented object-oriented design (OOD) object concepts, and the generalized objects contain both of the instances of the class and classes, that is, the class can also be called a generalized object, and the instance of the class can also be called It is a generalized object. Thus, if a person or a book does not specify programming, it is generally, and the object refers to a broad-scale object. When a certain segment code is referred to, the object refers to a narcotic object. Oh, there is no way, this is the problem caused by history. Second, inheritably I have seen a person with ASP.NET, he is writing a C # code file (not an ASP.NET page), which writes a class, he wants to reference a variable in Application in this class. If you are you, how would you do it? He is doing this, such as this class called Test, the code is as follows: public class test: system.Web.ui.page {public vid mymethod () {Object L_Value; l_value = Application ["myvar"] ; ...} What do you think? I don't know how many people will think of this method to solve the problem. But he finally ran to find me to help, he said that this class was wrong. Say that he will inherit the Test class from system.web.ui.page because he can finally use Application in the code. But this application has not been working properly. The TEST class is not an ASP.NET page. Do not say technical problems, only an object is a wrong usage, there is no inheritance relationship between things that do not have successful relationships. If the Human class inherits from the Dog (Dog) class, I am afraid no one ends? Because there is no direct biological association between the dog and people. Finally, let's take the reason why the application cannot be used. All classes inherited from system.web.ui.page are ASP.NET pages, and each ASP.NET page must be initialized in the ASP.NET process, that is, the page must be inside ASP.NET. Perform initialization and create, use new to manually create, although you can use Application in the code, but this Application is not assigned, so it is NULL. Third, the role of the misunderstanding object method is inherited, so people do a lot, and those who don't correctly distinguish between the objects of the object. Even in some people with multi-year programming experience will also make this mistake.

In a system, I saw the following code written by others: // Product database Operation class PUBLIC CLASS ProductionDB {Public ProductionDB () {} // Save a product to the database, A_Object is a product object instance public Bool SaveProduction (ProductionObject) A_Object) {...} // Remove a product from the database, a_object is a product object instance public bool deleteProduction (ProductionObject a_object) {...} // New Product, and return to create new product object instance public productionObject PUBLIC ProductionObject NewProduction () {...} // returns the ID of the object product public ProductionObject GetProduction (string a_productionID) {...} // search item, the search and returns the results public ProductionObject [] SearchProduction (object [] a_conditions) {. ..}} I don't know if you are watching the test, I have a code like the above, I don't know what is the mood when you see the above code. Anyway, I have seen the end of this code. It is the back of the gas, and I have been unclear. How do you use this ProductionDB class to operate the product database table? To give an example: ProductionObject L_Prod; ... // Assume that l_prod has assigned the productionDB l_proddb = new productDb (); if (l_proddb.saveproduction (l_prod) == false) ... Else ... above is one Typical use examples, you see, if you want to save a product, you must first create a productionDB instance, New ProductionDB (). The problem is here, all the information of the SaveProduction method in the ProductionDB class all from the method parameter A_Object, which is the external L_Prod variable, and in addition to these 5 methods in the ProductionDB class, New ProductionDB ( )has no meaning? ? ? Why create a sampleDB instance? ? ? The writeer's misunderstanding is that there is no difference in the difference between the different methods of clarifying the instance method and the class method. Any information on the above, method code does not depend on class should declare the method as a class method, while the method relies on and using members, information, etc. in the class, requiring method to declare the method as an instance method.

转载请注明原文地址:https://www.9cbs.com/read-14051.html

New Post(0)