任何人都可以解释这个代码,在这种情况下创建一个对象来抽象类时会发生什么,因为我们无法实例化抽象类? [英] can anyone explain this code , what happening when an object is created to abstract class in this case as we cannot instantiate abstract class?

查看:63
本文介绍了任何人都可以解释这个代码,在这种情况下创建一个对象来抽象类时会发生什么,因为我们无法实例化抽象类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

公共抽象类Abs {





abstract void display(int a,int b);

void disp(){

System.out.println(你好);

}

}

抽象课帮助延伸Abs {



}



class main {



public static void main(String [] args){



Abs as = new Abs(){



@Override

void display(int a,int b){

}

};



帮助h =新帮助(){



@Override

无效display(int a,int b){



}

};



}



}

public abstract class Abs {


abstract void display(int a,int b);
void disp(){
System.out.println("hello");
}
}
abstract class help extends Abs{

}

class main{

public static void main(String[] args) {

Abs as= new Abs() {

@Override
void display(int a, int b) {
}
};

help h= new help() {

@Override
void display(int a, int b) {

}
};

}

}

推荐答案

什么都没发生。没有对象创建类这样的概念。这个问题毫无意义,因为 runtime 类型的任何对象都不能是任何抽象类。这是抽象类功能的目的,以防止此类的实例化。



反过来,抽象类的目的是服务器作为其他类的基类,这种方式为多态对象集提供了一个通用接口。这样的集合中的对象可以是不同的运行时类型,并且使用那时的代码可以与它们的差异无关。这样,某些方法的不同实现可以隐藏在此基类的外观后面。此类还可以用作仅一个非抽象时间的基类,以便将实现细节从基类的用户隐藏为编译时类型。



请参阅: https:/ /docs.oracle.com/javase/tutorial/java/IandI/abstract.html [ ^ ]。



-SA
Nothing happens. There is no such concept as "object created to a class". The question makes no sense, because runtime type of any object cannot be any abstract class. This is the purpose of this abstract class feature to prevent instantiation of such class.

In turn, the purpose of abstract class is to server as a base class for other classes, this way providing a common interface for the polymorphic set of objects. The objects in such set can be of different runtime types, and the code using then can be agnostic about their differences. This way different implementations of some methods can be hidden behind the facade of this base class. Such class can also be used as a base class of just one non-abstract time, for the purpose of hiding implementation detail from the user of the base class as a compile-time type.

Please see: https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html[^].

—SA


这篇关于任何人都可以解释这个代码,在这种情况下创建一个对象来抽象类时会发生什么,因为我们无法实例化抽象类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆