Kotlin object-oriented self-diary

Object-Oriented

- Object
- Class
- Inheritance
- package
- polymorphism

Meaning object refers to one specific thing, that is, in real life can be tangible things. In object-oriented programming, an object is referred to a component of a computer system. In object-oriented programming, object contains two meanings, one of which is data, the other is action. And the operation of the object data is a combination thereof. Not only the object can be operated, while also recording the result of the operation in time.

Objects

First, here referenced in object-oriented interpretation of the object. Object actually refers to specific things in reality , each thing has its corresponding specific properties when categorize these properties. And data can be attributed to the action (method) , for example to take television: the basic, the inherent properties of length, width, thickness, material, and so the interface that the data will have the attributes of the television set, the television; operation ( the method) includes: the TV, on-demand, change the channel, and so on provided state change.

= + Data Object Method

It also refers to the object-oriented data and methods considered as a whole, its abstract modeling, run closer to the natural law of things. At this point you can easily define one thing. That can easily be modeled on a television through modeling of the data:

class

At this time, the introduction of class concepts :

Abstract object class is identical characteristics (data elements) and behavior (function). Thus, the object is an abstract class, object class is concrete, it can be said that an object instance of the class, the class is actually a data type. Class has attributes which are abstract state object data structure to describe the properties of the class.

The object is abstract class

Abstract means: the extraction of specific things, summed up their properties, characteristics and relationships ...
abstract objects and features of the property that is a thing of the relational approach and data extraction, etc., and then use the modeling approach to generalize. After summarized collectively as a class. Class can be understood as a template , the template can be built through a lot of things that have the same properties and methods.

Specific class is the object

The data and methods of specific template, an object can be obtained, and the method of data changes will be different objects, such as (defined inaccurate Please forgive):

class TV(var height:Int,var width:Int,var material:String,var land:Int,var brand:String)//电视类:包含属性  长宽厚度材质品牌

If I to this TV imparting length of 100 cm width 60 cm thickness of 50 cm plastic Panasonic brand attribute then it is Panasonic a bulk television;
if I to the television imparting 100 cm 60 cm 2 cm composite Haier brand attribute, then it Haier brand is the ultra-thin TV
they are TV
but because the parameters are different properties and different, which is embodied as a TV of each type of TV

A class is a data type

Class is actually a complex data types
Here we define a common data types:

var name:String="张三"

This is a String data type, define a name the name variable is the equivalent of a class, which is a template , we assign different names for objects generated is not the same. Joe Smith John Doe objects can be objects, but they are a name.
Back to Top TV:
In fact, the class is a more complex data types of simple data types . Television class includes several variables, depending on the assignment to produce different objects.

So understand the relationship between classes and objects, can be more profound understanding of how to abstract an object. It will be able to more accurate and detailed description of a thing.

inherit

Inherited popular understanding is that (continue to use the TV for example child): I have a TV class, now I can define what brand of what kind of TV, but I now have a new demand, I want to know the TV is LED or VA the refresh rate is how much, can not be networked. At this point, if I went to redefine a television class would be more harm than good. Time-consuming and laborious, at this time I can be like the TV as a base class, that is, has the basic attributes of the class, on this basis, with a special attribute to be a new class, which is derived class.
Inheritance is a general to the particular process

Inherit it simply is a hierarchical model, the hierarchical model can be reused. The upper hierarchy having versatility, but having the particularity of the underlying structure. Class can inherit the methods and variables from the topmost portion of the process succession. In addition to inheriting class may also be possible to modify or add. In this way it can effectively improve work efficiency.

Package

Popular understanding package (calculator): when we use the calculator, the input data a 1 + 1 = 2, or Lagrange, open square, square or, we are a data input, and then to the calculator we have a result, we can not see inside the calculator is how to calculate the data you enter, but directly get the results.

Package, i.e., the hidden object properties and implementation details, only open to the public interface to control read and modify the program attribute access level; abstract data obtained and acts (or function) combine to form an organic whole, but also the data source is the data operation organically combined to form a "class", the data and functions which are members of the class.

class dog(var kind:String,var name:String,var age:Int,var character:String)
{
	//内部定义函数
}
fun main(args:Array<String>)
{
	var dog01=dog("拉布拉多","妞妞","3","开朗")
	println(dog01.kind)
}

At this point dog class is to be a good package, we call this class directly in the main function, dog01 is a good definition of the object, this time into a specific category called niu Labrador 3-year-old, cheerful Xiao Gouzai, we may know that dog class, but it can not know the internal data and functions is what, for example, we do not know which depending on the variety and age Gouzi it can be calculated probability of suffering from some invisible disease (I did not write, do not know how to write ha ha ha) we can draw a direct input probability. This class is to be packaged, you can call us directly.

Polymorphism

Polymorphism is actually different kinds of realization of a function : for example, the daily behavior: eating, some people like to eat with chopsticks, some people like to eat with a spoon. Their acts are eating, early in the process definition, anyone who does not stipulate how to eat, but in the course of operation, depending on the data, the resulting behavior is different. Eat with a spoon like Joe Smith, John Doe likes to use chopsticks, then the input data when Joe Smith, would like to get with a spoon, get input John Doe like to use chopsticks. The same kind of behavior, different implementations .

Published 15 original articles · won praise 3 · Views 346

Guess you like

Origin blog.csdn.net/Py_csdn_/article/details/104603679