Kotlin difference between self-diary interfaces and abstract classes (on)

  • Interface Interface
  • Abstract class abstract

How to distinguish between interfaces and abstract classes?
Transfer from the following: HTTPS: //www.cnblogs.com/yongjiapei/p/5494894.html
. 1, abstract classes and interfaces can not be instantiated directly, if instantiated, abstract class variables must implement all abstract methods directed subclass object interface variable must point to implement all interface methods of the class object.
2, abstract class to be inherited by subclasses, the interface class to be realized.
3, the interface can only be done by method claims, the abstract class can declare methods do, you can do way to achieve
4, variables defined in the interface can only be public static constants, the abstract class variables are common variables.
5, the abstract class abstract method must be implemented by all the quilt class, subclass if the parent class abstract methods can not achieve full, then the subclass can only be an abstract class. Similarly, a time when implementation of the interface, if not all implement an interface method, the class can only be abstract.
6, abstract method can only affirm that can not be achieved, the interface is by design, abstract class is the result of the reconstruction of
7, an abstract class can be no abstract methods
8, if there is a class abstract method, then this class is only the abstract class
9, abstract method to be implemented, it can not be static, it can not be private.
10, an interface can inherit an interface, multiple inheritance and interfaces, but only single inheritance class.
The above description language may be a lot of people do not understand well, do the following simple instructions:
abstract class can be understood as: a thing has properties and methods humans have attributes, gender, age, date of birth, method, eat and drink Lazard sleep, and so on. It is a description of the inherent attributes and actions of a thing.
Interface can be understood as: a thing functions can be achievedHumans can learn, you can go shopping, you can play games and more.

Question: So abstract classes and interfaces can achieve this play Peas food shopping behavior, what is the difference between them?
A: An abstract class is inherited is single, if you want to instantiate a subclass of the abstract class, then it is a single inheritance, that is to say, I can not be both human and non-human. But the interface may be multiple inheritance, I can play Peas food shopping, but does not affect me I'm human. So in the realization of certain functions, if you want to realize the essence of something, you need to use abstract classes, methods, if the implementation of things, then use the interface.

For the 10-point reprint article mentioned, I will be described in code and some text to help them understand.

1. abstract classes and interfaces can not be instantiated directly, if instantiated, abstract class variables must point to implement all the abstract methods subclass object, the interface variable must point class object implement all interface methods.

abstract class Human(var name:String)
{	
	fun eat()
	fun drink()  //错误,提示必须要在吃喝方法前加入abstract或者实现上述方法
}

If you define an abstract class human, with a method of eating and drinking, if the class is abstract, so it should also be an abstract way, that is, I know this is personal, only that he may eat and drink, but he can not know how to eat and drink. If you want to know how to eat and drink, you have to be specific to who this man. If you want to know specifically is going to find whoever this person is, we need to subclass object to inherit the abstract class.
and so

abstract class Human(var name:String)
{	
	abstract fun eat()
	abstract fun drink()
}

At this instance of a subclass is created that you know this person eat and drink what specific behavior

class Man:Human(name=String())
{
	override fun eat()
	{
		println("细嚼慢咽")
	}
	override fun drink()
	{
		println("大口喝水")
	}
}

If we define an interface:

interface eatdinner
{
	fun eatdinner(name:String)
}

This interface is used to implement eatdinner this behavior, but if we do not call this interface to call the class variable, then this interface can not be instantiated. So it is necessary

class Man:Human(name=String()),eatdinner    //要注意的是继承抽象类需要类名(),方法只需要名字
{
	override fun eat()
	{
		println("细嚼慢咽")
	}
	override fun drink()
	{
		println("大口喝水")
	}
	override fun eatdinner(name:String)
	{
		println("影响均衡的吃")
	}
}

2, abstract class to be inherited by subclasses, the interface class to be realized.

Examples or by the above sub-code, an abstract class defines the properties and methods well, abstract classes and subclasses defined by the Human instantiated, the person having the properties and methods. But after the interface is called class, it is to achieve a certain behavior. It seems that the definition of a person, a human can eat and drink. But if going to be fishing games this behavior to be a special person to do a specific job action.

3, the interface can only be done by method claims, the abstract class can declare methods do, you can do way to achieve

abstract class Human(var name:String)
{	
	fun eat()   //eat就是方法实现
	{
		println("用嘴吃")  
	}
	abstract fun drink()  //drink是方法申明
}

In the above example, if the method used to achieve, then the subclass inherits is unable to override, as if I have to define a mouth to eat, but I can not rewrite a dinner ears.

interface eatdinner
{
	fun eatdinner()
	{
		println("自由实现")
	}
}

The method can be defined internal interfaces can also be achieved, but the effect is only to affirm that you can modify or may not be modified. However, the method of the abstract class, once achieved, is not be modified.

class Man:Human(name=String()),eatdinner    //要注意的是继承抽象类需要类名(),方法只需要名字
{
	override fun drink()  //所以在这里就不需要重写 eat也不能重写eat
	{
		println("大口喝水")
	}
	override fun eatdinner(name:String)
	{
		println("营养均衡的吃")
	}
}
fun main(args: Array<String>) {
    var man1=Man()
    man1.eat()//结果为用嘴吃
    man1.drink()//结果为大口喝水
    man1.eatdinner("11")//结果为营养均衡的吃
}
Published 15 original articles · won praise 3 · Views 344

Guess you like

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