As long as it is an object-oriented programming language, it basically has classesClass
The usage is just whether it is easy to use or not, it is easy to remember. It is object-orientedc++
It started to introduce, butc++
Things about classes are too complicated. In fact, in real projects, very complex concepts are rarely used.
Kotlin
It is a language that pursues simplicity, and has put a lot of effort into the category and given up a lot of it.c++
The concept of class is very complex. In fact, classes can be understood in this way. In order to facilitate and complete reuse, we combine variables and functions to form the concept of class.
Initialization and use of classes
Kotlin
The definition of the class in the class is also very simple, and there are not many concepts to explain. For more definitions of the class, you can actually refer to functions and variables.
class AutoMobile(name:String) { init { println(name) } fun Driver(){ println("Forward drive") } }
We useclass
Define aAutoMobile
class, this class can accept a parametername
, when the class is initialized, it is frominit
Started.
Comparison withc++
The language ofKotlin
It is very simple to complete a class, and it does not need to understand it for programmers who need to use the class simply.c++
The constructor and destructor of the class can be written.
We can use him like this.
var auto:AutoMobile = AutoMobile("public") ()
Like calling a function, we initialize itauto:AutoMobile
, brought onename
parameters. Maybec++
If you have too many concerns, the usage of the class has become very complicated. No need herepublic
Because the declaration of , is public by default , when writing a project , most of the cases are public unless there are some special functions or variables that are not known .
I have always thought that attribute acquisition methods and settings methods are a very strange way of programming, every time I seec#
orjava
In the code, a lot ofgetxxxx(),setxxxx()
, I don't think it's necessary. This kind of programming method will only exhaust the programmer's efforts and will not be able to replace the readability and stability of the program.
inherit
It is because of the inheritance that so many design patterns have emerged. It can also be said that it reduces the complexity of programming. But class inheritance is actually not that simple if it is useful.
We defined aCar
The class inheritedAutoMobile
class Car(name:String):AutoMobile(name) { init { println(name) } }
The inherited one also inherits his parameters. This is indeed interesting and you can write a lot of code less. AutoMobile represents the base class, and a keyword open` needs to be declared in the previous section.
certainlyKotlin
Abstract class, keywordabstract
and interface keywords areinterface
, I won't discuss it in detail here.
summary
An object-oriented programming language means that you have objects, and the organization of large projects is becoming easier and simpler. Using good classes can design a good code framework. Of course, the use of classes looks simple, but it is really necessary to do a good job. It is not that simple to do a good job.
This is the article about the initialization and use of Kotlin class object class. For more related Kotlin class object class content, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!